Josef
Josef

Reputation: 2726

Same query works different

I made php script that updates database:

<?php
include 'config.php';

$PauseID = "2";
$ProductionID = "1411979966";

$sql = "SET @max = (SELECT MAX(Id) FROM tblproductionbreaks); UPDATE tblproductionbreaks SET IDPause = '$PauseID' WHERE ProductionID = '$ProductionID' AND Id = @max;";     

mysql_query($sql) or die(mysql_error());

mysql_close($connect);

?>

While executing this script it returns error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE tblproductionbreaks SET IDPause = '2' WHERE ProductionID = '1411979966' A' at line 1

But if I try same update query execute by command line it works

SET @max = (SELECT MAX(Id) FROM tblproductionbreaks); UPDATE tblproductionbreaks SET IDPause = '2' WHERE ProductionID = '1411979966' AND Id = @max;

I don't understand how the same thing works differently.

Upvotes: 0

Views: 46

Answers (2)

fancyPants
fancyPants

Reputation: 51888

You can not run multiple queries as one statement with PHP. Try like this:

$sql = "UPDATE tblproductionbreaks SET IDPause = '$PauseID' WHERE ProductionID = '$ProductionID' ORDER BY Id DESC LIMIT 1;";     

You don't need this variable anyway.

Upvotes: 1

Raheel
Raheel

Reputation: 9024

Its is because your PHP interpreter treats your $sql as string. Change your code to

$sql = "UPDATE tblproductionbreaks SET IDPause = '{$PauseID}' WHERE ProductionID = '{$ProductionID}' AND Id = (SELECT MAX(Id) FROM tblproductionbreaks)";

Also you can debug it by echo $sql and see what actually the $sql returning

Upvotes: 0

Related Questions