0

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.

Edited by DILO3D: n/a

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by DILO3D
1

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

Edited by tnjiric: n/a

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.