我试图让这段代码正常工作,但是有什么地方不对劲。
<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "db";
$connection = mysqli_connect($server, $username, $password, $database);
if(mysqli_connect_errno())
{
echo("Failed-Connect");
exit();
}
else
{
if(mysqli_ping($connection))
{
$query =
"
SET @a = 5;
SET @b = 2;
SELECT @c := col FROM whatever WHERE doesn't matter;
IF @a > @b THEN
UPDATE table2 SET col1 = col1 + 1, col2 = @c WHERE somthing = 'thething';
END IF;
";
$result = mysqli_multi_query($connection, $query);
if($result)
{
echo("Done");
}
}
else
{
echo("Failed-Ping");
}
}
mysqli_close($enter code hereconnection);
?>此代码没有错误,但它没有更新行。当我删除IF语句时,它完美地工作,但我需要该IF语句来验证某些内容。你知道怎么回事吗?如果代码有重大问题,你能给我提供一个替代方案吗?
发布于 2018-03-04 12:40:07
多个查询不等于存储的例程和代码块,如:
IF @a > @b THEN
UPDATE table2 SET col1 = col1 + 1, col2 = @c WHERE somthing = 'thething';
END IF;仅在存储例程的上下文中有效。因此,给定的代码有一个错误,如果您添加了错误检查,MySQL会告诉您这一点。
你可以这样写:
UPDATE table2 SET col1 = col1 + 1, col2 = @c WHERE somthing = 'thething' AND (@a > @b);因此,只有在@a > @b和somthing = 'thething'的情况下才会进行更新。
https://stackoverflow.com/questions/49095399
复制相似问题