jim.davidson Posted November 14, 2007 Share Posted November 14, 2007 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'] Quote Link to comment https://forums.phpfreaks.com/topic/77374-solved-confused-as-to-what-quote-mark-to-use-in-a-query/ Share on other sites More sharing options...
arianhojat Posted November 14, 2007 Share Posted November 14, 2007 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']}"; Quote Link to comment https://forums.phpfreaks.com/topic/77374-solved-confused-as-to-what-quote-mark-to-use-in-a-query/#findComment-391722 Share on other sites More sharing options...
Barand Posted November 14, 2007 Share Posted November 14, 2007 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 "; Quote Link to comment https://forums.phpfreaks.com/topic/77374-solved-confused-as-to-what-quote-mark-to-use-in-a-query/#findComment-391779 Share on other sites More sharing options...
arianhojat Posted November 14, 2007 Share Posted November 14, 2007 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"; Quote Link to comment https://forums.phpfreaks.com/topic/77374-solved-confused-as-to-what-quote-mark-to-use-in-a-query/#findComment-391807 Share on other sites More sharing options...
jim.davidson Posted November 15, 2007 Author Share Posted November 15, 2007 Barand, Thanks, that was the problem, right in front of my nose. Sometimes you can't see the forest through the trees. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/77374-solved-confused-as-to-what-quote-mark-to-use-in-a-query/#findComment-392148 Share on other sites More sharing options...
jim.davidson Posted November 15, 2007 Author Share Posted November 15, 2007 Barand, Thanks, that was the problem, right in front of my nose. Sometimes you can't see the forest through the trees. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/77374-solved-confused-as-to-what-quote-mark-to-use-in-a-query/#findComment-392149 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.