SQL Query Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

SQL Query Error

Post by mohson »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Can anyone see anything word with thie query, I keep getting 
"Error in query: You have an error in your SQL syntax near 'WHERE....

Code: Select all

foreach($HTTP_POST_VARS as $varname => $value)
        $formVars[$varname]=$value;

$query = "SELECT 
		 
o.org_id,o.web_url,
f.salutation,f.name,f.surname,f.organisation,
f.email,f.address,f.address1,
f.telephonefax,f.mobile,f.mscints,
f.mscactive,f.otheractivities,f.gradjobs,
f.ugproj,f.pdev,f.bcsmem,f.bcspds,f.teach,
f.acconsult,f.person_id,f.org_id 
	
FROM feedbackcontacts f LEFT JOIN organisations o 
ON o.org_id = f.org_id
ORDER BY name ASC 

WHERE name LIKE '$formVars[name]%'";


$result = mysql_query($query);

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

The ORDER clause needs to be after the WHERE clause.

Here's the proper clause order for a SELECT statement in MySQL: http://dev.mysql.com/doc/refman/5.0/en/select.html
Last edited by aaronhall on Mon Dec 11, 2006 8:15 am, edited 1 time in total.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

Would you need to specify where name comes from for ORDER BY and WHERE?

Code: Select all

ORDER BY f.name ASC 

WHERE f.name LIKE '$formVars[name]%'";
EDIT obviously not from correct answer posted below at same time!!!!
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Also, the second error you're going to get is that "name" is ambiguous - it needs to be qualified with a table name, like 'f.name' or 'o.name'
Post Reply