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!

Recommended Answers

All 2 Replies

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.