我编写这段代码是为了从数据库中检索一些数据,但是代码不显示表中从第二行开始的第一个值。 我如何使这段代码从第一行检索数据。
<html>
<head>
<title>hello</title>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM student_record WHERE id =201102887;";
$result = mysqli_query($con , $q ) ;
if($row = mysqli_fetch_array($result)) {
echo "<table border=\"1\" style=\"width:500\">";
echo "<tr>";
echo "<th>courses</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["grade"]. "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</body>
</html>
最佳答案
改变
if($row = mysqli_fetch_array($result)) {
到
if(mysqli_num_rows($result) > 0) {
更新代码
<html>
<head>
<title>hello</title>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM student_record WHERE id =201102887;";
$result = mysqli_query($con , $q ) ;
if(mysqli_num_rows($result)>0) {
echo "<table border=\"1\" style=\"width:500\">";
echo "<tr>";
echo "<th>courses</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["grade"]. "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</body>
</html>
关于php - 如何在PHP代码中从数据库的第一行检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36098853/