PHP在PDO查询结果中循环

PHP在PDO查询结果中循环,php,oop,pdo,foreach,Php,Oop,Pdo,Foreach,我是OOP和PDO的新手。提前感谢您的帮助 我在将SQL查询结果循环到页面时遇到问题 这是我的notes.inc.php $note->tulosta_notet('notes'); 在课堂笔记中我有这个方法 public function tulosta_notet($table) { $sql ="SELECT * FROM " . $table . ""; $result = $this->_db->query($sql);

我是OOP和PDO的新手。提前感谢您的帮助

我在将SQL查询结果循环到页面时遇到问题

这是我的notes.inc.php

$note->tulosta_notet('notes');
在课堂笔记中我有这个方法

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/>";
        }
    }
我做错了什么

如果我更愿意在notes.inc.php中执行循环,那么我应该从tulosta_notet()返回什么来让它工作呢? 谢谢

-E

这里是var\u转储($this->\u db);退出

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) }
类DB中的query()

 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;
    } 
公共函数查询($sql,$params=array()){
$this->_error=false;
如果($this->_query=$this->_pdo->prepare($sql)){
$x=1;
如果(计数($params)){
foreach($params作为$param){
//echo$param;
$this->_query->bindValue($x,$param);
$x++;
//echo$x.“
”; } } 如果($this->\u query->execute()){ 呼应“成功”; $this->\u results=$this->\u query->fetchAll(PDO::FETCH\u OBJ); $this->_count=$this->_query->rowCount(); }否则{ $this->_error=true; } } 退还$this; }
在“查询”方法中,使用流畅的界面(return
$this
返回当前对象)。因此,当您打印时,会打印一个对象

修改您的查询方法或使用如下获取函数:

public function tulosta_notet($table) {   

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

    for ($i = 0; $i < count($res); $i++) {
        print $res[$i]["id"] . "-" . $res[$i]["note_text"] ."<br/>";
    }
}
公共功能tulosta_notet($table){
$sql=“从“$table”中选择*;
$stmt=$this->\u db->prepare($sql);
$res=$stmt->execute();
对于($i=0;$i”;
}
}

this->\u db是什么?它是“PDO”类的对象还是某种包装器?您试图将$results作为数组循环,但它不是数组。它是一个包含_查询、_错误和_结果参数的对象。在执行查询之后,您可能需要一些fetch/fetchAll调用。很难推荐代码,因为您似乎没有使用本机pdo类,请查阅文档,查找您正在使用的任何db类。将var_dump结果添加到原始消息中。DB是一个单独的类,其中有query()。我将把它的内部添加到原始消息tooSee query();我在原始信息中添加的内容。我在里面执行execute()和prepare()$this->_results=$this->_query->fetchAll(PDO::FETCH_OBJ);我看到了,不是你没有使用这些命令,我想是你回应了整个对象。在返回整个对象的函数处返回$this。这称为“流畅的界面”。我无法检查您的代码,但如果您有错误,您必须以某种方式修改您的“查询”方法谢谢Nikos的帮助。实际上,我又提出了两种方法来解决这个问题。
public function tulosta_notet($table) {   

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

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