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
Solved

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

Posted on 2009-04-06
2
592 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
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
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 dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 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