PDA

View Full Version : odd experience with num_rows


songboy
03-15-2010, 12:02 AM
I'm using mysqli_num_rows to decide if an exact piece of data exists in a data table. This is the code:

$query1 = sprintf("SELECT theme_word FROM new_themes WHERE theme_word = '$theme_tag_two'");
$result1 = mysqli_query($connect, $query1) or die('Error, query 1 failed');
$num_rows_theme = mysqli_num_rows($result1);

if($num_rows_theme ???)
{
$query3 = ("INSERT INTO new_themes (theme_word) VALUES ('$theme_tag_two')");
$result3 = mysqli_query($cxn, $query3) or die('Error, query 3 failed');
}

My issue involves the ??? above.
Firstly, my test involves a theme tag that does not exist in the table. Obviously, as this new tag doesn't exist in the table, I will want it to go into the table.
If I put = 0 where you see ??? the new theme tag is not put into the table, even though there is no other tag in the table that is identical to it.
BUT...if I put in < 1 for ??? above, the tag is sent to the table.
Surely, if the new tag is not already in the table then there is no match so = 0 should work.
Is this just odd or is there something I'm not getting ?

Thanks -
Songboy

davidj
03-15-2010, 12:49 PM
it may be returning a boolean so could be evaluating to -1

output the value to see

songboy
03-16-2010, 12:08 AM
I got the value and it was 0.
Anyway, I tried:
if($num_rows_theme == 0)
etc etc and it worked.

I forgot that !!
PS Thanks dj for the prompt.
Songboy