I'm confused about the results I get from using mysqli_fetch_all()
and mysqli_fetch_array
.
I wrote a function to grab arrays from the primary key table that match the foreign key of another table where $field
is the desired field in the primary key table and $property
in the FK table.
function getProp($table,$field,$property,$cnn){
$findRow = "SELECT * FROM $table WHERE $field='$property'";
$res = mysqli_query($cnn,$findRow);
if ($res) {
$row = mysqli_fetch_array($res,MYSQLI_ASSOC);
return $row;
} else {
echo "we couldn't find the row that matched $field";
}
}
Now the idea is that there should be only one match found in the primary key table. So I thought replacing mysql_fetch_array()
with mysqli_fetch_all()
would be okay. However, when I invoke the function like so:
$profileRow = getProp('profile','id', $runner_id, $cnn);
$profileRow['question_text'];
$profileRow['avatar']`
I get the following error:
Undefined index: avatar
I would appreciate any help. I looked at the PHP manual, but don't understand why these MySQL actions yield different results in my case.
EDIT--
Thanks for your help, but I was specifically asking about what caused the error. Please explain to me as if I were a six year old - most technical explanations pass over my head. For example what does this mean?:
mysqli_fetch_all() should only be used in those situations where the fetched result set will be sent to another layer for processing.