views:

75

answers:

2

I am getting this error: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\wamp\www\vacation_showcase\admin\participants_edit_list.php on line 17

This is my sql statement:

$sql = 'SELECT substring_index(description,' ',100) as preview, name, extra_line, link FROM participants
ORDER BY name 
ASC';

I dont know why it's not working, because when I run the SQL statement in the SQL tab in phpMyAdmin, it works great and shows me the results I want to see.

+3  A: 

Syntax error. You have a string formed with single quotes but you have single quotes as part of the string. You either need to use a double quoted string like

$sql = "SELECT substring_index(description,' ',100) as preview, name, extra_line, link FROM participants ORDER BY name  ASC";

or, you need to escape the single quotes

$sql = 'SELECT substring_index(description,\' \',100) as preview, name, extra_line, link FROM participants ORDER BY name ASC';
Justin Johnson
thanks for the quick reply!
zeckdude
Sure thing. It was all chance ;)
Justin Johnson
+2  A: 

Because you've got single quotes in your string and you've also got them in the query. You either need to use a different string syntax or escape your single quotes in the string.

I prefer to use the heredoc syntax for this kind of thing:

$sql = <<<END
SELECT substring_index(description,' ',100) as preview, name, extra_line, link
FROM participants
ORDER BY name ASC
END;

It's a lot more readable than having escaped quotes and you can cut and paste it into a DB program to test the query without removing backslashes.

cletus
thanks for the quick reply!
zeckdude