0

I am trying to use the mysqli to access a mysql database and a table called phptest, with the code below. I am trying to check if row is = 0 or something else but it does not echo anything. What is wrong with the code?

$mysqli = new mysqli("localhost", "root", "", "phptest");
if (!$mysqli) {
    echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error();
    exit;
}
$query = "SELECT `id` FROM `test` WHERE `email`='[email protected]' AND `password`='5f4dcc3b5aa765d61d8327deb882cf99'";
$query_run = $mysqli->query($query);
$row_cnt = $query_run->num_rows;
if ($row_cnt==0) {
    echo 'wrong..';
} else {
    echo 'correct!';
}

EDIT: edited the variable in the if statement. I have runned the query directly in the mysql panel in the tabel. I get the right row count then. Why doesnt it work in php ?

2
  • 1
    $row_count is not defined. Turn on strict error reporting with error_reporting(E_ALL | E_STRICT); and you get a notification. Commented Jun 24, 2012 at 16:55
  • I did a mistake, in my file it says $row_cnt instead of count. Have edit it now. but that is not the problem. It seems like the if statement are not being runned or something like that. Any solution ? Commented Jun 24, 2012 at 18:01

2 Answers 2

1

Although you are checking for an undefined variable $row_count (which should be $row_cnt, that isn't your immediate problem (you should still fix this). You are getting a fatal error either because:

  • Your query is failing. Your query may be failing because of a missing field on the table or indeed you are missing the actual table in the database. The query will return FALSE if it fails, but you aren't checking for that, instead you try to access a property of the result. If the query fails, there is no result object, it is a boolean FALSE and will produce a fatal error something like Fatal Error: Trying to access a property of a none object.

  • You don't have the MySQLi library configured for your PHP installation

To further debug you should turn on error reporting as below, or consult your server's error log.

error_reporting(E_ALL);
ini_set('display_errors', '1');
Sign up to request clarification or add additional context in comments.

Comments

0

One thing is that you defined $row_cnt and then checked for $row_count, but that's just a quick look at your program, so there might be something else wrong too.

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.