dnatal
asked on
PHP function parameter call by reference--interesting 'feature'?
Running PHP 5.1 on an Apache 2.2.6 (localhost with appropriate DB permissions) on a windows XP home machine (my laptop). Function foo(in the snippet) is an implementation of an abstract object from a child object. Function get_query_resource() returns a query resource.
The expected result of the attached code snippet, for a resource with five rows, should be
Bar calls foo.
Now in foo.
Query resource rows: 5
Processing query_resource row
Processing query_resource row
Processing query_resource row
Processing query_resource row
Processing query_resource row
Foo completed successfully.
But what I'm actually getting is
Bar calls foo.
Now in foo.
Query resource rows: 5
Foo completed successfully.
The code in the while loop isn't executing.
However, if I don't pass the query resource in bar (ie, if(foo())...) then bar gets the query resource and finishes normally. Why is this?
The expected result of the attached code snippet, for a resource with five rows, should be
Bar calls foo.
Now in foo.
Query resource rows: 5
Processing query_resource row
Processing query_resource row
Processing query_resource row
Processing query_resource row
Processing query_resource row
Foo completed successfully.
But what I'm actually getting is
Bar calls foo.
Now in foo.
Query resource rows: 5
Foo completed successfully.
The code in the while loop isn't executing.
However, if I don't pass the query resource in bar (ie, if(foo())...) then bar gets the query resource and finishes normally. Why is this?
function foo(&$query_resource=NULL){
echo "Now in foo<BR>";
if(is_null($query_resource)) $query_resource = get_query_resource();
if(!$query_resource) exit("SQL error");
echo "Query resource rows: ".mysql_num_rows($query_resource);
while($row = mysql_fetch_assoc($query_resource)){
echo "Processing query_resource row<BR>";
}
return true;
}
function bar(&$q_r=NULL){
echo "Bar calls foo.<BR>";
if(foo($q_r)) echo "Foo completed successfully.<BR>";
}
bar();
thats because in this case $query_resource === null and thus the result of get_query_resource() is used which might be an unfetched result. If you pass something to bar this resultset might be already read and thus does not contain any more rows.
ASKER
Testing shows that the contents of the if(is_null($query_resource )) conditional do not execute--I put an echo in there (something like echo "No resource found; getting resource") and I don't see that on the output.
ASKER
How do I determine if the resultset has been read and thus doesn't contain any rows? Wouldn't the mysql_num_rows() return 0?
try:
if ($query_resource === FALSE) {
// error
}
if ($query_resource === FALSE) {
// error
}
ASKER
rob263, how about line 4
if(!$query_resource) exit("SQL error");
since the negation of the boolean false (or, actually, a NULL that should have previously been detected, or a 0), should return true.
if(!$query_resource) exit("SQL error");
since the negation of the boolean false (or, actually, a NULL that should have previously been detected, or a 0), should return true.
Yeah, you're right - my comment was premature.
after "echo "Query resource rows:...", try putting:
var_dump(mysql_fetch_assoc ($query_re source));
and see what that value is. It might give you a hint as to why the while loop isn't executing.
var_dump(mysql_fetch_assoc
and see what that value is. It might give you a hint as to why the while loop isn't executing.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That would do it.
ASKER
Because I was executing multiple calls to foo() in bar() the first call ate my query and didn't leave anything for the others to process. That'd cause the observed problem.