1

I have a Mysql query that gives the following result as using while($row = mysql_fetch_array($res))

x | 12432

y | 232432

z | 323423

I want to take each column and put it in a new array in order to export them in a table that would look like

x | y | z

12432 | 232432 | 323423

If I fetch the same query more than once, the second row does not show up.

Can you please help?

2

2 Answers 2

1

Edit: Switch to mysqli (or PDO) as soon as you can! Plenty of reasons can be found in searchengines, so im gonna leave that to you.


You can use a nested loop to do this:

$array = array();

while($row = mysql_fetch_array($res)){
    foreach($res as $key=>$value){
        $array[$key][] = $value;
    }
}

The first round of the while will give the $array the key-names and their first value, the next litterations through the loop will only add values

1

That is the code that worked for me.

while($row = mysql_fetch_array($res)){

    $clients[] = $row[0];

    $stats[] = $row[1];

}

foreach ($clients as $client)
{
    echo "<td>$client</td>";
}

echo "</tr><tr>";

foreach ($stats as $stat)
{
    echo "<td>$stat</td>";
}
1
  • However, there is a problem. It does not display the first entries in each array. Commented Nov 14, 2013 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.