I am dynamically creating a HTML from a selection made from a dropdown box on the event field in my databse. It works perfectly with a simple SQL query $selectStmt = $con->prepare('SELECT * FROM men WHERE event like :start');
. I do however want to run a complex query but then get an
Parse error: syntax error, unexpected '',surname) AS '' (T_CONSTANT_ENCAPSED_STRING).
I am not sure if it is the query or the code after it, as the on the line number of the long query. The query works perfectly in other scripts that are not dynamically generated. Any help please.
My code:
<?php
$dsn = 'mysql:host=localhost;dbname=****';
$username = '****';
$password = '****';
try {
$con = new PDO($dsn, $username, $password);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $ex) {
echo 'Not Connected '.$ex->getMessage();
}
$tableContent = '';
$start = '';
$selectStmt = $con->prepare('SELECT * FROM men');
$selectStmt->execute();
$users = $selectStmt->fetchAll();
foreach ($users as $user) {
$tableContent = $tableContent.'<tr>'.
'<td>'.$user['id'].'</td>'
.'<td>'.$user['name'].'</td>'
.'<td>'.$user['surname'].'</td>'
.'<td>'.$user['event'].'</td>'
.'<td>'.$user['performance'].'</td>';
}
if(isset($_POST['search']))
{
$start = $_POST['start'];
$tableContent = '';
$selectStmt = $con->prepare('SELECT rank, performance, wind,athlete,dob,prov,pos,place,date FROM (SELECT performance, wind,name,surname,dob,prov,pos,place,date, @curRank := IF(@prevRank = performance, @curRank, @incRank) AS rank, @incRank := @incRank + 1, @prevRank := performance, CONCAT(name,' ',surname) AS 'athlete' FROM men p, ( SELECT @curRank :=0, @prevRank := NULL, @incRank := 1 ) r WHERE event like :start ORDER BY performance) s');
$selectStmt->execute(array(
':start'=>$start.'%'
));
$users = $selectStmt->fetchAll();
foreach ($users as $user){
$tableContent = $tableContent.'<tr>'.
'<td>'.$user['id'].'</td>'
.'<td>'.$user['name'].'</td>'
.'<td>'.$user['surname'].'</td>'
.'<td>'.$user['event'].'</td>'
.'<td>'.$user['performance'].'</td>';
}
}
?>