Solved

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

Posted on 2009-04-06
2
609 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
shobinsun earned 500 total points
ID: 24075925
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
ID: 31566955
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

691 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