Jump to content

[SOLVED] Confused as to what quote mark to use in a query


jim.davidson

Recommended Posts

I have a session variable, $_SESSION['last_name'] that contains a last name.  I want to use that variable in my query. I've tried it with single quote, double quotes and no quotes.  Something just not sinking through my thick head.  Can someone help me?  Here's the query. 

Thanks

 

SELECT * FROM patient ORDER BY patient.last_name, patient.first_name, patient.mi WHERE patient.last_name = $_SESSION['last_name']

which quotes?

doesnt matter, all same

 

$query = 'SELECT * FROM patient ORDER BY patient.last_name, patient.first_name, patient.mi WHERE patient.last_name ='. $_SESSION['last_name'];

$query = "SELECT * FROM patient ORDER BY patient.last_name, patient.first_name, patient.mi WHERE patient.last_name =".

$_SESSION['last_name'];

$query = 'SELECT * FROM patient ORDER BY patient.last_name, patient.first_name, patient.mi WHERE patient.last_name ='. $_SESSION["last_name"];

$query = "SELECT * FROM patient ORDER BY patient.last_name, patient.first_name, patient.mi WHERE patient.last_name =". $_SESSION["last_name"];

 

prob could also do all within 1 string

$query = "SELECT * FROM patient ORDER BY patient.last_name, patient.first_name, patient.mi WHERE patient.last_name = {$_SESSION['last_name']}";

 

which quotes?

doesnt matter, all same

 

No, they aren't.

 

Try doing your final example with the " and ' swapped around.

 

Also, you forgot the quotes needed around the session var as lastname is almost certinly a string value.

 

$query = "SELECT * FROM patient 
             WHERE patient.last_name = '{$_SESSION['last_name']}'
             ORDER BY patient.last_name, patient.first_name, patient.mi ";

whoops, forgot to say anything within double qoutes is interpreted hence why could include variable inside qoutes in my last example.

and yes i did forget to place examples in qoutes.

and u seem to found his mistake too... he is putting ORDER BY before the WHERE clause.

 

so jim any of these should work:

$query = 'SELECT * FROM patient WHERE patient.last_name ="'. $_SESSION['last_name'] .'" ORDER BY patient.last_name, patient.first_name, patient.mi';

$query = "SELECT * FROM patient WHERE patient.last_name ='".

$_SESSION['last_name'] ."' ORDER BY patient.last_name, patient.first_name, patient.mi ";

$query = 'SELECT * FROM patient WHERE patient.last_name ="'. $_SESSION["last_name"] .'" ORDER BY patient.last_name, patient.first_name, patient.mi ';

$query = "SELECT * FROM patient WHERE patient.last_name =". $_SESSION["last_name"] ."' ORDER BY patient.last_name, patient.first_name, patient.mi ";

 

or that last example where any vars in the double qouted string are interpreted.

$query = "SELECT * FROM patient WHERE patient.last_name = {$_SESSION['last_name']} ORDER BY patient.last_name, patient.first_name, patient.mi";

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.