Page 1 of 1

Please help me

Posted: Thu Jan 17, 2008 5:32 am
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> ";
}

Re: Please help me

Posted: Thu Jan 17, 2008 5:36 am
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!

Re: Please help me

Posted: Thu Jan 17, 2008 6:07 am
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?

Re: Please help me

Posted: Thu Jan 17, 2008 6:10 am
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 );

Re: Please help me

Posted: Thu Jan 17, 2008 6:49 am
by shakeel
Thanks VladSun
greate job!!!!!!!!!!!
it works