porta325 Posted April 23, 2008 Share Posted April 23, 2008 Hey guys, i was wondering... if i have something like this $result = mysql_query("SELECT * FROM something"); while ($row = mysql_fetch_array($result)){ $some = $row['some'] } how do i get something like; if ($some == false){ echo "nothing"; } else { echo "something"; } The question is how can i return false is the array contains nothing ? Quote Link to comment Share on other sites More sharing options...
micah1701 Posted April 23, 2008 Share Posted April 23, 2008 are you just asking to see if the query found no results? if(mysql_num_rows($query) == 0){ return false; } Quote Link to comment Share on other sites More sharing options...
porta325 Posted April 23, 2008 Author Share Posted April 23, 2008 if(mysql_num_rows($result) !== 0){ echo "nothing"; } else { echo "something"; } returns "something" if the table is not empty. but i table is empty it returns nothing and i need both true and false. Quote Link to comment Share on other sites More sharing options...
Spaceman-Spiff Posted April 23, 2008 Share Posted April 23, 2008 Do this for debugging: echo '<pre>'; $result = mysql_query("SELECT * FROM something"); while ($row = mysql_fetch_array($result)){ print_r($row); } echo '</pre>'; Quote Link to comment Share on other sites More sharing options...
porta325 Posted April 23, 2008 Author Share Posted April 23, 2008 Array ( [0] => 16 [id] => 16 [1] => dssgsdg [col1] => dssgsdg [2] => [col2] => ) returns nothing if tables empty ??? Quote Link to comment Share on other sites More sharing options...
porta325 Posted April 23, 2008 Author Share Posted April 23, 2008 I wanna display a message like "Nothing in my book store on SCi-FI section" in case i have nothing in that table. Quote Link to comment Share on other sites More sharing options...
Spaceman-Spiff Posted April 23, 2008 Share Posted April 23, 2008 That means $row['some'] does not exist. There's no array key called "some". Make sure that your table actually has a column name "some" and it contains a boolean value if you want to do if ($some == false) . If you want to check if that value exists, you can use if (empty($some)) Quote Link to comment Share on other sites More sharing options...
porta325 Posted April 23, 2008 Author Share Posted April 23, 2008 Should have worked but this is stupid. Empty takes all kind of things from empty strings to declared variables with no value but still it wont work. $result = mysql_query("SELECT * FROM table"); while ($row = mysql_fetch_array($result)){ $col1 = $row['col1']; if (empty($col1)){ echo "nothing"; } else { echo "something"; } } Still displays "something" if table not empty and nothing if table empty. Quote Link to comment Share on other sites More sharing options...
Spaceman-Spiff Posted April 23, 2008 Share Posted April 23, 2008 That's because you're checking for the value of $row['col1']. In your first post, you were checking for $row['some']. They're different. From your print_r() output, you can already see that the value of 'col1' exists. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.