Member Avatar for DILO3D

I need to use the mysql query result.following is the mysql query.

while($row = mysql_fetch_array($result)) { $name=$row['name']; }

For example: result of above query is (name1,name2,name3...)


The problem is...
i need to return that result in the following function.How can i do it?

function results() { return array( 'name1' => t('name1'), 'name2' => t('name2'), 'name3' => t('name3'), ); }

plz anyone help me.

Member Avatar for tnjiric

First if I understand good, as a result you want to return ALL the names from the query.

If that is the case, in this first while loop, instead of using $name = $row;,use:

$names = array(); while($row = mysql_fetch_array($result)) { array_push($names,$row['name']); }

This code will push the names from the query automatically to the array called $names.
And for example after 3 runs through the for loop, $name array would look like this:

$names = array([0] => "name1", [1] => "name2", [2] => "name3");

Of course for example to retrieve first name from the list you would type:

echo $names[0];

And returning the entire array with names now through the function is easy:

function returnNames() { return $this->names; }

this would return to you the array with all the names from the query in one array.

Hope I helped you a bit with this,

Best regards,

Toni

Member Avatar for DILO3D

Thanks a Lot.It helps me.

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.