Forum Moderators: coopster

Message Too Old, No Replies

syntax error, unexpected T VARIABLE

Can't get the variable to work

         

shutchens

10:05 pm on Nov 12, 2009 (gmt 0)

10+ Year Member



I have a database that has a field with the year and I'd like for my select code to default to the current year(variable) when doing my query. Below is the code I'm trying to use....

// 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.

bkeep

11:53 pm on Nov 12, 2009 (gmt 0)

10+ Year Member



remove the trailing semi-colon }; as a start

shutchens

1:00 am on Nov 13, 2009 (gmt 0)

10+ Year Member



If you are referring to the ; on

// pull current year as default
$currentyear = date("Y");

I then get the error parse error on that line.

rocknbil

1:01 am on Nov 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



haha . . . looked at it for almost a full minute before I saw the one you were referring to. (the one at the end of the closing PHP block.) Well, that's not the cause of his/her problem, and it will sometimes execute without error with those - done it myself in both PHP and Perl. :-)

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.