2015-02-05 100 views
1

對於那些熟悉本書的讀者,我正在閱讀本書「Luke Welling Laura Thomson第四版的PHP和MySQL Web開發」第751頁。然而,本書提供的解決方案使用MySQLi DB Connector,它在測試時工作正常。我想通過這個解決方案來使用我的一個使用PHP PDO連接器的項目,但是我試圖得到與課本相同的結果。我正在尋求一些幫助來轉換MySQLi連接器來處理PDO過程。這兩個例子都使用MySQL DB。我不確定我做錯了什麼,並尋求一點幫助。如何將MySQLi連接器轉換爲PHP PDO

我試圖讓我的PDO程序產生相同的結果擴展數組作爲原始文本書陣列做它的工作。

// 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); 
+0

那麼什麼是你看到的區別? – 2015-02-05 23:11:17

回答

0

您正在使用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) 

這兩種方法shoudl給你喜歡的數組:

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

我在這裏看到你的觀點並且一致,但是這個答案將產生一個單獨的數組,其中的文本書籍示例爲擴展的子代碼生成了4個數組。那麼,我怎麼能這樣做,還是因爲他們正在使用for循環? – 2015-02-05 23:48:47