我是一名初级程序员,我通过mysqli_query
从数据库获取信息有点麻烦。我首先连接到数据库,然后尝试从数据库内部的表cbo中获取信息。然后打印出查询的结果,而不是表中的信息。而这是我得到的。
mysqli_result Object
(
[current_field] => 0
[field_count] => 8
[lengths] =>
[num_rows] => 12
[type] => 0
)
这是我使用的代码。Dump只回显变量。
<?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 );
?>
$result
只是一个包含结果集的对象。你必须从中获取数据。读取mysqli_fetch_assoc或mysqli_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的数组,它将包含您获取的所有行,然后您可以稍后访问该数组并相应地处理
这是mysqli对象,你想用它做什么?你应该阅读https://www.php.net/manual/mysqli-result.fetch-assoc.php, https://www.php.net/manual/mysqli-result.fetch-object.php或https://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);
}
?>
$result = mysqli_query($con,"SELECT * FROM cbo");
$rows = array();
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
print_r($rows);