Al12
Al12

Reputation: 63

Simple query issue, where clause

I am trying to get a query work! I have this query right here:

$data = mysql_query( "SELECT date, time,location,type_of_payment,
        basket_information, user_id FROM retailer")

I have this variable also:

$getuser[0]['user_id']

I need to set a clause, but i have difficulties in the way of writing the query. Can someone help me editing it please?

$data = mysql_query( "SELECT date, time,location,type_of_payment,
        basket_information, user_id FROM retailer 
        WHERE user_id = $getuser[0]['user_id']")

Thanks..

Upvotes: 1

Views: 76

Answers (3)

Gordon Bailey
Gordon Bailey

Reputation: 3911

$data = mysql_query( "SELECT date, time,location,type_of_payment,
                      basket_information, user_id FROM retailer 
                      WHERE user_id=".$getuser[0]['user_id'])

And, you should really use PDO or mysqli instead of mysql.

Upvotes: 2

dplanet
dplanet

Reputation: 5403

You can't read an array in the SQL code, before you execute the code you could do:

$user = $getuser[0]['user_id'];
$data = mysql_query( "SELECT date, time,location,type_of_payment,
                      basket_information, user_id FROM retailer 
                      WHERE user_id = $user");

Upvotes: 1

Marc B
Marc B

Reputation: 360562

You're running into a PHP parser glitch:

$data = mysql_query( "SELECT [..snip..]  WHERE user_id=$getuser[0]['user_id']")

you're trying to insert a multidimensional array (strike #1) into a double-quoted string, while using quoted array keys (strike #2). PHP's parser isn't greedy, and will not "see" the ['user_id'] as part of the array reference. That's why there's {}. Plus, quoting keys in array references in double-quoted strings will produce a warning, so... try this instead:

$data = "....... WHERE user_id={$getuser[0]['user_id']}")
                               ^---                   ^---

Upvotes: 2

Related Questions