MYSQLI multi-SQL statement execution related issues

Source: Internet
Author: User
MYSQLI multi-SQL statement execution issues
 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 '), [email protected] ', 0);";
$sql. = "INSERT into login (user1, pass, email, type) VALUES (' Dengchao1 ', MD5 (' dengchao123 '), [email protected] ', 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 do you get an error: Commands out of sync; You can ' t run the This command now
This error, the corresponding query statement is
$sql = "SELECT * from Login";
$res = $mysqli->query ($sql);

Very puzzled, please answer, no Baidu to.
------Solution--------------------
Release the multi_query result set first, then query
  • 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.