PHP calls the solution to the inconsistency problem of the return value of stored procedure analysis _php Skills

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.