Tuesday, 15 June 2010

php - mysqli_fetch_assoc returns duplicate data -



php - mysqli_fetch_assoc returns duplicate data -

there similar questions here, after lot of searching , trying different things, i'm still stuck.

when using mysqli_fetch_assoc() populate array, each row beingness added twice.

$query = "select column table year = 2012"; if ($result = mysqli_query($connect, $query)) { while ($row = mysqli_fetch_assoc($result)) { $data[] = $row["column"]; } echo implode(',', $data); mysqli_free_result($result); }

this returning following: 1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10

i'm expecting 1,2,3,4,5,6,7,8,9,10

i added few lines debugging...

print_r($data) yields

array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 [10] => 1 [11] => 2 [12] => 3 [13] => 4 [14] => 5 [15] => 6 [16] => 7 [17] => 8 [18] => 9 [19] => 10 )

print_r($result); yields

mysqli_result object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 10 [type] => 0 )

if [num_rows] => 10, , i'm using mysqli_fetch_assoc() , not mysqli_fetch_array(), why adding values array twice?

probably you've assigned info $data array. seek empty before while instruction:

$query = "select column table year = 2012"; if ($result = mysqli_query($connect, $query)) { $data = array(); while ($row = mysqli_fetch_assoc($result)) { $data[] = $row["column"]; } echo implode(',', $data); mysqli_free_result($result); }

and see outputs.

i believe column not real column name otherwise you'll error.

php mysql mysqli fetch

No comments:

Post a Comment