简体   繁体   English

我可以在 PHP 中使用 DECLARE 作为 SQL 语句吗

[英]Can I use DECLARE as an SQL statement in PHP

I am trying to execute a query in PHP, but this code wouldn't work for some reason, it doesn't detect the keywords at all(DECLARE, SELECT, WHERE).我正在尝试在 PHP 中执行查询,但是由于某种原因,此代码无法正常工作,它根本没有检测到关键字(DECLARE,SELECT,WHERE)。 Is there anything I can do and after all can I even use DECLARE in PHP as a mySQLi statement.有什么我可以做的,毕竟我什至可以在 PHP 中使用 DECLARE 作为 mySQLi 语句。

$sql2 ="DECLARE @MaxID INT; SELECT @MaxID = MAX(productID) FROM products; UPDATE sunglasses SET sunglassesId = @MaxID WHERE sunglassesId = 0;";

After all I am trying to update a field in Table1 where its initial value is 0 with a value from a field in Table2.毕竟,我试图用 Table2 中的一个字段的值来更新 Table1 中初始值为 0 的字段。 Hope that made sense.希望这是有道理的。

PS tried this query in Microsoft SQL Management studio and it worked, in PHP it doesnt. PS 在 Microsoft SQL Management Studio 中尝试了此查询,并且在 PHP 中没有。

change your query to this and you don't need any other variable:将您的查询更改为此,您不需要任何其他变量:

UPDATE sunglasses 
SET sunglassesId = (SELECT MAX(productID) FROM products)
WHERE sunglassesId = 0;

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

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