Solved

PHP function parameter call by reference--interesting 'feature'?

Posted on 2008-06-10
10
1,076 Views
Last Modified: 2013-12-13
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?
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();

Open in new window

0
Comment
Question by:dnatal
  • 5
  • 3
  • 2
10 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 21755109
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.
0
 
LVL 2

Author Comment

by:dnatal
ID: 21755151
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.
0
 
LVL 2

Author Comment

by:dnatal
ID: 21755167
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?
0
 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21755238
try:

if ($query_resource === FALSE) {
   // error
}
0
 
LVL 2

Author Comment

by:dnatal
ID: 21755268
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21755277
Yeah, you're right - my comment was premature.
0
 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21755299
after "echo "Query resource rows:...", try putting:

     var_dump(mysql_fetch_assoc($query_resource));

and see what that value is.  It might give you a hint as to why the while loop isn't executing.
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 375 total points
ID: 21755303
> Wouldn't the mysql_num_rows() return 0?

no it returns the numbers of records in the resultset and not the number of records still readable in the result set.
0
 
LVL 2

Author Closing Comment

by:dnatal
ID: 31465935
That would do it.
0
 
LVL 2

Author Comment

by:dnatal
ID: 21755339
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Passing variables to stored procedure 3 35
mysqli 3 21
Amazon Redshift 2 26
test if query has no results 2 19
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now