mysqli - $stmt->num_rows returning 0

2019-01-20 12:19发布

问题:

i am looking to count the number of records returned by the query below using mysqli / prepared statements:

$mysql = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('There was a problem connecting to the database');
$stmt = $mysql->prepare('SELECT id,vcref,jobtitle,jobtype,jobintro,closingdate FROM jobs WHERE active = 1');
$stmt->execute();
$stmt->store_result;
$stmt->bind_result($id,$vcref,$jobtitle,$jobtype,$jobintro,$closingdate);
$stmt->fetch();
$totalLiveJobs = $stmt->num_rows();

The output is consistantly 0

回答1:

You're using mysql_stmt_num_rows in the OOP style, so calling it as a function is incorrect. Try:

$stmt->num_rows;

instead of:

$stmt->num_rows();

Basically, you're trying to get this value:

class mysqli_stmt { 

   int num_rows

}

Also,

$stmt->store_result;

Should be:

$stmt->store_result();