PDA

View Full Version : Problem with passing query results to a function


AsscBB
06-24-2008, 02:25 PM
Please check my process and help me understand where I'm going wrong.

I'm taking the results from a query and passing them to a function where I'm deconstructing the info in a while loop and running conditionals. Is this the right way to do this? I have comments in the code for what I think the code is doing (or at least what I want it to do) so I can also put this in my cheat sheet for future use. The code is not currently providing the output I desire. The problem I'm seeing is that I don't think the while loop is scanning through the array like I assume it is. Am I going crazy???


Thanks!




function check_fields($my_values) //this is my custom function with my array as a pass parameter. The name does not need to be the same but I'm leaving it the same for sake of simplicity
{
while ($my_value=$db->fetch_array($my_values) //this while loop goes through the array entry by entry. If there were 10 instances of field5 = $some_var from earlier, this code would be executed 10 times - once for each entry
{
if($my_value['field1'] == $field1_var) //This goes to the array and runs a conditional against "field1". The name of the fields here corresponds with the name of the fields in the origional table
{
//Do something for this if true
}
if($my_value['field2'] == $field2_var) //Same as before but for a different table
{
//Do something for this if true
}
if($my_value['field3'] == $field3_var)
{
//Do something for this if true
}
if($my_value['field4'] == $field4_var)
{
//Do something for this if true
}
if($my_conditional == true)
{
return false; //if $my_conditional = true then retun a value of "false" to the calling function
}
else
{
return true; //if $my_conditional = false then retun a value of "true" to the calling function
}
}
}


$my_values=$db->query("SELECT 'field1','field2','field3','field4' FROM " . TABLE_PREFIX . "demo_table WHERE field5=".$some_var);
//$my_values is now an array that is 4 fields wide and has as many rows as the number of occurances of field5 = $some_var
if (check_fields($my_values)) //This sends the $my_values array to a custom function and expects a true or false as a return value
{
//Do something
}

MoT3rror
06-24-2008, 02:27 PM
You forgot

global $db;

in your function.

Opserty
06-24-2008, 07:27 PM
Why are you putting your while inside the function? It makes more sense to loop through and apply the function to each array returned by the mysql.

AsscBB
06-24-2008, 07:41 PM
I tried to boil the code down to support asking a general question. The reason I'm doing the while inside the function, is becuase thats where the data is being used, and calling the function and just passing those parameters doesn't work with my actual application which is calling the function with parameters which were omitted in this example/question. Suffice it to say, it makes the most sense for my application to pass the array as a parameter and deconstruct it in a while loop inside the function if that will work in php. I'm trying to minimize database queries and function calls, and while this does sound kinda kludgy, if the sytax allows, it works for my application.

Marco van Herwaarden
06-25-2008, 08:26 AM
What type of variable is $my_values? It seems to be an array.

while ($my_value=$db->fetch_array($my_values) //this while loop goes through the array entry by entry. If there were 10 instances of field5 = $some_var from earlier, this code would be executed 10 times - once for each entry
$db->fetch_array() needs a MySQL resource as parameter, not a simple array.

AsscBB
06-25-2008, 12:22 PM
$my_values is an array build from a mySQL query before being passed into the function. I was looking at other code examples and saw similar examples of populating the array with a mySQL query, then useing fetch_array in a while loop to scan through the array.

I've added the step of passing the array as a parameter to a function and deconstructing there. I've been unable to work on my board the past few days so i don't know if this will actually work, but this was a problem I was having trouble with when I last had time to work on it, and I'm trying to learn how to do this so I don't get stumped in 5 minutes next time I work on it...

Thanks for the help!

Marco van Herwaarden
06-25-2008, 01:29 PM
$my_values must be a valid MySQL resource if used with fetch_array().

Maybe you should post more code then only this function.

AsscBB
06-25-2008, 08:11 PM
The query where $my_values is populated and the call to the function is located in the chunk of code below the function. Prior to that point, $my_values does not exist.


$my_values=$db->query("SELECT 'field1','field2','field3','field4' FROM " . TABLE_PREFIX . "demo_table WHERE field5=".$some_var); // $my_values is now an array that is 4 fields wide and has as many rows as the number of occurances of field5 = $some_var
if (check_fields($my_values))
// This sends the $my_values array to a custom function and expects a true or false as a return value
{
//Do something
}

I'm positive that I've included all of the code to figure out if what I'm trying is possible or not, anything else would just be noise. If "valid MySQL resource" means something other than this piece of code, then I'm afraid you've lost me.

Sorry I missed the php tags in the original post and grabbed the code ones instead.


Thanks again for your help and patience!

Marco van Herwaarden
06-26-2008, 07:49 AM
Actually that is a resource, not an array. You will however need to pass the resource by reference, not by value.

AsscBB
06-26-2008, 06:51 PM
That may explain why I couldn't get it to work going through the array. I've gotten some ideas from "passing references" over on php.net to try now thanks to that suggestion and I'll see where that gets me. I'll post back with results my next chance to play!

Thanks Marco!

Marco van Herwaarden
06-27-2008, 08:32 AM
All that would be needed is changing:
function check_fields($my_values) //this is my custom function with my array as a pass parameter. The name does not need to be the same but I'm leaving it the same for sake of simplicity

to:function check_fields(&$my_values) //this is my custom function with my array as a pass parameter. The name does not need to be the same but I'm leaving it the same for sake of simplicity

More info:
References Explained - Manual (http://nl3.php.net/manual/en/language.references.php)
Passing by Reference - Manual (http://nl3.php.net/manual/en/language.references.pass.php)

AsscBB
06-27-2008, 10:05 PM
I had tried that and it still wasn't working as expected. If I restructure it to not call a function and just do it in my main, it seems to work, so I'll probably just do it that way. I'll probably come back to it to see if I can get it to work by passing it later, but I'd rather work throught the rest of my logic right now... That can always be compartmented later!

Thanks again for the help! Hopefully I'll get it working right!