johntp Posted July 25, 2008 Share Posted July 25, 2008 Hey guys, What I'm trying to acheive is pretty much call my sql database and say if this username isnt in the database then echo whatever, else echo all the data with it. Here is what i have <?php $query2 = "SELECT * FROM info WHERE Username LIKE '$user'"; if ($query2 = "") { echo "what the heck dood"; } else { $query = "SELECT Username, Address, Phone FROM info WHERE Username = '$user'"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "Address: {$row['Address']} <br>"; echo "Phone: {$row['Phone']}"; } } ?> and whats happening when i do this, it works for people with usernames in the db, but if your username is not in there than it's just blank. I'm not even sure if what i have would work as an if else statement so any help is appreciated. Link to comment https://forums.phpfreaks.com/topic/116640-solved-if-else-statement-help/ Share on other sites More sharing options...
jonsjava Posted July 25, 2008 Share Posted July 25, 2008 problem: you were saying "if the query variable is empty"(which it never is), "echo something", else, "run the query, more selectivly". solution: <?php $query2 = "SELECT * FROM info WHERE Username LIKE '$user'"; $result = mysql_query($query2); if (mysql_num_rows($result) == 0) { echo "what the heck dood"; } else { $row = mysql_fetch_assoc($result); echo "Address: {$row['Address']}<br />\n"; echo "Phone: {$row['Phone']}\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/116640-solved-if-else-statement-help/#findComment-599743 Share on other sites More sharing options...
johntp Posted July 25, 2008 Author Share Posted July 25, 2008 thanks works great. Link to comment https://forums.phpfreaks.com/topic/116640-solved-if-else-statement-help/#findComment-599747 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.