简体   繁体   中英

Php skips first MySQL row. Not the common double fetch() mistake

Hello everyone

I bring forth the following issue: My php code skips the first mysql row when printing it onto the screen. I have already seen that the issue is solved by removing a double fetch() method outside the while() loop on other questions, but I fail to see that happening here, so what else could it be? Im lost.

This is the code:

<?php
$servername = "localhost";
$username = "root";
$password = "standup";
$database = "sakila";
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully<br><br>";
$query = "select * from actor;";
$queryResult = $conn->query($query);
while($queryRow = $queryResult->fetch_row()){
    echo "$queryRow[1]<br>";
}
$conn->close();
?>

This is the original database:

Mysql

And this is the result in a browser

Browser

Answer provided by @rtfm in the comments

Solution 1

Add order by to the query, in this situation $query = "select * from actor order by actor_id;"; solved the issue of skipping the first row.

Solution 2

Add limit to the query, in this situation $query = "select * from actor limit 100;"; solved the issue of skipping the first row also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM