Current location: Hot Scripts Forums » Programming Languages » PHP » MySQL Query Santax Error


MySQL Query Santax Error

Reply
  #1 (permalink)  
Old 05-25-11, 04:55 PM
jrsmith jrsmith is offline
New Member
 
Join Date: May 2011
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Unhappy MySQL Query Santax Error

I can't for the life of me figure out what I'm doing wrong here.

I had the query string echoed to see what was being sent. Here is the query:

PHP Code:

"SELECT * FROM user WHERE ID='2VJj8Khd1EOKuCXDW7N7c7URA5q9YYTE'" 

Here is the error message I see when I try to run 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 ''2VJj8Khd1EOKuCXDW7N7c7URA5q9YYTE'' at line 1

There is no error connecting to the database. The table "user" exists. The field "ID" is correct. As far as I know the santax of the query is correct.

What else might be going wrong here? Somebody please help me.
Reply With Quote
  #2 (permalink)  
Old 05-25-11, 05:41 PM
kfurlong's Avatar
kfurlong kfurlong is offline
Wannabe Coder
 
Join Date: Oct 2010
Posts: 150
Thanks: 6
Thanked 20 Times in 20 Posts
Entire Code,

Hey, can you post the code? I would like to see whats going on.
I want to see where that string is coming from.
Also what is the data type of the ID field?


Thanks,
Kevin
Reply With Quote
The Following User Says Thank You to kfurlong For This Useful Post:
jrsmith (05-25-11)
  #3 (permalink)  
Old 05-25-11, 06:07 PM
jrsmith jrsmith is offline
New Member
 
Join Date: May 2011
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Hey Kevin

That string is built by a function that creates a random 32 character string that I use for ID's. What i was doing is sending a query to the database to see if a given ID already exists. The ID field is CHAR(32).

I have a function for building SELECT queries. Here is the code:
PHP Code:

//$S is what fields I want selected, $T is what table to select from, $C is the conditions or "WHERE" statements, and $O is the "ORDER BY" statement

public function Select_Query($S,$T,$C,$O){ 
    
$S filter_var($SFILTER_SANITIZE_STRING);
    if(!
$S){die("Select Query Error: Selection Failed Validation");}
    else{if(
$S == "ALL"){$S "*";}$T filter_var($TFILTER_SANITIZE_STRING);
        if(!
$T){die("Select Query Error: Table Failed Validation");}
        else{
$C filter_var($CFILTER_SANITIZE_STRING);
            if(!
$C){die("Select Query Error: Condition Failed Validation");}
            else{if(
$C == "NULL"){$C "";}$O filter_var($OFILTER_SANITIZE_STRING);
                if(!
$O){die("Select Query Error: Order Failed Validation");}
                else{if(
$O == "NULL"){$O "";}
                    
$Q "SELECT ".$S." FROM ".$T//This is where the Query string is built
                    
if($C != ""){$Q .= " WHERE ".$C;}
                    if(
$O != ""){$Q .= " ORDER BY ".$O;}
                    echo 
$Q."<br><br>"// This is where I had the Query string echoed to see what was being sent
                    
$R self::SecQuery($Q); //this is the function that actually sends the query
                    
return $R;
                }
            }
        }
    }

The function that sends the query is below:

PHP Code:

private function SecQuery($Q){$R $this->cxn->query($Q) or die($this->cxn->error);} 

All of the code above is part of a class that I use to handle interaction with the database.

Any tips you can provide would be greatly helpful.
Reply With Quote
  #4 (permalink)  
Old 05-25-11, 06:15 PM
kfurlong's Avatar
kfurlong kfurlong is offline
Wannabe Coder
 
Join Date: Oct 2010
Posts: 150
Thanks: 6
Thanked 20 Times in 20 Posts
Try adding ticks

Try adding tick marks
SELECT * FROM `users` WHERE `ID`='YOUR STRING'

if that doesnt work, echo it again and run it through like PhpMyAdmin. Thats will tell you if its the way your set up the query, like in your code, or your query itself. It doesnt look like there is anything wrong with the query. However, you are setting up your columns and table with variables. which shouldnt be a problem , but I would add the ticks just to rule it out.
Reply With Quote
The Following User Says Thank You to kfurlong For This Useful Post:
jrsmith (05-25-11)
  #5 (permalink)  
Old 05-25-11, 06:23 PM
jrsmith jrsmith is offline
New Member
 
Join Date: May 2011
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Hey Kevin,

Thanks for the tip. I tried adding the tick marks but I still got the same error message. I also ran the query through phpMyAdmin and the query works fine. I'm stumped here.
Reply With Quote
  #6 (permalink)  
Old 05-25-11, 06:44 PM
kfurlong's Avatar
kfurlong kfurlong is offline
Wannabe Coder
 
Join Date: Oct 2010
Posts: 150
Thanks: 6
Thanked 20 Times in 20 Posts
Call

Can you post the calling of the function. I am getting stumped lol
Reply With Quote
  #7 (permalink)  
Old 05-26-11, 02:47 AM
stoopkid stoopkid is offline
New Member
 
Join Date: May 2011
Location: chicago, il
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
If you're positive that your SQL is fine, then your PHP is broken shortly before you call the query. Check to make sure that your SQL Query is getting filled from the right $result.
PHP Code:

$R self::SecQuery($Q); //this is the function that actually sends the query
                    
return $R
Is your $R getting reset? That MySQL error can occur when something is left emptied, or if another column needs to be filled in.
Reply With Quote
  #8 (permalink)  
Old 05-26-11, 11:50 AM
sqlhjalp sqlhjalp is offline
New Member
 
Join Date: Apr 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,

I wanted to ask a somewhat related question.

Are you using this ID as your primary key on the table ? You could be wasting memory and it could take a hit with performance versus an integer id. They are stored in memory better.

Yes having this as a reference to get an id is fine. Example being you reference this via url and hide real id. But once internal to code and sql use the integer id. Should be faster. More reading on this is in the High_Performance_MySQL book.

Could you also make this easier on you and let MySQL generate your code ?

select ENCRYPT('foobar@asdf.com','secretpassword'); Make sure table can handle a 13 char hash.


Otherwise that sql runs on my test db.
Reply With Quote
Reply

Bookmarks

Tags
error, mysql, php, query, santax


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
mysql query error divya-paru PHP 1 12-13-10 05:44 PM
C++ and MSSQL tutorials? scott2500uk C/C++ 8 05-11-09 02:33 AM
Php based mysql query problome prasanth PHP 3 04-16-08 05:54 PM
MySQL Error umarrana PHP 1 10-19-07 06:35 AM
single quote "'" error in mysql query by php sjems PHP 4 09-27-05 08:36 AM


All times are GMT -5. The time now is 08:48 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.