0

I looked mostly everywhere on Stackoverflow for adding multiple WHERE instances but none of them seem to work.

My Select query:

$result = mysql_query("SELECT * FROM $tableName WHERE user = $user AND column = 1");          //query

I tried IN and some other ways but I dont know why it wont get the column. If I take out the user column it works, but I want it to also restrict to the column as well..

Any help will be appreciated!

Ram
  • 143,282
  • 16
  • 168
  • 197
designtocode
  • 2,215
  • 4
  • 21
  • 35

4 Answers4

2

column is reserved word in mysql. You have to use ` around that kind of column_name and use ' single quotes around string data '$user'

SELECT * FROM $tableName WHERE user = '$user' AND `column` = 1
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
  • 1
    Yea, here comes the right answer, also you might like to add single quotes around user? LOL you did it already, +1 – Mr. Alien Jun 19 '13 at 09:56
  • 1
    Thanks! :) I did for both table fields so `user` = '$user' AND `column` = '1'. And it works, thanks for the tip and explanation! – designtocode Jun 19 '13 at 10:21
0

You need to wrap the username in single-quotes & as mentioned by Yogesh column is a reserved keyword in MySQL. Try this:

user = '$user' AND `column` = 1

So the whole statement becomes:

$result = mysql_query("SELECT * FROM $tableName WHERE user = '$user' AND `column` = 1");

Also, you should be using mysqli or PDO with prepared statements instead.

Precastic
  • 3,742
  • 1
  • 24
  • 29
0

Rewrite your query. Use the below code:

$result = mysql_query("SELECT * FROM `".$tableName."` WHERE user = '".$user."' AND `column` = 1");

And here I am missing that what is column in query is it name of column? And you need to give the value in single quota and table name in ` till mark.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
0

try this one:

$sql = 'SELECT * FROM $tableName WHERE user = "'.$user.'" AND `column` = 1';
skparwal
  • 1,086
  • 6
  • 15