PHP: How to call a stored procedure

The following code snippet was created in MySQL Workbench:

DELIMITER //
CREATE PROCEDURE getCustomers()
BEGIN
	SELECT * FROM customers;
END //
DELIMITER ;

CALL getCustomers(); // This works just fine, I get the result set

Open in new window


But I'm having a problem on the PHP side. Here's the code in PHP:

    $stmt = $db->prepare("call getCustomers()");
    $result = $stmt->execute();
    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
        echo $row['custid'] . '<br/>';
    }

Open in new window


I am getting a PHP error that says, "Fatal error: Call to a member function fetch() on boolean in C:\xampp\htdocs\newdimension\public\DBPrep.php".

Can someone please tell me what I'm doing wrong? Thanks.
elepilAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
Try:
$result = $db->query($sql);
$result->setFetchMode(PDO::FETCH_ASSOC);
while ($row = $result->fetch() ) {
        echo $row['custid'] . '<br/>';
    }

Open in new window

use prepare if you need to supply arguments to the stored procedure:

FYR:
http://www.mysqltutorial.org/php-calling-mysql-stored-procedures/
MlandaTCommented:
You are calling the fetch() on $result instead of on $stmt.
You need to do a $stmt->fetch(PDO::FETCH_ASSOC) and not a $result->fetch(PDO::FETCH_ASSOC). $result is actually a boolean as returned by the PDO::execute statement.

That said, it might be a goot idea to add some error handling as well...
    $stmt = $db->prepare("call getCustomers()");
    if($stmt->execute()) {
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            echo $row['custid'] . '<br/>';
        }
    }else {
        echo "\nPDOStatement::errorInfo():\n";
        $arr = $stmt->errorInfo();
        print_r($arr);
    }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
elepilAuthor Commented:
I'm learning how to use both PDO and mysqli, and I keep getting confused which object returns the results.

Thanks for the help, guys.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.