如何将MySQLi连接器转换为PHPPDO


How to convert MySQLi connector to PHP PDO

我正在为熟悉这本书的人阅读这本书"PHP和MySQL Web开发,作者Luke Welling Laura Thomson第四版"第751页。然而,本书中提供的解决方案是使用MySQLi DB连接器,该连接器在测试时运行良好。我想在我的一个项目中采用这个解决方案,该项目使用PHP PDO连接器,但我遇到了一个问题,因为我试图导出与文本书相同的结果。我正在寻求一些帮助,以转换MySQLi连接器来处理PDO过程。这两个例子都使用MySQL数据库。我不知道自己做错了什么,也不知道自己在寻求什么帮助。

我正试图让我的PDO过程在children id上为扩展数组生成与原始文本书数组相同的结果。

// Example taken from the text book
function expand_all(&$expanded) {
    // mark all threads with children as to be shown expanded
    $conn = db_connect();
    $query = "select postid from header where children = 1";
    $result = $conn->query($query);
    $num = $result->num_rows;
    for($i = 0; $i<$num; $i++) {
        $this_row = $result->fetch_row();
        $expanded[$this_row[0]]=true;
    }
}
// The print_r form the text book example looks like this:
// result:
mysqli_result Object (  [current_field] => 0 [field_count] => 1 
                        [lengths] => [num_rows] => 3 [type] => 0 
                      ) 
// expended:
   Array ( [0] => 1 ) Array ( [0] => 2 ) Array ( [0] => 4 ) 
//--------------------------------------------------------------//
// Know, here is my new adopted changes for using PHP PDO connector  
function expand_all(&$expanded)
{
    // mark all threads with children to be shown as expanded
    $table_name = 'header';
    $num = 1; 
    $sql = "SELECT postid FROM $table_name WHERE children = :num";
    try
    {
        $_stmt = $this->_dbConn->prepare($sql);
        $_stmt->bindParam(":num", $num, PDO::PARAM_INT);
        $_stmt->execute();
        $result  = $_stmt->fetchAll(PDO::FETCH_ASSOC);          
        // get the $expanded children id's
        foreach ($result as $key => $value) 
        { 
            foreach ($value as $k => $val)
            {
                $expanded[$k] = $val;
            }
        }
        return $extended;
    }
    catch(PDOException $e)
    {
        die($this->_errorMessage = $e);
    }
    //close the database  
    $this->_dbConn = null;          
}
// The print_r for the result looks like this:
 Array ( [0] => Array ( [children_id] => 1 ) 
         [1] => Array ( [children_id] => 2 ) 
         [2] => Array ( [children_id] => 4 ) 
        ) 
// The return extended print_r for the children id's
// look like this:
    Array ( [children_id] => 4); 

您使用的是PDO::FETCH_ASSOC,由于每个结果都有相同的列名,因此您将覆盖循环每次迭代中的值。

如果你想让$exapnded是一个id数组,你需要调整你的循环,或者你应该使用PDO::FETCH_COLUMN

带回路调节:

   foreach ($result as $key => $value) 
    { 
        foreach ($value as $k => $val)
        {
            $expanded[] = $val;
        }
    }
    return $expanded;

带提取模式调整:

    $_stmt = $this->_dbConn->prepare($sql);
    $_stmt->bindParam(":num", $num, PDO::PARAM_INT);
    $_stmt->execute();
    $result  = $_stmt->fetchAll(PDO::FETCH_COLUMN, 0);
    // at this point im not sure whay you are both passing 
    // $expanded by reference and returning it...
    // if you want to overwrite this array just do $expanded = $result
    // if you need to add these items TO an existing $expanded you can use
    // $expanded = array_merge($expanded, $result)

这两种方法都应该给你一个数组,比如:

Array(
  0 => 1,
  1 => 2,
  2 => 4
)