Problem using variables in MySql script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
timski72
Forum Newbie
Posts: 15
Joined: Sun Jan 13, 2008 6:19 am

Problem using variables in MySql script

Post by timski72 »

Hello there,

I am trying to use variables in a MySql query but can't get it to work. Here is my query. I've printed some of the variables so that I know there are the correct values in my variables.

Code: Select all

 
$selectedVerb = $_POST['selectedVerb'];
$selectedTense = $_POST['selectedTense'];
 
echo "<br>$selectedVerb</br>";
echo "<br>$selectedTense</br>";
 
$query = 'select * from conjugats where verbID = $selectedVerb and tenseID = $selectedTense';
echo "<br>$query</br>";
 
I am getting the following results:
4
2
select * from conjugats where verbID = "$selectedVerb" and tenseID = "$selectedTense"
No rows found
As you can see the query isn't using the values that are in the variables, but using the variable name. Any ideas how I get to the actual values in the variables?

Thanks,
Tim.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Problem using variables in MySql script

Post by Christopher »

Double quotes will cause variables to be expanded. Single quotes around strings in the SQL itself.

Code: Select all

$query = "select * from conjugats where verbID = '$selectedVerb' and tenseID = '$selectedTense'";
(#10850)
timski72
Forum Newbie
Posts: 15
Joined: Sun Jan 13, 2008 6:19 am

Re: Problem using variables in MySql script

Post by timski72 »

Thanks, that sorted it :-)
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Problem using variables in MySql script

Post by Mordred »

Also use mysql_real_escape_string()
Post Reply