0

I'm using PHP/MySQL to verify that one value exists in the column "tokencol". "$token" is the value that I want to verify and I'm sure that it has been entered properly.

Below is my code:

$verifytoken = mysql_query("SELECT * from tokens where tokenrow = '$token'");


if(mysql_num_rows($verifytoken) == TRUE) {
     echo "hello";
} else {
    die("Valid token not provided.");
}

I always get the "Valid token not provided." message instead of "hello".

What should I do to fix? Thanks

2
  • var_dump or print_r result of query $verifytoken and also check what mysql_error says. Commented Feb 18, 2014 at 8:10
  • var_dump shows string(0) "" Commented Feb 18, 2014 at 8:24

4 Answers 4

3

mysql_num_rows() returns a integer value and not Boolean value.so what you was trying is basically wrong. So try comparing with int values like this

if(mysql_num_rows($verifytoken) != 0) {
     echo "hello";
} else {
    die("Valid token not provided.");
}

also mysql_* functions are deprecated from php5.5. so try to stop using it and start using mysqli_* functions or PDO

Sign up to request clarification or add additional context in comments.

4 Comments

no. if it is == 0 means not found and != 0 means found. because it returns no of rows the query returns
Weird...It's now showing that the value is not found.
$dbconnect = mysql_connect('mysqlhost', 'mysql-user', 'mysql-password', 'mysql-db'); Does it look right?
the dbconnect was correct.Make sure that you have a row in database having $token value as tokenrow value
2

Are you sure the database connection is ok and you have that value in the tokens table?

Try with $verifytoken = mysql_query("SELECT * from tokens where tokenrow = '$token'") or die(mysql_error());

Comments

0

It's better do it like this, if you need only amount of toknes:

$verifytoken = mysql_query("SELECT COUNT(*) from tokens where tokenrow = '$token'");

if(mysql_result($verifytoken, 0) > 0) {
     echo "hello";
} else {
    die("Valid token not provided.");
}

With mysql_num_rows mysql has to prepare structure for returned data and then count the result. With COUNT it will directly return just amount. Also, if you have for example some tokenid column in your table, do it like SELECT COUNT(tokenid)....

Comments

0
$verifytoken = mysql_query("SELECT * from tokens where tokenrow = '$token'");
if($row_verifytoken = mysql_fetch_assoc($verifytoken))
{
    echo "hello"; 
}
else 
{
    die("Valid token not provided.");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.