Anonym..
Anonym..

Reputation: 333

php mysql get row

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 ?

Upvotes: 0

Views: 981

Answers (2)

MrCode
MrCode

Reputation: 64526

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');

Upvotes: 1

Gustav Larsson
Gustav Larsson

Reputation: 8487

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.

Upvotes: 0

Related Questions