Link to home
Start Free TrialLog in
Avatar of elepil
elepil

asked on

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.
SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of elepil
elepil

ASKER

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.