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!
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)) {
// ....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With