Reputation: 109
I want to make query to mysql, but when i execute it at end of the page i have needless text. How can i delete it?
this is the code:
try {
//create PDO connection
$db = new PDO("mysql:host=".DBHOST.";port=3306;dbname=".DBNAME, DBUSER, DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
//show error
echo '<p class="bg-danger">'.$e->getMessage().'</p>';
exit;
}
$stmt = $db->query('SELECT * FROM temperature');
while ( $row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['timestamp'] . "\t" . $row['humidity'] . "\n";
}
?>
This is the output:
2015-06-16 13:07:47 89 2015-06-16 13:08:46 0 2015-06-16 13:08:56 86 2015-06-16 13:09:06 86 2015-06-16 13:09:16 86 2015-06-16 13:09:27 86 query('SELECT * FROM
temperature
'); while ( $row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo $row['timestamp'] . "\t" . $row['humidity'] . "\n"; } } } */?>
Upvotes: 0
Views: 102
Reputation: 37065
Here's a fun and different way you could write that loop:
foreach($db->query('SELECT * FROM temperature') as $row) {
echo "{$row['timestamp']}\t{$row['humidity']}\n";
}
See if doing it that way gets the same issue.
Upvotes: 0
Reputation: 2761
I can't see it in the code code you posted but you have a typo in the original: "$stmt = $db->query" has an extra question mark "$stmt = $db-?>query". Or maybe you have commented out an area with html tags and php comments which are incorrectly nested?
Either way, it will break out of PHP parsing and produce the results you describe.
Upvotes: 2