PHP 遍历 PDO 查询结果
PHP Looping through PDO query results
我是 OOP 和 PDO 的新手。在此先感谢您的帮助!
我在将 SQL 查询结果循环到页面时遇到问题。
这是我的 notes.inc.php
$note->tulosta_notet('notes');
在class注意我有方法
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/>";
}
}
我用 print_r($result);
得到了正确的结果
所以查询是正确的,我只是无法将行正确打印到页面。
这是 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 )
我做错了什么?
我是否应该在 notes.inc.php 中进行循环,我应该从 tulosta_notet() 中 return 做什么来使其工作?
谢谢
-E
这里是 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) }
class 数据库中的查询()
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;
}
在您的 'query' 方法中,您使用了流畅的界面(return $this
returns 当前对象)。因此,当您 print_r()
时,会打印一个对象。
修改您的查询方法或使用如下提取函数:
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/>";
}
}
我是 OOP 和 PDO 的新手。在此先感谢您的帮助!
我在将 SQL 查询结果循环到页面时遇到问题。
这是我的 notes.inc.php
$note->tulosta_notet('notes');
在class注意我有方法
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/>";
}
}
我用 print_r($result);
所以查询是正确的,我只是无法将行正确打印到页面。
这是 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 )
我做错了什么?
我是否应该在 notes.inc.php 中进行循环,我应该从 tulosta_notet() 中 return 做什么来使其工作? 谢谢
-E
这里是 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) }
class 数据库中的查询()
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;
}
在您的 'query' 方法中,您使用了流畅的界面(return $this
returns 当前对象)。因此,当您 print_r()
时,会打印一个对象。
修改您的查询方法或使用如下提取函数:
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/>";
}
}