shadrxninga Posted September 12, 2011 Share Posted September 12, 2011 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 https://forums.phpfreaks.com/topic/246948-how-to-check-if-database-query-is-null/ Share on other sites More sharing options...
doddsey_65 Posted September 12, 2011 Share Posted September 12, 2011 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 https://forums.phpfreaks.com/topic/246948-how-to-check-if-database-query-is-null/#findComment-1268240 Share on other sites More sharing options...
shadrxninga Posted September 12, 2011 Author Share Posted September 12, 2011 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 https://forums.phpfreaks.com/topic/246948-how-to-check-if-database-query-is-null/#findComment-1268243 Share on other sites More sharing options...
doddsey_65 Posted September 12, 2011 Share Posted September 12, 2011 WHILE($rows = mysql_fetch_array($query)): $nickname = (!empty($rows['nickname'])) ? $rows['nickname'] : $user; // tells it to display username if nickname is empty echo $nickname; endwhile; Link to comment https://forums.phpfreaks.com/topic/246948-how-to-check-if-database-query-is-null/#findComment-1268244 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.