Link to home
Start Free TrialLog in
Avatar of _agx_
_agx_Flag for United States of America

asked on

PHP + mysqli + dislaying stored orocedure results

I'm brand new to PHP. I need to retrieve the results of a mysql stored procedure. So I looked at the docs and tried example #3. The only change was host and db credentials.

The docs say it should output something like:

---
array(3) {
  [0]=>
  array(1) {
    [0]=>
    string(1) "1"
  }
  [1]=>
  .....

Open in new window


But my test page simply outputs the header and nothing else:
    ---

Open in new window


If I log into the db, the procedure p() exists, and returns data when executed. Yet my php script shows nothing but "---".

Any ideas on what might be going wrong or how to debug it?
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

What is the result of the stored procedure when you run it directly from MySQL?
Avatar of _agx_

ASKER

In MySQLWorkbench, call p(); returns

id + 1
----------
2
3
4
SOLUTION
Avatar of Dan Craciun
Dan Craciun
Flag of Romania 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
Avatar of _agx_

ASKER

It's literally a copy and paste of example #3. I just deleted and recreated the file to be sure.  Only thing I changed was the 2nd line:

$mysqli = new mysqli("localhost", "myuser", "mypassword", "mydatabase");

(Edit)  Maybe some sort of permissions problem ... or error occurring that isn't shown?
Avatar of _agx_

ASKER

It works here. MySQL 5.6, php 5.5
(Edit)  Thanks for the sanity check.  I'm running:

PHP 5.3.3
MySQL 5.1.69

Here it my test file fwiw:

<?php

$mysqli = new mysqli("localhost", "myuser", "mypassword", "mydatabase");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if (!$mysqli->query("DROP TABLE IF EXISTS test") ||
    !$mysqli->query("CREATE TABLE test(id INT)") ||
    !$mysqli->query("INSERT INTO test(id) VALUES (1), (2), (3)")) {
    echo "Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

if (!$mysqli->query("DROP PROCEDURE IF EXISTS p") ||
    !$mysqli->query('CREATE PROCEDURE p() READS SQL DATA BEGIN SELECT id FROM test; SELECT id + 1 FROM test; END;')) {
    echo "Stored procedure creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

if (!$mysqli->multi_query("CALL p()")) {
    echo "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

do {
    if ($res = $mysqli->store_result()) {
        printf("---\n");
        var_dump($res->fetch_all());
        $res->free();
    } else {
        if ($mysqli->errno) {
            echo "Store failed: (" . $mysqli->errno . ") " . $mysqli->error;
        }
    }
} while ($mysqli->more_results() && $mysqli->next_result());
?>

Open in new window

It worked for me with PHP 5.3.8 and MySQL 5.0.45.
Avatar of _agx_

ASKER

Yeah, obviously something's different in my environ .. but I'm not sure what :/ Permissions maybe?  But I'd expect to see an error, but there's nothing in the output.

I'll check back tomorrow if anyone has any ideas on how to troubleshoot this.
Add "error_reporting(E_ALL);" as the second line of your script and see if you get any messages.
Use root to connect to your database. If the result displays, it's a permission problem.
If it's not 'localhost', "root" usually isn't allowed to connect.
Avatar of _agx_

ASKER

> Add "error_reporting(E_ALL);" as the second line of your script

No change.

   <?php
      error_reporting(E_ALL);
      ...

> Use root to connect to your database.
> If it's not 'localhost', "root" usually isn't allowed to connect.

Tried it for the heck of it, same result: "---"

I also added some debug statements, before and after the dump.

        printf("---\n");
        echo "before dump";
        var_dump($res->fetch_all());
        echo "after dump";
        $res->free();

Only the "before" statement prints:

       --- before dump
Run phpinfo() and see the the 'mysqlnd' driver is loading.  This page http://us1.php.net/manual/en/mysqli-result.fetch-all.php says it only works with the 'mysqlnd' driver.  And the 'mysqlnd' driver is not the default driver until PHP 5.4.
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
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 _agx_

ASKER

> Just for giggles try

No change.

> Run phpinfo() and see the the 'mysqlnd' driver is loading.

Silly question - what would it look like if it were loaded?  Can't post a screen shot right now, but I see categories for "mysql" (Client API 5.1.69 ....) and "mysqli", but a find in page turns up nothing for "mysqln".
It would look a lot like this (in phpinfo()):
User generated image
Avatar of _agx_

ASKER

Thanks. Mine goes from "mysqli" to "odbc", nothing in between. So I think that confirms it..
Try my code above in https://www.experts-exchange.com/questions/28409081/PHP-mysqli-dislaying-stored-orocedure-results.html?anchorAnswerId=39992636#a39992636 .  I hacked your code up to work on PHP 5.2.17 which does not have 'mysqlnd' available.  It's not exact but the first part of the display is.
I just double checked and there is an extension for 'php_mysqli_mysqlnd.dll' on my machine.  While it's not 'enabled' on my machine, you may be able to 'enable' it on yours and get the original code working.  That is, if it is included with your copy and version of PHP.
Avatar of _agx_

ASKER

> Try my code above in

Oh, thanks for that.  I totally missed it in all my multi-tasking.  I have to get something out the door, in the next hour, but will try it (and your other suggestion after that).
Avatar of _agx_

ASKER

@Dave Baldwin - Perfect, that version works for me too.  Now I actually have something to work with besides a blank screen :) It's probably the missing mysqlnd driver like you said. I'll try installing it later.  

Thanks everyone for the responses!