jandante@telenet.be Posted December 28, 2008 Share Posted December 28, 2008 Hi, You probably had a lot of these questions, but after googling and searching fora i couldn't find the answer to the following. $where = $_POST['where']; $query_getAccount="SELECT name, address FROM accounts WHERE id =".mysql_result($result_getAccountID, $i, "account_id")."AND postalcode=".$where; $result_getAccount= mysql_query($query_getAccount) or die(mysql_error()); I get the following error: 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 'postalcode=2525' at line 1 1. I know the first half of the query is working, because if I comment everything after AND with AND included i get fine results 2. If i run the query like here under in phpMyAdmin I get result SELECT name, address FROM accounts WHERE id = 1 AND postalcode = 2525 3. When i use the above query hard coded in my application I also get result, but I really can't find how I misplaced the variable $where. Thanks in advance. Edit: added code blocks Quote Link to comment https://forums.phpfreaks.com/topic/138637-solved-error-in-mysql-syntax/ Share on other sites More sharing options...
carrotcake1029 Posted December 28, 2008 Share Posted December 28, 2008 The source of your error is in your query. $query_getAccount="SELECT name, address FROM accounts WHERE id=".mysql_result($result_getAccountID, $i, "account_id")."AND postalcode=".$where; 1st Error: You should probably use apostrophe's instead of quotations there. 2nd Error: You need an extra space. Quote Link to comment https://forums.phpfreaks.com/topic/138637-solved-error-in-mysql-syntax/#findComment-724854 Share on other sites More sharing options...
MadTechie Posted December 28, 2008 Share Posted December 28, 2008 Can you try this and post any errors your getting.. Note i added a space before the AND this should also tell us what ID is set to an $i <?php $where = $_POST['where']; $ID = mysql_result($result_getAccountID, $i, "account_id"); $query_getAccount="SELECT name, address FROM accounts WHERE id =$ID AND postalcode=$where"; $result_getAccount= mysql_query($query_getAccount) or die($query_getAccount." I = $i<br> ".mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/138637-solved-error-in-mysql-syntax/#findComment-724855 Share on other sites More sharing options...
jandante@telenet.be Posted December 28, 2008 Author Share Posted December 28, 2008 Thanks a lot. You're posts helped me. I didn't know spaces where needed in sql query but if you come to think about it I made this: <code>SELECT name, address FROM accounts WHERE id =1AND postalcode=2525</code> So SQL probably looked for an id that equals 1AND. Also thanks for the hint on first setting the variables and use those in the query, might be easier to debug. If anyone knows how to put this one on solved, go ahead. Quote Link to comment https://forums.phpfreaks.com/topic/138637-solved-error-in-mysql-syntax/#findComment-724858 Share on other sites More sharing options...
MadTechie Posted December 28, 2008 Share Posted December 28, 2008 Topic Solved button is bottom left (i think) Quote Link to comment https://forums.phpfreaks.com/topic/138637-solved-error-in-mysql-syntax/#findComment-724862 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.