ok well I have written a function that is supposed to take the sql you give it an return an array for you to output. the problem is getting the array becasue the sql is different every time so I get an array of the column names names like so.

for($i=0; $i < mysqli_num_fields($result); $i++)
{
$row = mysqli_fetch_field($result);
$values[] =$row->name;

}

so that puts all the names in an array but what I do next I have no clue usually with my sql I know the row names so I create a while loop to put the values in an array like this

while( $query = mysqli_fetch_array($result))
{
$gather[] = array('id' => $query['id'], 'name' => $query['name']);
$i++;
}

so the problem as you can see is how do I also get the variable names in that array?
if its dynamic.
i want to be able to do something like

for($i=0; $i < mysqli_num_fields($result); $i++)
{
$row = mysqli_fetch_field($result);
$values[] =$row->name;
$values2[] = $query['$row->name'];

}

but you can't just use $query without it being set at a varaible and if it gets set as a variable then it's not just $query anymore so how do go about getting this dynamic array?

Recommended Answers

All 6 Replies

I don't understand the problem. You know how you can get column names using mysql_field_name(). You also know how to get the field content using mysql_result. Put both together to create any array you want. For example (not tested):

for ($i = 0; $i < mysql_num_fields($result); $i++)
  $columnNames[] = mysql_field_name($result, $i);
for ($r = 0; $r < mysql_num_rows($result); $r++)
  for ($i = 0; $i < sizeof($columnNames); $i++)
    $myResultTable[$r][$columnNames[$i]] = mysql_result($r, $i);

It that's not what you want, how would your desired result look like?

what I wanted but I have one problem mysql_result does not work! and my desired result in a array form would be

array(array(id=>6 name=>something text=>some sample stuff)array(id=>7 name=>example text=>daniweb.com)

something to that effect

What does "does not work" mean? If the function mysql_result() does not work in your installation, you might have to start with a new system.
With a closer look you might have noticed that I missed a parameter in mysql_result. Try mysql_result($result, $r, $i) , then my code should give you the desired array.

$member = array();
while( $query = mysqli_fetch_array($result))
{
array_push($member,query['name']);
}
$member = implode(',',$member');
//all names now store in $member
//explode if you want to extract
$member = explode(',',$member');
for($x=0; $x<count($member); $x++){
$name = $member[$x];
//do something or query
}
$t = mysql_query("select Id,Name,Text from table");
$a= array();
while($r=mysql_fetch_array($t)){
$b = "ID:"$r['Id']." Name:".$r['Name']." Text:".$r['Text'];
array_push($a,$b);
}
$a = implode(',',$a);
echo $a;
//will display ID: 1 Name:uncle sam Text:abcdefg,ID: 2 Name:watever Text:daniweb
//explode it to extract record
$a = explode(',',$a);

i try it..it work...hope it work in youre script

the code works great but I was wondering if it is possible to write that code with the mysqli syntax

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.