首页 > 解决方案 > PDO 不关联使用 bindParam() 设置的参数,并且在使用 $args 的方法中返回一个空数组

问题描述

我想要达到的目标:

解释:

我正在构建一个简单的过程,我的类“内容”会将数组转换为对象,并在调用它们时将它们打印在我的网页上。

预期结果:

$stmt PDO 将使用 $stmt->bindParam() 绑定参数。绑定变量与构造中的 $args 匹配。然后将数组传递给一个静态方法,该方法会将数组转换为对象。

结果

使用 print_r(),我可以跟踪我的 PDO 结果到转换为 $object 的阶段。我将在代码末尾的注释中注明结果。

  1. 当我浏览结果时, print_r() 返回数据库列名而不是 bindPram() 变量名。
  2. 最后,我的网页上显示了一个空白数组。

对我来说,我的 bindParams() 似乎在我的 PDO 中不起作用。PDO 本身正在工作,因为我能够从数据库中选择和打印。我不确定我做错了什么。

我的代码

<?php

class Content {

// --- START OF ACTIVE RECORD CODE ---

public $n_id;  // bind with column name 'nid'  in database
public $n_text; //bind with column name 'ntext' in database


// instantiating a STATIC Database Connection for the class to use
static protected $database;

static public function set_database($database) {
  self::$database = $database;
}

// constructing arguments
public function __construct($args=[]) {

  $this->n_id = $args['n_id'] ?? '';
  $this->n_text = $args['n_text'] ?? '';

}

// Multi use method to pass in sql that will execute the PDO only two parameters are bound nid and ntext.

static public function look_all_sql($sql) {

// -------------BEGIN PDO-----------

// checking to ensure PDO extensions are loaded

if (!extension_loaded ('PDO' )) {
    echo 'PDO is NOT loaded!'."\n";
    if (!extension_loaded('pdo_mysql')){
        echo 'PDO mysql driver is NOT loaded.'."\n";
    }
}

// preparing PDO by loading sql from method find_all(), calling db connection, and storing in variable $stmt

  $stmt = self::$database->prepare($sql);

  // Binding Parameters for sql 
  $stmt->bindParam(':nid', $n_id,  PDO::PARAM_INT);
  $stmt->bindParam(':ntext', $n_text,  PDO::PARAM_STR);

  // executing $stmt PDO and storing the result in $stmt
  $stmt->execute();


// ------------END PDO ----------

  // Checking to see if a result exist. If no result is stored in $stmt, then it will echo "Database query failed." If result is stored, then the method will continue 

if(!$stmt) {
    exit("Database query failed.");
  }

// -------- BEGIN TURNING RESULTS INTO OBJECTS --------
$object_array = [];

  // The result $stmt will be stored in the variable $record

  while($record = $stmt->fetch(PDO::FETCH_ASSOC)) {


// passing $record to static method instantiate() to be converted to an object and returned as $object_array

    $object_array[] = self::instantiate($record);

}
  // clearing the PDO after information is stored in $record
    $stmt->closeCursor();

  return $object_array;

// ------------ END TURNING RESULTS INTO OBJECTS --------

}
// method to test passing $sql to method look_all_sql();

static public function find_all(){

  $sql = "SELECT * FROM `ncontent`";
//passing $sql to static method 
    return self::look_all_sql($sql);

}

// --- BEGIN INSTANTIATING 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;

}



  }


}
// ----- END INSTANTIATING OF RECORD TO CREATE OBJECTS ---

// ---END OF ACTIVE RECORD CODE---



}


?>

没有 print_r 的网页上填充了什么

没有任何页面是空的。

基于 print_r 的结果:

返还时

$return = $stmt->fetch(PDO::FETCH_ASSOC);
 return print_r($return);

Array ( [0] => Array ( [nid] => 2 [ntext] => 这是第一行称为 Top Main )

在look_all_sql($sql) 中的$record

while($record = $stmt->fetch(PDO::FETCH_ASSOC)) {
return print_r($record);

在静态方法实例化()的 $record 上:

static protected function instantiate($record) {
return print_r($record);

显示完整的数据库信息数组。没有绑定的婴儿车。类似于 fetchAll() 命令:

Array ( [nid] => 2 [ntext] => This is the first Line called the Top Main ) Array ( [nid] => 3 [ntext] => Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor ) etc

在静态实例化($record) 方法中的 $object 上。对象的数量与我的数据库中的对象数量相匹配。但是,它与数据库中的信息无关

$object = new self;
return print_r($object); 

此时 $object 应该返回到 $object_array[]。

$object_array 上的 print_r:

网页代码:

<h2><?php

    $ncontents = array(Content::find_all());

    foreach($ncontents as $ncontent) {
        echo $ncontent;
         }

    ?></h2> 

标签: phparrayspdoargsbindparam

解决方案


推荐阅读