简体   繁体   English

尝试创建MySQL变量时发生PHP / MySQL错误

[英]PHP/MySQL error while trying to create MySQL variable

I'm running this code in PHP: 我正在PHP中运行以下代码:

mysql_query("SET @update_id:=NULL");
echo mysql_error();

And this is what I get: 这就是我得到的:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 检查与您的MySQL服务器版本相对应的手册,以在第1行的''附近使用正确的语法

Also this same code runs perfectly in PHPMyAdmin. 同样,此代码也可以在PHPMyAdmin中完美运行。 What am I doing wrong? 我究竟做错了什么?

Additional information. 附加信息。 I'm trying to write a query described here: How to get ID of the last updated row in MySQL? 我正在尝试编写一个查询,描述如下: 如何获取MySQL中最后更新的行的ID? But the problem right now is that I even can't run a simple query to create variable. 但是现在的问题是,我什至无法运行简单的查询来创建变量。

PS Ok, now it seems that it desn't work because of some previous queries that are not related to this one. PS好的,现在似乎由于某些先前与此查询无关的查询而无法使用。 If i move this query to the top of the php file it works. 如果我将此查询移到php文件的顶部,它将起作用。 Also if I try to make this: 另外,如果我尝试这样做:

mysql_query("SET @update_id:=NULL; SELECT @update_id;"); mysql_query(“ SET @update_id:= NULL; SELECT @update_id;”); It fails with syntax error. 它因语法错误而失败。 But this works fine: mysql_query("SET @update_id:=NULL;"); 但这工作正常:mysql_query(“ SET @update_id:= NULL;”); mysql_query("SELECT @update_id;"); mysql_query(“ SELECT @update_id;”); Does somebody knows what am I missing here? 有人知道我在这里想念什么吗? Why can't I run two commands in one query and why they're the separate queries are related to each other? 为什么我不能在一个查询中运行两个命令,为什么它们是单独的查询却彼此相关?

mysql_query("UPDATE your_table SET update_id=NULL");

检查这可能会有所帮助

SELECT @update_id IN("SET @update_id:=NULL");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM