php - 从数据库获取数据以创建表 - PHP

标签 php html mysql

我创建了一个从数据库检索数据的函数和一个将数据放入表中的函数。

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/

相关文章:

javascript - 更改虚拟列phpgrid的位置

html - 媒体=全部 vs 媒体=屏幕

javascript - 按钮激活的 div 幻灯片进入下方而不是上方

html - 使textarea填充表格中整个单元格的方法

mysql - 计算mysql中选定数据的行数

php - 使用 PHP 将分隔值文件导入数据库时​​如何删除或避免插入列标题?

php - mysql 查询(如)和 php 的问题

php - Laravel/PHP 显示变量的错误输出

php - 如何在 mysql_fetch_assoc 结果中传递一个 post 变量

mysql - 如何将时间戳格式转换为2011-05-10T06 :58:00?