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"];
}
?>