Jump to content

Archived

This topic is now archived and is closed to further replies.

rshadarack

Weird result with mysql_fetch_array

Recommended Posts

I'm using the following code to save a table to a matrix:

[code]function tableToArray($tableName)
{
        $table = mysql_query("SELECT * FROM $tableName");
        $columns = mysql_query("SHOW COLUMNS FROM $tableName");

        for($i = 0; $i < mysql_num_rows($columns); $i++) {
                $result[0][] = mysql_result($columns, $i);
        }
        $n = 1;
        while ($row = mysql_fetch_array($table)) {
                for ($x = 0; $x < sizeof($row)/2; $x++) {
                        $result[$n][] = $row[$x];
                }
                $n++;
        }
        return $result;
}[/code]

Which works fine.  But notice in the last for loop (nested in the while loop), I have $x < sizeof($row)/2.  I did this because I found that sizeof was always returning twice the amount of columns that I had.  Before that, I was using a foreach statement, but it was saving data values to my matrix like:

dataVal1 | dataVal1 | dataVal2 | dataVal2 | dataVal3 | dataVal3....

Doubling each value.  I was wondering if anyone knew why this was happening.

Share this post


Link to post
Share on other sites
Use mysql_fetch_assoc. mysql_fetch_array returns two lots of the same results one with associative indices and the other number indices

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.