<html>
<head>
<meta http-equiv = "content-type" content = "text/html; charset = utf-8" />
<title>Using file functions PHP</title>
</head>
<body>
<h1>Web Development - Lab05</h1>
<?php
require_once("settings.php");
$dbconnect = @mysqli_connect($host, $user, $pswd, $dbnm);
if($dbconnect->connect_errno >0)
{
die('Unable to connecto to database [' . $db->connect_error . ']');
}
$queryResult = "SELECT car_id, make, model, price FROM cars";
echo "<table width='100%' border='1'>";
echo "<tr><th>ID</th><th>Make</th><th>Model</th><th>Price</th></tr>";
//initiate array
$displayrow= mysqli_fetch_array($queryResult);
//initiate while loop to iterate through table
while($displayrow)
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Make'] . "</td>";
echo "<td>" . $row['Model'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($dbconnect);
?>
</body>
</html>
这是我的头脑,我无法弄清楚为什么除了Table标题之外它不会显示实际数据。无论我用什么。 我试过mysqli_fetch_array,mysqli_fetch_row,mysqli_fetch_assoc但没有任何作用。
帮助和解释为什么它不显示数据将非常感激:)
答案 0 :(得分:3)
首先:您没有运行查询,只是将查询文本放在变量中。你需要使用mysqli_query。
第二:你应该将mysqli_fetch_array添加到循环中。
例如:
while($displayrow = mysqli_fetch_array($queryResult))
{
}
否则你只会获得第一行。
第三:数组键区分大小写。正如Jeribo指出的那样,没有$row['ID']
,在您的查询中引用了$row['car_id']
。 $row['Make']
与$row['make']
不同。
答案 1 :(得分:0)
如果你想在外面查询,你仍然需要在循环中设置它:
$result = $db->query($queryResult)
while($row = $result ->fetch_assoc()){
...
}
这里显示了一个好的教程:http://codular.com/php-mysqli
答案 2 :(得分:0)
$ row需要初始化,所以为什么不尝试:
while($row = mysqli_fetch_array($queryResult))
{
....
}
答案 3 :(得分:0)
您必须先获取结果集,然后尝试从结果集
中获取数组<?php
require_once("settings.php");
$dbconnect = @mysqli_connect($host, $user, $pswd, $dbnm);
if($dbconnect->connect_errno >0)
{
die('Unable to connecto to database [' . $db->connect_error . ']');
}
$query = "SELECT car_id, make, model, price FROM cars";
$resultSet=mysqli_query($dbconnect,$query)
echo "<table width='100%' border='1'>";
echo "<tr><th>ID</th><th>Make</th><th>Model</th><th>Price</th></tr>";
//initiate array
$displayrow= mysqli_fetch_array( $resultSet);
//initiate while loop to iterate through table
while($displayrow)
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Make'] . "</td>";
echo "<td>" . $row['Model'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($dbconnect);
?>
答案 4 :(得分:0)
请精确查询Query中的字段名称(car_id,make,...)
while($displayrow= mysql_fetch_assoc($queryResult) )
{
echo "<tr>";
echo "<td>" . $displayrow['car_id'] . "</td>";
echo "<td>" . $displayrow['make'] . "</td>";
echo "<td>" . $displayrow['model'] . "</td>";
echo "<td>" . $displayrow['price'] . "</td>";
echo "</tr>";
}