Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
You should try to use mysqli or PDO for your database connections. –  Fyntasia Nov 13 '13 at 16:02
2  
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. –  h2ooooooo Nov 13 '13 at 16:07

2 Answers 2

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

share|improve this answer

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>";
}
share|improve this answer
    
However, there is a problem. It does not display the first entries in each array. –  Harris Geo Nov 14 '13 at 11:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.