Page 1 of 1

some clarification with Databa communication using php,mysql

Posted: Tue Mar 10, 2009 8:28 am
by harishreddy
$query="select * from emp where ename='$ename' and empno='$empno'";
print "<br>".$query;
$result=mysql_query($query);
print "<br>".$result."<br>";
//print if($result);
if($result)
{
print_r(mysql_fetch_array($result));
while($row=mysql_fetch_array($result))
{
echo "This is from Stalinreddy";
echo "<br>".$row['ename'];
}

}


in the above code with in the if loop only one statement (i.e if we write print_r() first then it will excuted other wise whil)either print_r() or while loop is excuted ,(both are not excuted),can any body give me a explanation

Re: some clarification with Databa communication using php,mysql

Posted: Tue Mar 10, 2009 9:06 am
by JeffG
As the manual clearly states:
Remember that print_r() will move the array pointer to the end. Use reset() to bring it back to beginning.
Edit: No it's not that because you are calling mysql_fetch_array twice.

Try this (also from the manual):
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.