Jump to content

php and mysql how to use variables in queries ?


felix_alves

Recommended Posts

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 = $age

but 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!
Link to comment
Share on other sites

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 :

<?php
session_start(); 
if(!isset($_SESSION['locc']))
{
$_SESSION['locc'] = "New York";
 
}
?>

any idea ? :O

thanks again!
Link to comment
Share on other sites

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';
Link to comment
Share on other sites

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 43

Warning: 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 43


Thank you in advance
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.