Wasif Nadeem Wasif Nadeem - 6 months ago 10
PHP Question

Error on fetching number of rows

I am getting error "Trying to get property of non-object on line 76" while executing following:

require_once 'DBConnection.php' ;
$connection = new mysqli($hostname, $username, $password, $database);

if ($connection->connect_error) die ($connection->connect_error);
$query = "Select * From product where productCategory = $productCategory";
$result = $connection->query($query);

$rows = $result->num_rows; //line 76

for ($j = 0 ; $j < $rows ; ++$j)
{
$result->data_seek($j);

$row = $result->fetch_array(MYSQLI_NUM);

Answer

You should use mysql_num_rows() function. For your given code, it must be like mysql_num_rows($result).

You can refer PHP documentation of mysql_num_rows() for further details.