0

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"; } } } */?>

4
  • what is the output you desire ? Commented Jun 16, 2015 at 15:54
  • "i have needless text" - Define "needless". Voted to close as unclear until we do know what you want, and written in your question and NOT in comments. We read code, not minds. ;-) Commented Jun 16, 2015 at 16:03
  • Blind stab at this, your ouput doesn't make sense. You sure you're using .php extension? Is your computer setup as a webserver with both PHP/MySQL installed? Your question is unclear. another thing... you do have an opening <?php tag, right? Gawd, I hate guessing, unless it's "charades". Commented Jun 16, 2015 at 16:09
  • well, this question seems to have fallen dead/stale. Moving on; good luck. Commented Jun 16, 2015 at 16:37

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.