_agx_
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:
But my test page simply outputs the header and nothing else:
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?
The docs say it should output something like:
---
array(3) {
[0]=>
array(1) {
[0]=>
string(1) "1"
}
[1]=>
.....
But my test page simply outputs the header and nothing else:
---
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?
What is the result of the stored procedure when you run it directly from MySQL?
ASKER
In MySQLWorkbench, call p(); returns
id + 1
----------
2
3
4
id + 1
----------
2
3
4
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
$mysqli = new mysqli("localhost", "myuser", "mypassword", "mydatabase");
(Edit) Maybe some sort of permissions problem ... or error occurring that isn't shown?
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());
?>
It worked for me with PHP 5.3.8 and MySQL 5.0.45.
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.
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.
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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".
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".
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.
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).
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).
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!
Thanks everyone for the responses!