I have a function which should get data from a MySQL database with prepared statements and put it in an array, and I have a piece of code which should get the data from that function and put it in a table. The two pieces of code are below.
The function
function list_students($connect) {
if($stmt = mysqli_prepare($connect, "SELECT id,fullname,gender,dateofbirth,passed FROM students ORDER BY fullname ASC LIMIT 0, 20")) {
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $fullname, $gender, $dateofbirth, $passed);
}
$results = array();
while(mysqli_stmt_fetch($stmt)) {
$results['id'] = $id;
$results['fullname'] = $fullname;
$results['gender'] = $gender;
$results['dateofbirth'] = $dateofbirth;
$results['passed'] = $passed;
}
return $results;
}
The code which should get data from the function
<?php
$list = list_students($connect);
foreach($list as $variable) {
?>
<tr>
<td><? echo $variable["id"]; ?></td>
<td><? echo $variable["fullname"]; ?></td>
<td><? echo $variable["gender"]; ?></td>
<td><? echo $variable["dateofbirth"]; ?></td>
<td><? echo $variable["passed"]; ?></td>
</tr>
<?php
}
?>
I can imagine I do something terribly wrong, because the data I get with this code is far from what's in the database. It's important to know that I have three rows in the table student
, and the code above adds five rows to the table.
Can someone help me out with this, or put me in the right direction?
I hope I have explained it clear enough. If I didn't, please let me know so I can improve my question.
Cheers
EDIT:
I tried to use this, but mysqli_prepare
doesn't support fetch_array
as far as I know.