Skip to content
Advertisement

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). Is there anything I can do and after all can I even use DECLARE in PHP as a mySQLi statement.

$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. Hope that made sense.

P.S tried this query in Microsoft SQL Management studio and it worked, in PHP it doesnt.

Advertisement

Answer

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;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement