Reputation: 2210
I'm not sure if this appropriate on here since it is more of a understanding issue on my part but I''m curious to know why I receive this output:
Response id: 3 Question id: 1 Question: What is my middle name? Title: How Well Do You Know Michael
Array ( [0] => Array ( [0] => 3 [r_id] => 3 [1] => 1 [question_id] => 1 [2] => What is my middle name? [question] => What is my middle name? [3] => How Well Do You Know Michael [title] => How Well Do You Know Michael ) )
when I run this PHP script:
// Grab the response data from the database to generate the form
$query = "SELECT qr.response_id AS r_id, qr.question_id, q.question, quiz.title " .
"FROM quiz_response AS qr " .
"INNER JOIN question AS q USING (question_id) " .
"INNER JOIN quiz USING (quiz_id) " .
"WHERE qr.user_id = '" . $_SESSION['user_id'] . "'";
$data = mysqli_query($dbc, $query) or die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $query");
$questions = array();
while ($row = mysqli_fetch_array($data)) {
echo 'Response id: ' . $row['r_id'] . 'Question id: ' . $row['question_id'] . ' Question: ' . $row['question'] . ' Title: ' . $row['title'] . '<br />';
array_push($questions, $row);
}
print_r($questions);
It seems as though there are two entries for each piece of data. For example, there are two entries of the 'Response id' under index [0] and [r_id]. Both equal 3. Is it a delirious fear of mine worrying about duplicated data in my array? I'm thinking of the future when I'm doing 10's of queries at a time and whether this will affect speed or accuracy. Any input is greatly appreciated!
Upvotes: 2
Views: 192
Reputation: 1119
Reading http://php.net/manual/en/function.mysql-fetch-array.php, it states that the returned value of mysqli_fetch_array()
(which works the same way as mysql_fetch_array()
) contains both numerically indexed row data as well as field name indexed row data. If you just want field name indexed data, use mysqli_fetch_assoc()
.
Upvotes: 2
Reputation: 2585
Taken straight out of documentation:
Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows. The type of returned array depends on how result_type is defined. By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).
So changing this:
while ($row = mysqli_fetch_array($data)) {
To:
while ($row = mysqli_fetch_array($data, MYSQL_ASSOC)) {
This will return only string keys or alternatively, you could use mysqli_fetch_assoc
Upvotes: 2
Reputation: 12537
If you check out the documentation of mysqli_fetch_array
you see the second parameter $result_type
which is by default set to MYSQL_BOTH
.
That means both the numerical field index (MYSQL_NUM
) and the field name (MYSQL_ASSOC
) are used as array-index. So if you only need the field name indices you have to use it like that:
while ($row = mysqli_fetch_array($data, MYSQL_ASSOC)) {
// ....
Upvotes: 4
Reputation: 1593
This behaviour is excpected.
From http://php.net/manual/en/mysqli-result.fetch-array.php
mysqli_fetch_array() is an extended version of the mysqli_fetch_row() function. In addition to storing the data in the numeric indices of the result array, the mysqli_fetch_array() function can also store the data in associative indices, using the field names of the result set as keys.
Upvotes: 4