Jump to content

How to check if database query is null?


shadrxninga

Recommended Posts

I'm trying to get a nickname system working for a little project, but I've run into a problem.

 

This code that I wrote here gets the users nickname from their username:

 

$user = $_GET['user'];
@mysql_select_db("db") or die( "Unable to select database");

$query = mysql_query("SELECT `nickname` FROM `info` WHERE `username`='$user'");

       WHILE($rows = mysql_fetch_array($query)):
           $nickname = $rows['nickname'];
           echo $nickname;
        endwhile;
?>

 

But the problem is, if the user doesn't have a nickname then it just turns up blank. How can I check if the output of $nickname is blank and if it is then just return their normal name ($user)

 

Thanks

 

Oh, and how to you format php code to look like php code on these forums?

Link to comment
Share on other sites

use the [ php \] and [/ php \] tags without the ending backslashes.

 

For your query just use !empty

 

WHILE($rows = mysql_fetch_array($query)):
           $nickname = (!empty($rows['nickname'])) ? $rows['nickname'] : $rows['username']; // tells it to display username if nickname is empty
           echo $nickname;
        endwhile;

 

Link to comment
Share on other sites

use the [ php \] and [/ php \] tags without the ending backslashes.

 

For your query just use !empty

 

WHILE($rows = mysql_fetch_array($query)):
           $nickname = (!empty($rows['nickname'])) ? $rows['nickname'] : $rows['username']; // tells it to display username if nickname is empty
           echo $nickname;
          endwhile;

 

There's only one problem with that. It may sound strange but... If the $nickname variable is blank - I want it to return the $user variable from the url.

That code will only return the username value in the database.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.