Mysqli multi-SQL statement execution problems

Source: Internet
Author: User
Mysqli multi-SQL statement execution & lt ;? Php // object creation $ mysqli & nbsp ;=& nbsp; new & nbsp; mysqli ('localhost', & nbsp; 'root', & nbsp; 'nagiosxi ', 'hrsystem'); // connection error if ($ mysqli-& gt; conn mysqli multi-SQL statement execution problem
 // Object creation
$ Mysqli = new mysqli ('localhost', 'root', 'nagiosxi', 'hrsystem ');
// Connection error
If ($ mysqli-> connect_error)
{
Die ('connect database error'. mysqli_connect_error ());
}
$ SQL = "insert into login (user, pass, email, type) values ('dengchao1', md5 ('dengchao123'), 'dengchao1 @ gmail.com ', 0 );";
$ SQL. = "insert into login (user1, pass, email, type) values ('dengchao1', md5 ('dengchao123 '), 'dengchao1 @ gmail.com', 0 )";
$ Res = $ mysqli-> multi_query ($ SQL );
If ($ res & $ mysqli-> affected_rows> 0)
{
Echo "insert OK Rows: ". $ mysqli-> affected_rows;
}
Else
{
// $ Mysqli-> close ();
Die ('Insert error '. $ Mysqli-> error. $ mysqli-> close ());
}
If (! Is_bool ($ res ))
{
$ Res-> free ();
}

$ SQL = "select * from login ";
$ Res = $ mysqli-> query ($ SQL );
Echo $ mysqli-> error ." ";
If ($ res)
{
While ($ row = $ res-> fetch_row ())
{
Foreach ($ row as $ key => $ val)
{
Echo "-- $ val ";
}
Echo" ";
}
}
If (! Is_bool ($ res ))
{
$ Res-> free ();
}
$ Mysqli-> close ();
?>

Why is the error: Commands out of sync; you can't run this command now
The corresponding query statement is
$ SQL = "select * from login ";
$ Res = $ mysqli-> query ($ SQL );

I am not very puzzled. please answer the question, not Baidu.
------ Solution --------------------
Release the result set of multi_query before querying

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.