I'm using a WHERE condition in a SELECT clause of my query thats looking for 3
parameters. When I plug the WHERE condition with strings it works:
[i]
mysql_connect(localhost, $username, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
// Get a specific result from the "example" table
$result = mysql_query("SELECT * FROM allMeetings WHERE [b]city = 'Boise,
Idaho' AND day = 'Monday' AND time = '7:00a[/b]'")
or die(mysql_error());
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row
//echo $row['city']." - ".$row['group']. "<br />";
echo $row['city']." - ".$row['day']." - ".$row['time']." -
".$row['group']." - ".$row['address']." - ".$row['flag1']." - ".$row['flag2'].
" - ".$row['flag3'].'<br />';
}[/i]
Now the fun part. If I replace the strings with variables it errors out:
[i]
[b]$fcity = "Boise, Idaho";
$fday = "Monday";
$ftime = "7:00a";[/b]
mysql_connect(localhost, $username, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
// Get a specific result from the "example" table
$result = mysql_query("SELECT * FROM allMeetings WHERE [b]day=$fday AND
city=$fcity AND time=$ftime [/b]") or die(mysql_error());
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row
//echo $row['city']." - ".$row['group']. "<br />";
echo $row['city']." - ".$row['day']." - ".$row['time']." -
".$row['group']." - ".$row['address']." - ".$row['flag1']." - ".$row['flag2'].
" - ".$row['flag3'].'<br />';
}[/i]
The above returns the following:
[b]You have an error in your SQL syntax near ' Idaho AND time=7:00a ' at line
1[/b]
I'm sure its something simple and stupid so if anyone can enlighten me I would
be much obliged.
Gorka Ludlow - 22 Jun 2007 15:07 GMT
Why are you asking this in a flash forum?
"You have an error in your SQL syntax"
Anyways, your variables need quotes for the query, try this instead ( var = "
\ ' value ' \ " ; variable = double quote, slash, single quote, value, single
quote, slash, double quote):
$fcity = "\'Boise, Idaho\'";
$fday = "\'Monday\'";
$ftime = "\'7:00a\'";
Cheers
Gorka