i have a table with the following fields:
row_id
first_field
second_field
row_id is of type integer, is set to auto increment and is the primary key. the other two fields are of type text.
the table is populated up to five rows. as such, the values for row_id are 1, 2, 3, 4 and 5.
i also have another table of a similar structure that has a one-to-many correspondence with my first table.
something weird happens though when i do a select query and feed the result to mysql_fetch_array.
when i run this:
$query = "select a.*, b.* from table1 as a
left join table2 as b on a.row_id = b.row_id";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo '<pre>'; print_r($row); echo '</pre>';
}
i get this:
Array
(
[0] => 1
[row_id] => 1
[1] => some text
[first_field] => some text
[2] => some text
[second_field] => some text
}
Array
(
[0] => 2
[row_id] => 2
[1] => some text
[first_field] => some text
[2] => some text
[second_field] => some text
}
Array
(
[0] => 3
[row_id] =>
[1] => some text
[first_field] => some text
[2] => some text
[second_field] => some text
}
Array
(
[0] => 4
[row_id] =>
[1] => some text
[first_field] => some text
[2] => some text
[second_field] => some text
}
Array
(
[0] => 5
[row_id] =>
[1] => some text
[first_field] => some text
[2] => some text
[second_field] => some text
}
on each array result, i'd like to direct your attention to the first field, row_id. in the first two arrays, index 0 and row_id have the same values, whereas in the subsequent three arrays, only index 0 has a value. row_id appears to be null.
this is the first time i've ever encountered something like this. what is causing this? how can this be fixed?
thanks!
row_id
yet your array referencesthread_id
. Confuse. – Blake Apr 5 '12 at 15:20a.*, b.*
. Since both tables haverow_id
, the fetch call is overwriting array keys. – Michael Berkowski Apr 5 '12 at 15:35