Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2008
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    PHP MySQL Stored Procedure Getting Number of Rows Returned

    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 Code:
    <?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);
    ?>
    Code:
    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 ;

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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.

  • Users who have thanked Keleth for this post:

    jonatec (02-01-2012)

  • #3
    New Coder
    Join Date
    Aug 2008
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ok thanks, I will try and get into PDO.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •