Reputation: 1388
I keep getting this error and I have tried putting quotes around the actual array, periods, nothing has worked.
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING on line 8
$id = $_GET['id'];
$record = "SELECT `Name`, `Type`, `Size`, `Content` FROM election_candidates WHERE `ID` = '$id'";
$result = $db->query($record);
foreach ($result as $results) {
header("Content-length: $results['Size']"); <---Line 8
header("Content-type: $results['Type']");
header("Content-Disposition: attachment; filename=$results['Name']");
// echo $results['Size'];
echo $results['Content'];
}
exit();
Upvotes: 0
Views: 63
Reputation: 29424
Try the complex (curly) syntax, symple syntax or the concatenation operator:
// Added isset() and intval()
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
// Removed quotes from 'id'=...
$record = "SELECT `Name`, `Type`, `Size`, `Content` FROM election_candidates WHERE `ID` = $id";
$result = $db->query($record);
foreach ($result as $results) {
// complex (curly) syntax
header("Content-length: {$results['Size']}");
// simple syntax (without quotes)
header("Content-type: $results[Type]");
// using concatenation operator
header('Content-Disposition: attachment; filename='.$results['Name']);
// echo $results['Size'];
echo $results['Content'];
}
exit();
In addition you should escape $_GET['id']
and you don't need quotes for an integer in MySQL. I've corrected these two "problems" in the above code.
You could also add LIMIT 1
to the SQL query and remove the foreach
loop ;)
Upvotes: 1
Reputation: 29886
Since the key is literal string, you can remove the internal quotes:
header("Content-length: $results[Size]");
See the examples in the simple syntax section.
Upvotes: 1
Reputation: 738
Since you're nesting array accessors within a string, I think you want this syntax:
"Content-length: {$results['Size']}"
Upvotes: 0
Reputation: 265251
You cannot use array subscripts inside interpolated strings. You have two options:
'Content-Length: ' . $results['Size']
"Content-Length: {$results['Size']}"
Upvotes: 0
Reputation: 51807
instead of
header("Content-length: $results['Size']");
you'll have to write
header("Content-length: ".$results['Size']);
or
header("Content-length: {$results['Size']}");
you'll have to do the same in line 9 and 10
Upvotes: 0