Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/138637-solved-error-in-mysql-syntax/
Share on other sites

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.

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());
?>

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.