0

我是一名初学者程序员,在通过mysqli_query. 我首先连接到数据库,然后尝试从数据库内部的表 cbo 中获取信息。然后我打印出查询的结果,而不是表中的信息。相反,这就是我得到的。

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

这是我正在使用的代码。转储只是回显变量。

<?php
    $con = mysqli_connect("localhost", "root", "harvard", "cbo projections");
    if ( mysqli_connect_errno() ) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result = mysqli_query($con,"SELECT * FROM cbo");
    dump( $result );
?>
4

3 回答 3

3

$result只是一个包含结果集的对象。您必须从中获取数据。读取mysqli_fetch_assocmysqli_fetch_array

例子:

if ($result = mysqli_query($link, $query)) {
    while ($row = mysqli_fetch_assoc($result)) {
        //Display fields here according to your table structure
    }
    mysqli_free_result($result);
}

你可以做类似的事情

    while ($row = mysqli_fetch_assoc($result)) {
        $records[]=$row;
    }

这将创建一个名为 records 的数组,其中将包含所有获取的行,然后您可以稍后访问该数组并进行相应的处理

于 2013-03-01T05:50:30.987 回答
2

那就是mysqli对象,你想用它做什么?您应该阅读https://www.php.net/manual/mysqli-result.fetch-assoc.phphttps://www.php.net/manual/mysqli-result.fetch-object.phphttps: //www.php.net/manual/mysqli-result.fetch-array.php

举例:

<?php

$con=mysqli_connect("localhost", "root", "harvard", "cbo projections");
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM cbo");
while ($row = mysql_fetch_assoc($result)) {
    var_dump($row);
}

?>
于 2013-03-01T05:52:27.807 回答
0
$result = mysqli_query($con,"SELECT * FROM cbo");
$rows = array();
while ($row = mysqli_fetch_assoc($result)) {
  $rows[] = $row;
}
print_r($rows);
于 2013-03-01T05:53:33.357 回答