在网上搜索数小时后,我无法看到问题的答案 试图获取上次更新行的ID。这是我的php脚本 -
$con = mysqli_connect("localhost","DB_NAME","PWD");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db($con,"DB") or die ("no database");
$result=mysqli_query($con,"SET @update_ID := 0;
UPDATE TABLE_234 SET PLAYERNAME = PETER, ID = (SELECT @update_ID := ID)
WHERE PLAYERNAME IS NULL
LIMIT 1;
SELECT @update_ID ");
if (!mysqli_query($con,$result))
{
die('Error: ' . mysqli_error($con));
}
echo $ID;
mysqli_close($con);
错误 -
警告:mysqli_query()[function.mysqli-query]:在...中的空查询...
错误:SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在...附近使用正确的语法。
感谢您的帮助。
答案 0 :(得分:1)
<强>编辑:强> 你试图在最后一行显示$ ID;但你在哪里用PHP代码设置它?
试试这个:
$con = mysqli_connect("localhost","DB_NAME","PWD");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db($con,"DB") or die ("no database");
$sql="SET @update_ID := 0;
UPDATE TABLE_234 SET PLAYERNAME = PETER, ID = (SELECT @update_ID := ID)
WHERE PLAYERNAME IS NULL
LIMIT 1;
SELECT @update_ID as ID";
if (mysqli_multi_query($con,$sql))
{
do
{
// Store first result set
if ($result=mysqli_store_result($con))
{
while ($row=mysqli_fetch_row($result))
{
$ID=$row[0];
}
}
}
while (mysqli_next_result($con));
}
echo "ID=" . $ID;
mysqli_close($con);
答案 1 :(得分:0)
您在代码中呼叫mysqli_query
两次。第一种方式是正确的方式,第二种方式是错误的。在第一次调用中,您传递的是SQL字符串,但是在第二次调用中,您传递的是结果。
根据您的代码,我认为您要在$result
声明中检查if
,如下所示:
if (!$result)
{
die('Error: ' . mysqli_error($con));
}
答案 2 :(得分:0)
尝试这样的事情
$con = mysqli_connect("localhost","DB_NAME","PWD");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db($con,"DB") or die ("no database");
$result=mysqli_query($con,"SET @update_ID := 0;
UPDATE TABLE_234 SET PLAYERNAME = PETER, ID = (SELECT @update_ID := ID)
WHERE PLAYERNAME IS NULL
LIMIT 1;
SELECT @update_ID ");
if (!$result)
{
die('Error: ' . mysqli_error($con));
}
echo $ID;
mysqli_close($con);