Please help me

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shakeel
Forum Newbie
Posts: 12
Joined: Wed Jan 16, 2008 4:15 am

Please help me

Post by shakeel »

Hello to All

I m new to PHP and Mysql, I have a problem plz help me.
my question is how to display rows from result resource in second loop, my script is like:


$_parent_id=$_GET['parent_id'];

if(!isset($_parent_id) OR $_parent_id==0){
$_parent_id=0;
}

$sql="SELECT * FROM menu WHERE parent_id=".$_parent_id;
$result=mysql_query($sql);

// this loop returns all rows
while($RS=mysql_fetch_array($result)){
echo "<a href=menu.php?parent_id=".$RS['id'].">".$RS['name']."</a> :: ";
}

// this loop does not return anything (same result resource as above)
while($ROW=mysql_fetch_array($result)){
echo "<a href=menu.php?parent_id=".$ROW1['id'].">".$ROW1['name']."</a> <br> ";
}
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Please help me

Post by VladSun »

Instead of echoing use two separate string variables to store HTML in and after the loop echo them.

PS: Use intval() to sanitize the $_GET['parent_id'] value!
There are 10 types of people in this world, those who understand binary and those who don't
shakeel
Forum Newbie
Posts: 12
Joined: Wed Jan 16, 2008 4:15 am

Re: Please help me

Post by shakeel »

Thanks VladSun

But my question is why the same result resource does not return any row in the second loop,
or I have to query again to create new result resource?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Please help me

Post by VladSun »

From http://php.net/mysql_fetch_array
Description
array mysql_fetch_array ( resource $result [, int $result_type ] )

Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.
So, at the end of the loop the internal data pointer is at the end of the result data.
You may reset it by using:

Code: Select all

mysql_data_seek( $result );
There are 10 types of people in this world, those who understand binary and those who don't
shakeel
Forum Newbie
Posts: 12
Joined: Wed Jan 16, 2008 4:15 am

Re: Please help me

Post by shakeel »

Thanks VladSun
greate job!!!!!!!!!!!
it works
Post Reply