Forum Moderators: coopster
// pull current year as default
$currentyear = date("Y");
//Query to pull the sermons for 1st Quater
$query1 = 'SELECT * FROM `sermons` WHERE `year`= '$currentyear' AND `quarter` LIKE CONVERT(_utf8 \'1\' USING latin1) COLLATE latin1_swedish_ci ORDER BY `audio_file_name` ASC';
$result1 = mysql_query($query1);
Then this is how I'm displaying the results.
<?php while ($row = mysql_fetch_array($result1,MYSQL_ASSOC))
{
//ehco the results on the screen
echo"<td valign=top align=left >{$row['date']}</td><td align=left> {$row['speaker']}</td><td><a href='../audio/{$row['audio_file_name']}'>Play</a> </td>";
echo"</tr>";
};
?>
If the variable year is hard coded (2009) query works with no problem, otherwise I'm getting this: Parse error: syntax error, unexpected T_VARIABLE in /home3/boonvill/public_html/audio3.php on line 19
New to this and would appreciate any help possible.
Welcome aboard shutchens! There are two problems here.
In PHP (and most other languages) a double-quoted variable will interpolate, exchanging it's value for the variable.
$var = 'test';
echo "$test"; // echoes test
single quoting will not interpolate so it is a string literal.
$echo '$test'; // echoes $test
Your select statement is single quoted. There are two fixes for this.
Concatenate:
$query = 'select from table where value="' . $val . '"';
or swap quoting:
$query = "select from table where value='$val'";
Second, you have single quotes inside single quotes, causing PHP to think the string has ended when it hasn't. The two ways around this are a combination of the above or escape quotes within the string delimiter:
$query1 = 'SELECT * FROM `sermons` WHERE `year`= \'' . $currentyear . '\' AND `quarter` LIKE CONVERT(_utf8 \'1\' USING latin1) COLLATE latin1_swedish_ci ORDER BY `audio_file_name` ASC';
$query1 = "SELECT * FROM `sermons` WHERE `year`= '$currentyear' AND `quarter` LIKE CONVERT(_utf8 '1' USING latin1) COLLATE latin1_swedish_ci ORDER BY `audio_file_name` ASC";
The confusion comes in with the second one, "I see single unescaped quotes in the string so the variable won't interpolate." But those single quotes are just ordinary characters, the actual string is delimited by the double quotes.