user1247567
user1247567

Reputation: 15

How can I make a mysql_query a true or false variable in PHP?

I am looking to have the sql_query look into the Table and tell me in a $variable if the item is in the Table or not.

Am I going about this all wrong?

$string = mysql_query("SELECT * FROM Table WHERE Column = 'item'");

if ( $string == true ) {
    $roger = "Found it!";
    } else {
    $roger = "Sorry dude!";
    }

echo $roger;

Upvotes: 1

Views: 123

Answers (1)

Daniil Ryzhkov
Daniil Ryzhkov

Reputation: 7596

Use mysql_num_rows() http://www.php.net/manual/en/function.mysql-num-rows.php

$resource = mysql_query("SELECT * FROM Table WHERE Column = 'item'");

if ( mysql_num_rows($resource) > 0 ) {
    $roger = "Found it!";
} else {
    $roger = "Sorry dude!";
}

echo $roger;

Upvotes: 3

Related Questions