查询错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'IF EXISTS(SELECT flag FROM welcomecall WHERE customerID='6767') SELECT flag' 附近使用正确的语法
此查询不在第 1 行附近,所以我不确定错误是什么。有人可以建议吗?:)
<?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());
}
}
?>
还尝试了以下错误:
$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";