php - Echo doesn't show first row made

577

I have this problem with my task system,

whenever a new member makes his first task, it doesn't show up. Only the ones made after the first one.

    <?php

            $sql = mysql_query("SELECT * FROM todo WHERE member_id = '".$_SESSION['member_id']."' ORDER by id ASC");
            if (!$sql) {
                die ('invalid query: ' . mysql_error());
            }

                 $rows = mysql_fetch_assoc($sql);

            while($rows = mysql_fetch_array( $sql )) {    

?>

After that I just echo out all of the content (tasks). each row under eachother. But the first task made just doesn't show up. It remains hidden.

Does anyone know how to fix this?

520

Answer

Solution:

$rows = mysql_fetch_assoc($sql);

this is too much. delete it. it fetches the first line, so the while loop can't get the first line.

additionally, i wouldn't usemysql_fetch_array in the while, use mysql_fetch_assoc to use an associative array.

924

Answer

Solution:

The call $rows = mysql_fetch_assoc($sql); is fetching the first row, you don't need that line. What is happening is that that line fetches the first row, and updates $sql (which is the resultset resource) to point to the second row, then when you call mysql_fetch_array( $sql ) it starts from the second row.

People are also looking for solutions to the problem: php - Update database phone number

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.