views:

512

answers:

1

Hello everyone, 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

A: 

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();
karim79
thx for input m8 - i still getting used to OOP style,I thought i'd done it right already? in the final line of code? trying to assign the value to a variable?
Aaron Bentley
No, $stmt->num_rows() is calling a non-existent method of $stmt, whereas $stmt->num_rows references the property (which does have a value). See http://phpbuilder.com/manual/en/function.mysqli-stmt-num-rows.php
karim79
Thx Karim - i understand now, (extra kudos for the link too).It's working just fine now - thx again mate :)
Aaron Bentley