Jump to content

if statement not working


harkly

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/200102-if-statement-not-working/
Share on other sites

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.