2014-11-14 74 views
0

當我做一個查詢到我的數據庫是這樣的結果:mysqli的查詢返回的結構,而不是數據

mysqli_result Object 
(
    [current_field] => 0 
    [field_count] => 3 
    [lengths] => 
    [num_rows] => 3 
    [type] => 0 
) 

這是我使用的代碼:

$servername = "localhost"; 
$username = "root"; 
$password = ""; 

// Create connection 
$conn = new mysqli($servername, $username, $password, 'melona'); 

// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$sql = "SELECT * FROM votacion"; 

$result = $conn->query($sql); 

echo "<pre>"; 
print_r($result); 
echo "</pre>"; 

die(); 

當然我不我不想獲取該表,我想獲取表中包含的行,我的代碼有什麼問題?

+3

查詢返回一個結果集,然後您可以在何時,何處以及如何獲取結果集(作爲assoc數組,作爲對象,數字索引數組......),它就是如何工作的,以及它總是如何工作......另外:不要「死」,而是關閉連接,並在完成後釋放資源。這裏不需要_really_,但這是一個好習慣 – 2014-11-14 13:42:06

+1

謝謝,我也應用了你的建議。 – Mariano 2014-11-14 13:53:31

回答

1

那麼,你需要先搞獲取結果,要做到這一點,你需要爲使用->fetch_assoc()->fetch_array()

// you need to loop it if you're expecting multiple rows 
while($row = $result->fetch_assoc()) { 
    echo $row['column_name']; 
} 

編號:

http://php.net/manual/en/mysqli-result.fetch-array.php

http://php.net/manual/en/mysqli-result.fetch-assoc.php

+0

非常好,我用 while($ row = $ result-> fetch_assoc()) \t $ array [] = $ row; 獲得一個很好的陣列。 – Mariano 2014-11-14 13:52:22

+0

@Mariano im很高興這有幫助 – Ghost 2014-11-14 13:54:51

0

您必須做

$row = $result->fetch_assoc(); 
相關問題