Error in the query: 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 ‘IF EXISTS(SELECT flag FROM welcomecall WHERE customerID= ‘6767’)
SELECT flag’ at line 1
This query isn’t anywhere near line 1 so I’m not sure what the error is. Could anyone please advise? 🙂
<?php
$db_link = mysql_connect('localhost', 'secrets', '') or die('Could not connect to the database.');
mysql_select_db('thedb', $db_link) or die('Could not find the database table.');
require "/home/directory/public_html/app/Mage.php";
//umask(0);
Mage::app();
Mage::getSingleton('core/session', array('name'=>'frontend'));
$session = Mage::getSingleton('customer/session');
if($session->isLoggedIn()) {
print_r($session);
$customerID = $session->getCustomerId();
print("Customer ID is ". $customerID);
} else {
echo 'Not logged In';
}
$action = $_POST['action'];
switch($action) {
case 'retrieve' : retrieveFlag();break;
case 'postValue1' : postValue();break;
// ...etc...
}
function retrieveFlag(){
global $customerID;
$query="IF EXISTS(SELECT flag FROM welcomecall WHERE customerID= '$customerID')
SELECT flag FROM welcomecall WHERE customerID=" . $customerID . "
ELSE
INSERT INTO welcomecall VALUES (" .$customerID .",0)";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
function postValue(){
$query="IF EXISTS(SELECT flag FROM welcomecall WHERE customerID=" . $customerID . ")
SELECT flag FROM welcomecall WHERE customerID=" . $customerID;
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
if ($result==0){
$query="UPDATE welcomecall SET flag=1 WHERE customerID=" .$customerID. ")";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
else if($result==1){
$query="UPDATE welcomecall SET flag=0 WHERE customerID=" . $customerID. ")";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
}
?>
Also tried the following with an error:
$query="SELECT CASE
WHEN EXISTS(SELECT flag FROM welcomecall WHERE customerID= '$customerID')
THEN SELECT flag FROM welcomecall WHERE customerID=" . $customerID . "
ELSE
INSERT INTO welcomecall VALUES (" .$customerID .",0)
END";
You can’t use IF statements in MySQL outside of routines (stored procedures, functions and routines). You’ll need to either do the work in PHP by issuing multiple queries and looking at results, or you can write a stored procedure to do the work for you inside the database (see: http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html). A third option is to use INSERT…ON DUPLICATE KEY UPDATE (see: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html) or even REPLACE INTO instead of INSERT INTO (see: http://dev.mysql.com/doc/refman/5.0/en/replace.html).
From PHP, you can call stored procedures using this syntax:
Note that a stored procedure can return a resultset like a SELECT statement. In this case, I’d recommend using outbound parameters to return data back to PHP (see: PHP + MySql + Stored Procedures, how do I get access an "out" value? for more info, or http://php.net/manual/en/pdo.prepared-statements.php for PDO).
The other query types behave as expected.
I’ll leave the details of doing it with several queries in PHP as an exercise to the reader, but if you are stuck, I’ll be happy to give additional info.
Also, please please do not use the mysql_* functions. They are deprecated. Use MySQLi or PDO instead.
Further explanation:
SQL Server and probably other databases allow you to use imperative constructs when submitting query batches. In T-SQL, you can say things like
And that’s really nice. MySQL does not allow imperative constructs such as IF statements, CASE statements (not to be confused with CASE expressions — see next paragraph), loops and cursors to be used in plain queries. Instead, they must be contained in a stored procedure, function or trigger. This is a pretty annoying limitation, but there’s nothing you can do about it.
The reason your last query doesn’t work is because you are putting a data modification query inside a SELECT query. You can only embed other SELECT queries as subqueries. The reason for this is that the subqueries are in a place where a scalar or a set is expected, and INSERT returns neither, in addition to modifying the data, which breaks the semantics of SELECT[1]. Think about what you are asking the query to do. You are saying that you want to either return a dataset, or insert some new data depending on some fact about the data in your database. The two options are contradictory. You can either do one or the other, but not both. So MySQL disallows this type of thing.
[1] You can get away with this by putting an INSERT inside a function, which can be called from a SELECT query, but this is just awful and don’t ever do it.