I connect to a database, put the result set in an array using mysql_fetch_assoc and then later in my code I use mysql_fetch_assoc again to loop through the array and I found that the first row of the result set is missing. Later in my code I again use the mysql_fetch_assoc and find that the first 2 rows of the result set are missing.

Apparently everytime you run the mysql_fetch_assoc function it deletes the first row of the result set? How can I loop through an array more than once without having this problem?

Below is my code:

<?php
$host = "";
$uname="root";
$pass="";
$database="discuss";
$tablename="threads";
$connection = mysql_connect($host, $uname, $pass);
$result =mysql_select_db("discuss");
$query ="SELECT topic, thread_num, sub_num, new_thread, date_time, name from discuss22 order by thread_num desc, sub_num asc";
$result = mysql_query($query);
$t_array = mysql_fetch_assoc($result);
?>

<form name="form1" method="post" action="discuss_submit.asp">
<div align="left">Press:
<input name="imageField" type="image" src="images/Submit.gif" width="55" height="14" border="0">
<input type="hidden" name="newpost" value="yes">
<input type="hidden" name="thread_num" value="
<?php $max_thread = array_slice ($t_array,2,1);
foreach ($max_thread as $th)
{
echo $th+1;
}
?>
">
<input type="hidden" name="sub_thread" value="0">
to start a new topic.

<?php
while ($row = mysql_fetch_assoc($result))
{
echo $row["name"];
echo "<br>";
}
?>
</form>
<p align="left">View current topics here:</p>

<table width="557" border="1" cellspacing="0">

<?php
while ($row2 = mysql_fetch_assoc($result))
{
echo $row2["topic"];
}
?>

    Instead of this:
    while ($row = mysql_fetch_assoc($result))

    store the values in an array:

    while ($row[] = mysql_fetch_assoc($result))

    Now you can go back to $row[0],$row[1], etc., whenever you want.

      Theres also mysql_data_seek()-function to move resultpointer to beginning. For example you could put mysq_data_seek($result,0); before your while statement.

        I used your mysql_data_seek function to move the resultpointer back to the beginning and that works great-- but can someone tell me why in the hell the writers of php made the mysql_fetch_assoc function move the resultpointer forward everytime it runs?

        I can think of several instances where I need to loop through an array and write only one column of the array to the page- so I need to use the mysql_fetch_assoc more than once- but I have to run mysql_data_seek each time to move the pointer back. Seems like more work than it should be...

        Is there an easier way?

          Let's see -- you can either capture the info once and reuse it...my way...or reset the pointer...cahva's way. How exactly would you make this simpler?

            Nemonoman- I would prefer your way, but I understand that your way would work if I wanted to loop through specific columns of my resultset array at only one point in the code- I could echo one column of info from the array, and then subsequently echo another column using the method you described.

            However, if I want to loop through the array later in the code (meaning in another block of <?php ?>)I have to use:

            while ($row[] = mysql_fetch_assoc($result))

            a second time correct? How can I use the above function only once and then loop through the array again later?

            I'm having trouble figuring out how to loop through arrays- I thought everytime you wanted to loop through an array you had to run the mysql_fetch_assoc() function.

              Write a Reply...