harkly Posted April 29, 2010 Share Posted April 29, 2010 I am trying to check a table and determine if it is empty, for some reason I cannot get it to work. I have really dumbed down the code for myself but still can't figure out why it isn't working. Right now my table is all set to NULL, I have made changes to 0 and to 1 but to no avail. <?php include('library/login.php'); login(); mysql_select_db('test'); $sql = mysql_query("SELECT * FROM music WHERE userId = 'test'"); $results = mysql_fetch_array( $sql ); if ($results == NULL) { echo "Empty n"; } else { echo "Not Empty n"; } ?> Perhaps this is not the best way to approach this?? Can someone help? Quote Link to comment https://forums.phpfreaks.com/topic/200102-if-statement-not-working/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 29, 2010 Share Posted April 29, 2010 Perhaps if you read what mysql_fetch_array() returns - http://us3.php.net/mysql_fetch_array It will never return a NULL value. If you are trying to determine how many rows were matched by the query, see the mysql_num_rows() function. Quote Link to comment https://forums.phpfreaks.com/topic/200102-if-statement-not-working/#findComment-1050249 Share on other sites More sharing options...
harkly Posted April 29, 2010 Author Share Posted April 29, 2010 Tried that and wasn't able to get that to work either I always get this output, "1 Rows ", regardless if there is a valid userID or if the info is set to Null, 0 or 1. <?php include('library/login.php'); login(); mysql_select_db('test'); $result = mysql_query("SELECT * FROM music WHERE userId = 'test'") or die(mysql_error());; $num_rows = mysql_num_rows($result); echo "$num_rows Rows\n"; ?> What I am trying to do is determine if there is any info in the table under the userID and if so one set of code will run, if not than another set of code will run. Quote Link to comment https://forums.phpfreaks.com/topic/200102-if-statement-not-working/#findComment-1050270 Share on other sites More sharing options...
callesson Posted April 29, 2010 Share Posted April 29, 2010 Remove one " ; " ? $result = mysql_query("SELECT * FROM music WHERE userId = 'test'") or die(mysql_error());; Quote Link to comment https://forums.phpfreaks.com/topic/200102-if-statement-not-working/#findComment-1050338 Share on other sites More sharing options...
harkly Posted April 29, 2010 Author Share Posted April 29, 2010 Maybe this is not the way to go. What I want to verify is if in the table under the userID is anything there, but not that there is a row with the userID in it. Quote Link to comment https://forums.phpfreaks.com/topic/200102-if-statement-not-working/#findComment-1050455 Share on other sites More sharing options...
TheOnly92 Posted April 29, 2010 Share Posted April 29, 2010 Or maybe try this: <?php $row = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) AS n_rows FROM music WHERE userId='test'")); if ($row['n_rows'] > 0) { echo "Not empty"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/200102-if-statement-not-working/#findComment-1050461 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.