2015-05-17 57 views
0

我想將mysql查詢結果導入到PHP數組中。當我運行我的腳本並打印數組時,它不會顯示它從mysql表中獲取的信息,而只是顯示有關該數組的信息。無法將MySQL查詢結果導入到PHP數組中

這是我的代碼:

<?php 
class db { 
    public $conn; 
    function dbLogin() { 
    $this->conn = new mysqli("1.2.4.3","user","pwd","database");  
    } 

    public function selectQuery() { 
    $this->dbLogin(); 
    $query = " 
     SELECT  * 
     FROM  myTable 
    "; 
    echo "<pre>"; 
    $resultArray = Array(); 
    if ($result = $this->conn->query($query)) { 
     while ($row = $result->fetch_array()) { 
      $resultArray[] = $result; 
     } 

    } 
    var_dump($resultArray); 
    echo "</pre>"; 
    } 
} 

$fData = new db(); 
$fData->selectQuery(); 
?> 

當運行上述的腳本,我得到這樣的:

array(88) { 
[0]=> 
object(mysqli_result)#3 (5) { 
["current_field"]=> 
int(0) 
["field_count"]=> 
int(34) 
["lengths"]=> 
NULL 
["num_rows"]=> 
int(88) 
["type"]=> 
int(0) 
} 
[1]=> 
object(mysqli_result)#3 (5) { 
["current_field"]=> 
int(0) 
["field_count"]=> 
int(34) 
["lengths"]=> 
NULL 
["num_rows"]=> 
int(88) 
["type"]=> 
int(0) 
} 
... 
} 

以上所述顯示/對象/陣列的前兩88的實例在頁面上顯示的內容。但它不顯示來自數據庫的實際信息。

我試過到目前爲止:

我試着echovar_dumpprint_r,並且printf沒有運氣的陣列。我真的很想看到數組中的查詢結果,所以我知道我的腳本正在以我想要的方式工作,但我似乎無法弄清楚如何做到這一點。

我知道一個事實,該查詢工作正常,並且如果我只是用替換$resultArray[] = $result;,我可以看到該表的結果。

如何從我的查詢中查看數組中的數據,我知道我的腳本正在工作?

+0

'$ result'是您的查詢。嘗試傾銷'$行' –

回答

3

更改$resultArray[] = $result;$resultArray[] = $row;

+0

我的一部分愚蠢的疏忽。這做到了。謝謝! – aCarella

1

除了組件名稱之外,添加到someOne的解決方案中,var_dump()確實也會爲您提供數組的值。在數據類型後括號中的數字或字符串,是您數組的值:

array(88) { 
[0]=> 
object(mysqli_result)#3 (5) { 
["current_field"]=> 
int(0) 
["field_count"]=> 
int(34) 
["lengths"]=> 
NULL 
["num_rows"]=> 
int(88) 
["type"]=> 
int(0) 
} 

這裏,場current_field有型int和價值0num_rows也有類型int和值88

1

請將代碼從

if ($result = $this->conn->query($query)) { 
    while ($row = $result->fetch_array()) { 
     $resultArray[] = $result;// this would be $row 
    } 

} 

改爲

if ($result = $this->conn->query($query)) { 
    while ($row = $result->fetch_array()) { 
     $resultArray[] = $row; 
    } 

}