Solved

PHP: mysql_data_seek($rs, 0) returns additional blank line when used with 2nd Repeat Region

Posted on 2009-04-06
2
567 Views
1 Endorsement
Last Modified: 2013-12-12
I have a simple recordset that simply lists Record ID's and Record Names.
I want to repeat the same rows from the query in 2 or more separate places on the page.

When using "mysql_data_seek($rs, 0)" to restart the recordset query i am getting a additional blank line added to the repeated content.

------------------------
Existing Code:
<p>
<?php
      do {
            echo "1. ".$row_rs['Field']."<br/>";
      } while ($row_rs = mysql_fetch_assoc($rs));
?>
</p>
<p>
<?php
      mysql_data_seek($rs, 0);      
      do {
            echo "2. ".$row_rs['Field']."<br/>";
      } while ($row_rs = mysql_fetch_assoc($rs));
?>
</p>

Returns the following:
1. Field 01
1. Field 02
1. Field 03

2.
2. Field 01
2. Field 02
2. Field 03
------------------------

This is a scaled down example of what i'm actually doing which is multiple nested repeat regions, the problem above is causing my script to lose the primary key of the second repeat region.

I could of course fix this by using different Recordset Queries per repeat region but won't this be placing unnecessary stress on the database?

How can i fix this blank row being added?
Thanks
1
Comment
Question by:ticallian
2 Comments
 
LVL 14

Accepted Solution

by:
shobinsun earned 500 total points
Comment Utility
Hello,

Use this :

while ($row_rs = mysql_fetch_assoc($rs))
{
echo "1. ".$row_rs['name']."<br/>";
}
?>
</p>
<p>
<?php
mysql_data_seek($rs, 0);      
     
while ($row_rs = mysql_fetch_assoc($rs))
{
echo "2. ".$row_rs['name']."<br/>";
}


Instead of :

      do {
            echo "1. ".$row_rs['Field']."<br/>";
      } while ($row_rs = mysql_fetch_assoc($rs));
?>
</p>
<p>
<?php
      mysql_data_seek($rs, 0);      
      do {
            echo "2. ".$row_rs['Field']."<br/>";
      } while ($row_rs = mysql_fetch_assoc($rs));
?>

Do part will execute atleast once  even if the while condition is not true.


Hope this will Help you.

Regards.
1
 

Author Closing Comment

by:ticallian
Comment Utility
Thanks, with a small adjustment the code worked, without the adjust i lost the first row from the first repeat region.

Revised code:
<?php
      do {
            echo "1. ".$row_rs['Field']."";
      } while ($row_rs = mysql_fetch_assoc($rs));
?>



<?php
      mysql_data_seek($rs, 0);        
      while ($row_rs = mysql_fetch_assoc($rs)) {
            echo "2. ".$row_rs['Field']."";
      }
?>
1

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now