Jump to content


Photo

Weird result with mysql_fetch_array


  • Please log in to reply
1 reply to this topic

#1 rshadarack

rshadarack
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 07 August 2006 - 02:45 PM

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

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;
}

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.

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 August 2006 - 02:49 PM

Use mysql_fetch_assoc. mysql_fetch_array returns two lots of the same results one with associative indices and the other number indices




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users