我的数据库:
网站上的数据:
代码:
<?php
$con=mysqli_connect("localhost","root","","organisation");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM deletebs") or die(mysql_error());;
echo '<pre>'; print_r($result); echo '</pre>';
echo "<!doctype html>
<html lang=\"en\">
<head>
<!-- Bootstrap CSS -->
<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css\">
<title>Hello, world!</title>
</head>
<body>
<table border='1'>
<tr>
<th>id</th>
<th>below_whom</th>
<th>name</th>
</tr>";
$row = mysqli_fetch_array($result);
echo '<pre>'; print_r($row); echo '</pre>';
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['below_whom'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
我的问题是为什么第一行被省略?我在dbms方面有点弱。但我相信所写的查询是正确的,因为正确地获取了一些数据,为什么当below_whom留空时数据被省略。在创建db时,我确保所有列都接受空值。这是我的数据库的结构。
答案 0 :(得分:0)
首先不要删除此$row = mysqli_fetch_array($result);
行,因为您要提取两次。
然后不要这样做:
while($row = mysqli_fetch_array($result)) { echo "
<tr>"; echo "
<td>" . $row['id'] . "</td>"; echo "
<td>" . $row['below_whom'] . "</td>"; echo "
<td>" . $row['name'] . "</td>"; echo "</tr>"; }
试试这个
while ($row = mysqli_fetch_assoc($result)){
$id => $row['id'],
$below_whom => $row['below_whom'],
$name => $row['name']
}
现在你已经在这些变量中获得了数据,你可以在代码中的任何地方使用它