...

View Full Version : PHP MySQL Stored Procedure Getting Number of Rows Returned



jonatec
02-01-2012, 09:58 AM
Hi

Please take a look at my code snippet. Works fine, however I don't know how to get the number of rows returned from a MySQL stored procedure so that I can handle it accordingly. Any ideas please?

Thanks.



<?php
require ('mysqli_connect.php'); // Connect to the Db.

$sql = "CALL customers_for_rep(?, ?)";

$stmt = $dbc->prepare($sql);

if ($dbc->errno) {die($dbc->errno.":: ".$dbc->error);}

$stmt->bind_param("is", $i_user_id, $i_firstName);

$i_user_id = 2;
$i_firstName = "David";

$stmt->execute( );

$rows = $stmt->num_rows;

if ($dbc->errno) {die($dbc->errno.": ".$dbc->error);}

$stmt->bind_result($first_name, $last_name);

echo "Rows returned: " . $rows . "</p>";

$buf = "";
$buf .= '<table align="center" cellspacing="3" cellpadding="3" width="70%" border="1">';
$buf .= ' <tr>';
$buf .= ' <td class="td01">First Name</td>';
$buf .= ' <td class="td01">Last Name</td>';
$buf .= ' </tr>';
while ($stmt->fetch( )) {
$buf .= '<tr>';
$buf .= ' <td>' . $first_name . '</td>';
$buf .= ' <td>' . $last_name . '</td>';
$buf .= '</tr>';
}
$buf .= '</table>';
echo $buf;
echo "<p/>made it.";
mysqli_close($dbc);
?>



DELIMITER $$
DROP PROCEDURE IF EXISTS customers_for_rep$$
CREATE PROCEDURE customers_for_rep
(
IN i_user_id INT,
IN i_firstName VARCHAR(100)
)
BEGIN
SELECT first_name, last_name
FROM users
WHERE users_id > i_user_id;
END $$
DELIMITER ;

Keleth
02-01-2012, 03:17 PM
I'm pretty sure num_rows is mysqli. In PDO, you want rowCount(). So $stmt->rowCount(). Take a look at the PDO manual, it has it all in there.

jonatec
02-01-2012, 03:37 PM
Ok thanks, I will try and get into PDO.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum