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']

Link to comment
Share on other sites

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']}";

 

Link to comment
Share on other sites

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 ";

Link to comment
Share on other sites

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";

 

Link to comment
Share on other sites

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.