0
$select = "SELECT name FROM table_name WHERE location ='".$loc."' ";
$findname = mysql_query($select) or die(mysql_error());

I keep getting this error! I have tried everything!!!

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 'WHERE location ='Florida'' at line 1

$loc is determined by the following:

<input type="text" name="loc"> in the HTML
$loc = $_POST['loc']; in the PHP
3
  • Seems to me like something is wrong with your $loc, can you show the example of the variable? Seems to me the variable ' is escaping and makes your statement to be SELECT name FROM table_name WHERE ='' (2x single quoted) Florida' Commented Mar 18, 2014 at 7:30
  • The variable is just from a post field where you enter Florida. Simply $location = $_POST['location']; @Sky Commented Mar 18, 2014 at 7:51
  • @user3430837 try using like keyword Commented Mar 18, 2014 at 8:33

5 Answers 5

1

Try this , use mysql_escape_string or mysql_real_escape_string mysql safe string functions

$select = "SELECT `name` FROM `table_name` WHERE `location` ='".mysql_escape_string($loc)."' ";
$findname = mysql_query($select) or die(mysql_error());
Sign up to request clarification or add additional context in comments.

1 Comment

your problem is in table_name make sure you have added correct table name
0
$select = "SELECT name FROM table_name WHERE location ='".mysql_real_escape_string($loc)."' ";
$findname = mysql_query($select) or die(mysql_error());

1 Comment

This gives me: ...right syntax to use near 'WHERE location ='Florida'' … same error @SajithNair
0

Just check for any single quotes or double quotes in the location variable. That might be a problem.

Use str_replace(find,replace,string) to replace single and double quotes in the string.

Example, when you can contain a double quote in the $loc variable.

$select = "SELECT name FROM table_name WHERE location ='Flo"rida' ";

The query will end at Flo.

2 Comments

There is no single quote or double quote in my input string for location.
try using "SELECT table_name.name FROM table_name WHERE table_name.location ='".$loc."' ";
0

use mysql_real_escape_string after WHERE location =

Comments

0

I fixed it. I was using a variable for the table name and was referencing a null value. Thanks for the help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.