gpt4 book ai didi

php - 尝试在 HTML 表中显示 MySQL 查询的结果

转载 作者:行者123 更新时间:2023-11-29 03:37:23 27 4
gpt4 key购买 nike

我正在尝试从数据库表中获取一些简单信息并将其发布到 HTML 页面上。这看起来非常简单直接,我没有收到任何错误,这意味着我连接到数据库和表。我已经拥有所有数据(手动输入)。

我假设这是我调用列名进行查询的方式。

<?php
require_once("settings.php");
//Open Connection
$conn = @mysqli_connect("$host","$user","$pswd")
or die ('Failed To Connect to Server');
@mysqli_select_db("$conn", "$dbnm")
or die ('Database Not Available');
//Set up SQL string and excecute
$car_id = mysqli_escape_string($_GET['car_id']);
$make = mysqli_escape_string($_GET['make']);
$model = mysqli_escape_string($_GET['model']);
$price = mysqli_escape_string($_GET['price']);

$query = "SELECT car_id, make, model, price FROM cars";

$results = mysqli_query($conn, $query);

echo "<table width ='100%' border='1'>
<tr>
<th>car_id</th>
<th>make</th>
<th>model</th>
<th>price</th>
</tr>";

// $row = mysqli_fetch_row($query);
while ($row = mysqli_fetch_array($results)) {
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['make'] . "</td>";
echo "<td>" . $row['model'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>";
// $row = mysqli_fetch_row($query);
}
echo "</table>";
//Close Connection
mysqli_free_result($results);
mysqli_close($conn);
?>

settings.php 包含所有连接信息,并且全部检查出来。我什至需要 ($_GET['car_id']) 等吗?我可以只通过字段名称调用它们吗?

答案会很明显...

最佳答案

I get no errors which means I connect to the DB and the table

不是这个意思。

mysqli_select_db("$conn", "$dbnm")  // That `$conn` should not be inside those quotes.

应该是

mysqli_select_db($conn, $dbnm);  // that $conn has to be a MySQLi link identifier, not an interpolated one.

同时删除所有 @ 并对这些函数返回的内容进行一些错误检查。

关于php - 尝试在 HTML 表中显示 MySQL 查询的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19420347/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com