This article illustrates a solution to the inconsistency of the return value of a stored procedure called by PHP. Share to everyone for your reference, specific as follows:
I met a classmate today. The stored procedure return value often gets an unexpected value of NULL, because during the day, do an experiment in the evening and put it here for students with corresponding questions to see.
Stored procedures:
delimiter//
createprocedureusp_s2 (outpar1int)
begin
Selectinet_ntoa (IP), portfromproxy_listlimit5;
SelectCount (*) intopar1fromproxy_list;
end//
delimiter;
Session 1 Execution:
Mysql>callusp_s2 (@a);
+ ————— +--+
|inet_ntoa (IP) |port|
+ ————— +--+
|1.34.21.86 |8088|
| 1.34.59.50 |8088|
| 1.34.69.15 |8088|
| 1.34.73.110 |8088|
| 1.34.76.218 |8088|
+ ————— +--+
5rowsinset (0.00sec)
queryok,1rowaffected (0.01sec)
mysql>select@a;
+--+
|@a |
+--+
|4430|
+--+
1rowinset (0.00sec)
Session 2 Execution:
mysql>select@a;
+--+
|@a |
+--+
| null|
+--+
1rowinset (0.00sec)
The results of two sessions are shown to be inconsistent. It is almost certain that two calls fall into different sessions and get different values.
For consistency it can be invoked as follows:
<?php
$hostname = "127.0.0.1";
$username = "WUBX";
$password = "WUBXWUBX";
$database = "Proxydb";
$db =newmysqli ($hostname, $username, $password, $database);
if (Mysqli_connect_errno ()) {
printf ("Connect failed:%s\n", Mysqli_connect_error ());
Exit ();
}
$result = $db->multi_query ("Call Usp_s2" (@total); Select @total; ");
if ($result) {
do{
if ($r = $db->store_result ()) {
if ($r->field_count==2) {while
($row = $r- >fetch_row ()) {
print IP: $row [0], Port: $row [1]\n;
}
} else{
$row = $r->fetch_row ();
print ' total: $row [0]\n ';}}}
while ($db->next_result ());
}
$db->close ();
? >
$PHPT _proc_return.php
ip:1.34.21.86,port:8088
ip:1.34.59.50,port:8088
ip:1.34.69.15,port:8088
ip:1.34.73.110,port:8088
ip:1.34.76.218,port:8088
total:4430
Good luck.
More about PHP Interested readers can view the site topics: "MySQL stored process techniques," "PHP Data Structure and algorithm tutorial", "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", " PHP Operations Office Document Tips summary (including word,excel,access,ppt), "PHP Date and Time usage summary", "PHP Introduction to Object-oriented Programming", "PHP string (String) Usage Summary", "php+ MySQL Database operations Introduction tutorial and PHP Common database operation Skills Summary
I hope this article will help you with the PHP program design.