Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9309755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:56:46+00:00 2026-06-19T00:56:46+00:00

Error in the query: You have an error in your SQL syntax; check the

  • 0

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";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:56:47+00:00Added an answer on June 19, 2026 at 12:56 am

    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:

    $query = "CALL my_stored_procedure(arg1, arg2, ...)";
    

    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

    IF EXISTS(SELECT id FROM foo WHERE id = 5)
        UPDATE foo SET bar = 7
    ELSE
        INSERT INTO foo (bar) VALUES (7)
    GO
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

mysql query - error: You have an error in your SQL syntax; check the
I got error message You have an error in your SQL syntax; check the
I still encounter errors. Error: You have an error in your SQL syntax; check
Let's say that I have an error in a php/mysql query : $query =
I have this SQL query that keeps shooting me an error: You have an
Hey guys getting this syntax error on my sql query: '#1064 - You have
Please help me to find out error in my SQL query. I have created
This is stored procedure that worked on earlier version of mysql. Now, I have
is there something that I'm missing? Thanks :) Error SQL query: ALTER TABLE `venues`
This is the SQL error: Error: 1064 - You have an error IN your

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.