Emmi Lehto Emmi Lehto - 5 months ago 20
PHP Question

PHP Looping through PDO query results

I am new to OOP and PDO. Thanks in advance for any help!

I'm having a problem with looping out my SQL query results to the page.

Here is my notes.inc.php

$note->tulosta_notet('notes');


in class Note I have the method

public function tulosta_notet($table) {
$sql ="SELECT * FROM " . $table . "";
$result = $this->_db->query($sql);

echo "<br />";
print_r($result);

foreach ($result as $row) {
print $row["id"] . "-" . $row["note_text"] ."<br/>";
}
}


I am getting the right results with the
print_r($result);


So the query is correct, I just can't get the rows print right way to the page.

Here is the output of print_r($result);

DB Object ( [_pdo:DB:private] => PDO Object ( )
[_query:DB:private] => PDOStatement Object ( [queryString] => SELECT * FROM notes )
[_error:DB:private] => [_results:DB:private] => Array (
[0] => stdClass Object (
[id] => 1
[created] => 2015-03-08 13:50:43 [edited] => 2015-03-08 14:50:43
[note_text] => hei hei moi moi hei hei hei [
user_id] => 1 )

[1] => stdClass Object (
[id] => 2
[created] => 2015-03-08 14:23:55
[edited] => 2015-03-08 15:23:55
[note_text] => text text text text text text text text text text text text text text text text text text
[user_id] => 1 )
) [_count:DB:private] => 2 )


What am I doing wrong?

Should I rather do the looping in notes.inc.php, what should I return from the tulosta_notet() to get it work ?
Thanks

-E

here is the var_dump($this->_db);exit;

object(DB)#3 (5) {
["_pdo":"DB":private]=> object(PDO)#4 (0) { }
["_query":"DB":private]=> object(PDOStatement)#9 (1) {
["queryString"]=> string(28) "SELECT * FROM notes"
}
["_error":"DB":private]=> bool(false)
["_results":"DB":private]=> array(2) {
[0]=> object(stdClass)#5 (5) {
["id"]=> string(1) "1"
["created"]=> string(19) "2015-03-08 13:50:43"
["edited"]=> string(19) "2015-03-08 14:50:43"
["note_text"]=> string(27) "hei hei moi moi hei hei hei"
["user_id"]=> string(1) "1"
}

[1]=> object(stdClass)#10 (5) {
["id"]=> string(1) "2"
["created"]=> string(19) "2015-03-08 14:23:55"
["edited"]=> string(19) "2015-03-08 15:23:55"
["note_text"]=> string(90) "text text text text text text text text text text text text text text text text text text " ["user_id"]=> string(1) "1"
}
} ["_count":"DB":private]=> int(2) }


query() in class DB

public function query($sql, $params = array()) {
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql)) {

$x = 1;
if(count($params)) {
foreach($params as $param) {
// echo $param;
$this->_query->bindValue($x, $param);
$x++;
// echo $x."<br>";
}
}

if($this->_query->execute()) {
echo "Success";
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
} else {
$this->_error = true;
}
}

return $this;
}

Answer

in your 'query' method you use fluent interface (return $this returns the current object). As a result when you print_r, an object is printed. Modify your query method or use a fetch function likewise: public function tulosta_notet($table) {

        $sql ="SELECT * FROM " . $table . "";
        $result =  $this->_db->prepare($sql);
        $res=   $result->execute();

        for ($i=0;$i<count($res);$i++) {
        print $res[$i]["id"] . "-" . $res[$i]["note_text"] ."<br/>";
        }
}
Comments