mysqli查询返回结构而不是数据

时间:2014-11-14 13:38:27

标签: php mysql select 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();

当然我不想获得它,我想得到表中包含的行,我的代码有什么问题?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

你必须做

$row = $result->fetch_assoc();