我创建了一个从数据库检索数据的函数和一个将数据放入表中的函数。
function get_order()
{
$order_query = "SELECT order_number From tbl_order_header";
$data = mysqli_query($con, $order_query);
$order = array();
while ($object = mysqli_fetch_object($data))
{
$order[] = $object;
}
mysqli_close($con);
return $order;
}
function get_table()
{
$table_str = '<table>';
$get_orders = get_order();
foreach ($get_orders as $get_order)
{
$table_str .= '<td>';
$table_str .= '<td>'.$get_order->order_number.'</td>';
$table_str .= '</td>';
}
$table_str .= '</table>';
return $table_str;
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php echo get_table();?>
</body>
</html>
但是我收到如下错误消息。
line 8: $data = mysqli_query($con, $order_query);
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\cmg-logistics\Testing.php on line 8
line 10: while ($object = mysqli_fetch_object($data))
Warning: mysqli_fetch_object() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\cmg-logistics\Testing.php on line 10
line 14: mysqli_close($con);
Warning: mysqli_close() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\cmg-logistics\Testing.php on line 14
如何解决该错误?
最佳答案
这是因为$con没有定义。尝试在函数内传递 $con 。
$con = mysqli_connect(HOST, USER, PASS, DB_NAME);
关于php - 从数据库获取数据以创建表 - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37825793/