rshadarack Posted August 7, 2006 Share Posted August 7, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/16804-weird-result-with-mysql_fetch_array/ Share on other sites More sharing options...
wildteen88 Posted August 7, 2006 Share Posted August 7, 2006 Use mysql_fetch_assoc. mysql_fetch_array returns two lots of the same results one with associative indices and the other number indices Quote Link to comment https://forums.phpfreaks.com/topic/16804-weird-result-with-mysql_fetch_array/#findComment-70712 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.