I am wanting to select between two options for an SQL query from user supplied parameters.
The code is below. I’ve tried using the &(ampersand) in front of the variable but to no avail. The query works if I manually set $select to either $full or $summary, so I know that the SQL query is correct.
data.php
<?php
function chartGrid($stdate, $enddate, $select ){
include 'login.php';
include 'grid.php';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=charts", $username, $password);
$sql = $select;
$stmt = $dbh->prepare($sql);
$stmt->execute();
foreach($stmt as $row) {
$result = json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
print($result);
exit;
}
/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
chartGrid('2015-10-12', '2016-10-11', $full );
?>
grid.php
<?php
$full = "SQL SELECT full details from $stdate to $enddate";
$summary = "SQL SELECT summary details from $stdate to $enddate";
?>
What am I missing?
2
Answers