我正在创建一个内容类,它将通过PDO从数据库中提取所有内容,然后通过一个方法运行结果,该方法将结果转换为对象,循环通过对象,并将对象回显到特定模块的网页。
期待什么
当激活类时,将会有一个通过PDO创建的实例,它将从mysql数据库中检索所需的数据。然后,此信息将回显并显示在网页上。
怎么了
我最终没有错误,内容应该是布尔数“1”。起初我以为这是一个SQL错误。如果我使用print_r($ query_result),我会得到相同的结果“1”。请参阅下面的测试。
我的班级代码如下
<?php
class Content {
// --- START OF ACTIVE RECORD CODE ---
public $n_id;
public $n_name;
public $n_text;
public $n_photo;
// Instantiating a STATIC Database Connection for the class to use
static protected $dbconnect;
static public function database($dbconnect) {
self::$dbconnect = $dbconnect;
}
// constructing arguments
public function __construct($args=[]) {
$this->n_id = $args['n_id'] ?? '';
$this->n_name = $args['n_name'] ?? '';
$this->n_text = $args['n_text'] ?? '';
$this->n_photo = $args['n_photo'] ?? '';
}
// Multi use method to pass in sql that will execute the PDO only two parameters are bound ID and contentText.
static public function find_by_sql($sql) {
// -------------BEGIN PDO-----------
// preparing PDO by loading sql, calling db connection, and storing in variable $stmt
$stmt = self::$dbconnect->prepare($sql);
// Binding Parameters for sql
$stmt->bindParam(':nid', $n_id, PDO::PARAM_INT);
$stmt->bindParam(':nall', $n_name, PDO::PARAM_STR);
$stmt->bindParam(':ntext', $n_text, PDO::PARAM_STR);
$stmt->bindParam(':nphoto', $n_photo, PDO::PARAM_INT);
// executing $stmt PDO and storing the result in $stmt
$query_result = $stmt->execute();
return $query_result;
// clearing the PDO after information is stored in $record
$stmt->closeCursor();
// ------------END PDO ----------
// Checking to see if a result exist. If nop result is stored in $stmt, then it will echo "Database query failed."
if(!$query_result) {
exit("Query doesn't exist.");
}
// -------- BEGIN TURNING RESULTS INTO OBJECTS --------
$object_array = [];
// The result $stmt will be stored in the variable $record
while($record = $query_result->fetchAll()) {
// Taking $record and passing it to the static method instantiate() - see below. This method will return the $object_array.
$object_array[] = self::instantiate($record);
}
return $object_array;
// ------------ END TURNING RESULTS INTO OBJECTS --------
}
// method to test passing $sql to method find_all_sql();
static public function find_all(){
$sql = "SELECT * FROM nmain WHERE nid = :id AND nall = :nall AND ntext = :ntext AND nphoto = :nphoto";
return self::find_by_sql($sql);
}
// --- BEGIN INSTANTIATE METHOD TO CREATE OBJECTS ---
static protected function instantiate($record) {
$object = new self;
// Auto assign values
foreach($record as $property => $value) {
if(property_exists($object, $property)){
$object->$property = $value;
}
}
return $object;
}
// ----- END INSTANTIATE OF RECORD TO CREATE OBJECTS ---
// ---END OF ACTIVE RECORD CODE---
}
?>
**On my html webpage:**
$contents = Content::find_all();
foreach ((array) $contents as $content) {
echo $content;
}
我测试了什么
这是我运行var_dump($ stmt)时得到的输出;
object(PDOStatement)#3 (1) { ["queryString"]=> string(119) "SELECT * FROM ndb WHERE id = :id AND nall = :nall AND ntext = :ntext AND nphoto = :nphoto" }
如果我复制查询并将其粘贴到phpmyadmin中,查询将运行绑定参数。
如果我运行var_dump($ query_result),这是输出:
bool(true) if I use print_r($query_result) I get "1"
这传递了我的if(!$ query_result)测试
如果我运行var_dump($ record)或var_dump($ query_result),我什么也得不到。似乎$ query_result,因为它是一个bool,没有数组传递给$ record。因此,没有什么可以转换为一个对象。我在这里不知所措。这是我的PDO绑定吗?
你的获取应该在语句上而不是执行的结果(这只是说执行成功或失败),fetchAll
也会尝试返回所有记录,你最想要的是fetch
来处理1条记录。时间。所以你应该有类似......
while($record = $stmt->fetch()) {
您现在可以删除正在停止进一步处理的早期return
。