简体   繁体   中英

mysqli prepare not returning results in table

I am attempting to post each row returned from a prepared statement as an individual table row in custom format - unfortunately I have had poor success with prepared statements in the past and I would like to write this to the current standard-

the below code does not echo any rows onto the table.

Any help is appreciated

$link = mysqli_connect("host", "user", "password", "db");
if ($stmt = mysqli_prepare($link, "SELECT * FROM users WHERE currentemp=1")) {
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $result);
mysqli_stmt_fetch($stmt);
foreach ($result as $row) {
//print data
echo("<tr><td><input type='checkbox' name='emp". $row['UID'] ."'></td><td>". $row['Fname'] ." ". $row['Lname'] ."</td><td>". $row['hiredate'] ."</td><td>" .$row['certlevel'] ."</td><td>". $row['email'] ."</td><td>". $row['phonenumb'] ."</td></tr>");
}

} else {
echo(" <tr><td colspan=5>No employees found</td></tr>");
}
mysqli_stmt_close($stmt);

Note that, according to the documentation , mysqli_stmt_bind_result() "binds columns in the result set to variables."

Also mysqli_fetch() only returns one result row. You'll want to loop through the result set with a while loop.

$link = mysqli_connect("localhost", "root", "Ddsmp094", "dmt"); 

if ($stmt = mysqli_prepare($link, "SELECT UID, Fname, Lname, hiredate, certlevel, email, phonenumb FROM users WHERE currentemp=1")) {
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $UID, $Fname, $Lname, $hiredate, $certlevel, $email, $phonenumb);
while(mysqli_stmt_fetch($stmt)) {
    //print data
    echo("<tr><td><input type='checkbox' name='emp". $UID ."'></td><td>". $Fname ." ". $Lname ."</td><td>". $hiredate ."</td><td>" .$certlevel ."</td><td>". $email ."</td><td>". $phonenumb ."</td></tr>");
}
} else {
echo(" <tr><td colspan=5>No employees found</td></tr>");
}
mysqli_stmt_close($stmt);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM