0

Is it possible to pass arrays into a function and/or return an array from a function? I think it's possible but I don't really know how to go about it.

lets say I have a function that returns a bunch of field from a database

ex:

function a($sql){

$result = mysql_query($result);

/*should I do this...
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
     return $row;
}*/

/*or this*/
$row = mysql_fetch_array($result, MYSQL_ASSOC)
return $row;

}

and how do I call a function if it returns an array? should I do something like this?

$get_stuff= a($query);
foreach($get_stuff as $get_stuffs){
     //do stuff
}

and on another note... how do I handle an array after passing it to a function?

lets say I have something like this

$args=array('field1'=>'value',
            'field2'=>'value');

$query = $get_query($args);

function $get_query($array){
     foreach($array as $str){
         //form a string that will be accepted into mysql.
     }

     return str;
}

The reason I'm asking all these questions is because I'm tired of manually writing code over and over again when getting data from mysql, so I'm trying to create a class that'll make it easier for me.

thanks to anyone who is willing to help!

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by qazplm114477
1

That code is correct but keep note that the first code snippet function only returns a single row. Then loops through the columns of that row in the second code snippet and the third function is a dynamic function which it's name is $get_query as pre-defined.

0

That code is correct but keep note that the first code snippet function only returns a single row. Then loops through the columns of that row in the second code snippet and the third function is a dynamic function which it's name is $get_query as pre-defined.

thanks, the $get_query was a typo on my part. it was just supposed to be get_query(). I'll try building a class with this logic and post it here when I'm done. thanks again.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.