Page 1 of 1

Problem using variables in MySql script

Posted: Wed Feb 06, 2008 5:04 pm
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.

Re: Problem using variables in MySql script

Posted: Wed Feb 06, 2008 5:28 pm
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'";

Re: Problem using variables in MySql script

Posted: Wed Feb 06, 2008 5:38 pm
by timski72
Thanks, that sorted it :-)

Re: Problem using variables in MySql script

Posted: Thu Feb 07, 2008 1:09 am
by Mordred
Also use mysql_real_escape_string()