|
Posted by Rik on 12/17/79 11:55
Petr Vileta wrote:
> Hmm, curious ;-)
> I'm using while loop in all other cases without error or warning.
> With one exception - both
> mysql_query() and mysql_fetch_array() is in the same program block.
> If i try this
>
> $result = mysql_query($query)
> $rows = mysql_num_rows($result);
> while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
> {
> echo $row["id"], "<br>";
> }
>
> I don't get warning. But if I put while() into function, I will get.
> IMHO this is because $result is "resource" type variable. But how to
> pass this type variable to function? Maybe I temporary switch-off
> warnings in function :-)
Nope, a resource can be handles here perfectly. The following code produces
no errors:
<?php
function check($arg){
while($row = mysql_fetch_assoc($arg)){
print_r($row);
}
}
$link = mysql_connect('localhost','root');
mysql_select_db('testbase',$link);
$result = mysql_query('SELECT * FROM tbl_test');
check($result);
?>
Judging by your output:
>
> Rows=3
> 1
> 2
> 3
> Warning: mysql_fetch_array(): 5 is not a valid MySQL result resource
> in F:\webpub\php\test.php on line ...
>
Do the 1\n2\n3 mean these rows are shown? If yes, did you give us the entire
code? It would seem a _second_ fetch is performed on the result that we're
not aware of. Could you show us the entire relevant code?
Grtz,
--
Rik Wasmus
Navigation:
[Reply to this message]
|