felix_alves Posted January 13, 2007 Share Posted January 13, 2007 hi all, im having some difficulties here finding information on this topic on the net.i have a php page and i need to make a query to the database but using one value which is stored in a php variable,for example, $age.now i wanna do this:SELECT * FROM Person WHERE age = $agebut this not so much surprisingly doesnt work...how can i do it ? this is probably an easy question but im a asp.net c# developer where we just put a '?' and than command.addParameter...thanks for your time! Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 13, 2007 Share Posted January 13, 2007 even though its a variable, you still need the single quotes:SELECT * FROM Person WHERE age = '$age' Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 13, 2007 Share Posted January 13, 2007 Ted: not if it's a number, but it's a good idea to do it anyway.felix, what doesn't work? Error, etc? More code? We need more info. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 13, 2007 Share Posted January 13, 2007 nope, i tried it, whatever the variable is, you need a single quote or it will result in syntaxTed Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 13, 2007 Share Posted January 13, 2007 Ted, you're full of something.I have skipped the quotes on numbers. Maybe it's a mysql version thing, but it works for me. However, it's a good idea to use the quotes just in case. Quote Link to comment Share on other sites More sharing options...
felix_alves Posted January 13, 2007 Author Share Posted January 13, 2007 sorry i was away for a bit, thank oyu for your fast reply :) will try it now...i dont know yet...maybe was because im doing it with a session variable, will try and say something, thanks again Quote Link to comment Share on other sites More sharing options...
felix_alves Posted January 13, 2007 Author Share Posted January 13, 2007 ok, here´s what im doing:and the error is in this line :$query="SELECT * FROM Headshot WHERE valid = 1 AND location='$_SESSION['locc']' ORDER BY name ";if i change it to for example:$query="SELECT * FROM Headshot WHERE valid = 1 ORDER BY name "; works fine !also in the begining of my page im doing :<?phpsession_start(); if(!isset($_SESSION['locc'])){ $_SESSION['locc'] = "New York"; }?>any idea ? :Othanks again! Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 13, 2007 Share Posted January 13, 2007 When we ask for an error, we mean the error you see, not just telling us where it is.Change to this: $query="SELECT * FROM Headshot WHERE valid = 1 AND location='".$_SESSION['locc']."' ORDER BY name "; Quote Link to comment Share on other sites More sharing options...
felix_alves Posted January 13, 2007 Author Share Posted January 13, 2007 it worked ! :)thank you! huge!! Quote Link to comment Share on other sites More sharing options...
play_ Posted January 13, 2007 Share Posted January 13, 2007 2c here.For future reference, you might want to know to escape quotes.for example:echo 'Today i'm feeling ok';You have 3 single quotes in there. The first one starts the string.the php parser will see the second single quote (i'm) and think it's the end of the string, and give you a syntax error after that because you have some text after '.You'd have to escape it like this:echo 'Today i\'m feeling ok'; Quote Link to comment Share on other sites More sharing options...
felix_alves Posted January 13, 2007 Author Share Posted January 13, 2007 nice, makes sense! thanks will keep it in mind! Quote Link to comment Share on other sites More sharing options...
felix_alves Posted January 13, 2007 Author Share Posted January 13, 2007 in case you are still checking this post, another issue came up...problems with sessions...<?php if (!session_id()) { session_start(); if(!isset($_SESSION['locc'])) { $_SESSION['locc'] = "New York"; } }?>i have this on my page and gives me an error on line: session_start(); the error (warnings that appear on page...it still loads....but with thos big warnings on top) :Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/actandfi/public_html/headshotTEST.php:14) in /home/actandfi/public_html/headshotTEST.php on line 43Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/actandfi/public_html/headshotTEST.php:14) in /home/actandfi/public_html/headshotTEST.php on line 43Thank you in advance Quote Link to comment Share on other sites More sharing options...
play_ Posted January 13, 2007 Share Posted January 13, 2007 put this at the very top of your page<?phpob_start();?>and put this at the very end<?phpob_end_flush();?> Quote Link to comment 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.