Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将MySQLi连接器转换为PHP-PDO_Php_Mysql_Pdo_Mysqli - Fatal编程技术网

如何将MySQLi连接器转换为PHP-PDO

如何将MySQLi连接器转换为PHP-PDO,php,mysql,pdo,mysqli,Php,Mysql,Pdo,Mysqli,我正在阅读卢克·韦林·劳拉·汤姆森(Luke Welling Laura Thomson)的《PHP和MySQL Web开发》一书,第四版第751页,供熟悉本书的读者阅读。 然而,本书中提供的解决方案是使用MySQLIDB连接器,该连接器在测试时工作良好。我想将这个解决方案应用到我的一个使用PHPPDO连接器的项目中,但是我遇到了一个问题,因为我试图得到与教科书相同的结果。我正在寻求一些帮助,以转换MySQLi连接器来处理PDO进程。这两个示例都使用MySQL数据库。我不确定我做错了什么,也不寻

我正在阅读卢克·韦林·劳拉·汤姆森(Luke Welling Laura Thomson)的《PHP和MySQL Web开发》一书,第四版第751页,供熟悉本书的读者阅读。 然而,本书中提供的解决方案是使用MySQLIDB连接器,该连接器在测试时工作良好。我想将这个解决方案应用到我的一个使用PHPPDO连接器的项目中,但是我遇到了一个问题,因为我试图得到与教科书相同的结果。我正在寻求一些帮助,以转换MySQLi连接器来处理PDO进程。这两个示例都使用MySQL数据库。我不确定我做错了什么,也不寻求什么帮助

我试图让我的PDO过程在子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_列

带回路调整:

   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)
带取数模式调整:

   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
)

那么你看到的区别是什么呢?我在这里看到了你的观点,并且是一致的,但是这个答案将生成一个数组,其中教科书示例为扩展的子id生成4个数组。那么,我怎样才能做同样的事情呢,或者是因为他们使用了for循环?