除第1行以外的所有行都显示。

时间:2022-09-17 14:40:35

When I select some data from my database (MySQL), it shows all the rows except the first one. How to fix it?

当我从数据库(MySQL)中选择一些数据时,它显示除了第一个行之外的所有行。如何修复它吗?

Code

代码

// Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id, title, image, description, tekst, address, website, telephone, email, openinghours FROM ExperienceUtrecht";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
    echo "<table border='1'> <tr> <th>ID</th> <th>Title</th> <th>Image</th> <th>Description</th> <th>Tekst</th> <th>Address</th> <th>Website</th> <th>Telephone</th> <th>Email</th> <th>Opening Hours</th> </tr>"; while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . $row['title'] . "</td>"; echo "<td>" . $row['image'] . "</td>"; echo "<td>" . $row['description'] . "</td>"; echo "<td>" . $row['tekst'] . "</td>"; echo "<td>" . $row['address'] . "</td>"; echo "<td>" . $row['website'] . "</td>"; echo "<td>" . $row['telephone'] . "</td>"; echo "<td>" . $row['email'] . "</td>"; echo "<td>" . $row['openinghours'] . "</td>"; echo "</tr>"; } echo "</table>";
}
} else {
echo "0 results";
}

mysqli_close($conn);
?>

3 个解决方案

#1


2  

You have two while loops as @user2983401 pointed out. Get rid of the first one:

您有两个while循环,@user2983401指出。去掉第一个:

// Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id, title, image, description, tekst, address, website, telephone, email, openinghours FROM ExperienceUtrecht";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
// output data of each row
/*while($row = mysqli_fetch_assoc($result)) { Not needed*/
    echo "<table border='1'> <tr> <th>ID</th> <th>Title</th> <th>Image</th> <th>Description</th> <th>Tekst</th> <th>Address</th> <th>Website</th> <th>Telephone</th> <th>Email</th> <th>Opening Hours</th> </tr>"; 
    while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . $row['title'] . "</td>"; echo "<td>" . $row['image'] . "</td>"; echo "<td>" . $row['description'] . "</td>"; echo "<td>" . $row['tekst'] . "</td>"; echo "<td>" . $row['address'] . "</td>"; echo "<td>" . $row['website'] . "</td>"; echo "<td>" . $row['telephone'] . "</td>"; echo "<td>" . $row['email'] . "</td>"; echo "<td>" . $row['openinghours'] . "</td>"; echo "</tr>"; } echo "</table>";
/*} Not needed*/
} else {
echo "0 results";
}

mysqli_close($conn);
?>

#2


2  

You have two while loops. The first loop has moved The cursor to the first record. The second while loop moves the cursor again. Suggestion: Move the the table tag and table heads directly into the If and remove the first loop.

你有两个while循环。第一个循环将光标移到第一个记录。第二个while循环再次移动光标。建议:将表标记和表头直接移到If中,并删除第一个循环。

#3


0  

//it will work. your logics was good but unfortunately you did just simple mistake. you used two while loops to fetch same record.

/ /将工作。你的逻辑很好,但不幸的是你犯了一个简单的错误。您使用了两个while循环来获取相同的记录。

//first while loop fetch first record and after inner loop run, and second record and continue record fetch and you tried to print this record.

//首先,while循环获取第一个记录,在内部循环运行之后,第二个记录并继续记录取回,并尝试打印此记录。

#1


2  

You have two while loops as @user2983401 pointed out. Get rid of the first one:

您有两个while循环,@user2983401指出。去掉第一个:

// Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id, title, image, description, tekst, address, website, telephone, email, openinghours FROM ExperienceUtrecht";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
// output data of each row
/*while($row = mysqli_fetch_assoc($result)) { Not needed*/
    echo "<table border='1'> <tr> <th>ID</th> <th>Title</th> <th>Image</th> <th>Description</th> <th>Tekst</th> <th>Address</th> <th>Website</th> <th>Telephone</th> <th>Email</th> <th>Opening Hours</th> </tr>"; 
    while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . $row['title'] . "</td>"; echo "<td>" . $row['image'] . "</td>"; echo "<td>" . $row['description'] . "</td>"; echo "<td>" . $row['tekst'] . "</td>"; echo "<td>" . $row['address'] . "</td>"; echo "<td>" . $row['website'] . "</td>"; echo "<td>" . $row['telephone'] . "</td>"; echo "<td>" . $row['email'] . "</td>"; echo "<td>" . $row['openinghours'] . "</td>"; echo "</tr>"; } echo "</table>";
/*} Not needed*/
} else {
echo "0 results";
}

mysqli_close($conn);
?>

#2


2  

You have two while loops. The first loop has moved The cursor to the first record. The second while loop moves the cursor again. Suggestion: Move the the table tag and table heads directly into the If and remove the first loop.

你有两个while循环。第一个循环将光标移到第一个记录。第二个while循环再次移动光标。建议:将表标记和表头直接移到If中,并删除第一个循环。

#3


0  

//it will work. your logics was good but unfortunately you did just simple mistake. you used two while loops to fetch same record.

/ /将工作。你的逻辑很好,但不幸的是你犯了一个简单的错误。您使用了两个while循环来获取相同的记录。

//first while loop fetch first record and after inner loop run, and second record and continue record fetch and you tried to print this record.

//首先,while循环获取第一个记录,在内部循环运行之后,第二个记录并继续记录取回,并尝试打印此记录。