Jump to content

Weird result with mysql_fetch_array


rshadarack

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.