简体   繁体   中英

PHP bind param to SQL query and loop through and print results

I'm currently having issues while trying to print a html table of SQL query results.

I currently have an sql query which requires an 'sid' which is stored as a local variable.

I'm trying to create a prepared statement which binds the $sid var to the query and create a while loop to loop through the data and print the results into a table.

Below, I have tried to create the code but it does not yet seem to work. Any help would be appreciated, Thanks

$sid = '166410';

    $query =  "SELECT enrl.ayr, enrl.status, prog.ptitle, enrl.lvl
               FROM enrl, prog
               WHERE enrl.sid =?
               AND enrl.pid = prog.pid
               ORDER BY lvl DESC";

$scap = '';

if ($st = mysqli_prepare($link, $query)) {

    mysqli_stmt_execute($st);

    mysqli_stmt_bind_param($st, "s", $sid);

    mysqli_stmt_bind_result($st, $ayr, $status, $ptitle, $lvl);

    while (mysqli_stmt_fetch($st)) {
            $scap .= "
                  <table id=\"test\" style=\"width:100%\">
                      <tr>
                          <td> " . $ayr . " </td>
                          <td> " . $status . "</td>
                          <td> " . $ptitle . "</td>
                          <td> " . $lvl . "</td>
                      </tr>
                  </table>
              ";
            } $st->free();
        mysqli_stmt_close($st);
    }
    mysqli_close($link);

    print($scap);
<?php

$sid = '166410';
$query = "SELECT enrl.ayr, enrl.status, prog.ptitle, enrl.lvl
          FROM enrl, prog
          WHERE enrl.sid =?
          AND enrl.pid = prog.pid
          ORDER BY lvl DESC";
$scap = '';
$st = mysqli_prepare($link,$query);
mysqli_stmt_bind_param($st, "s", $sid);
mysqli_stmt_execute($st);
mysqli_stmt_bind_result($st, $ayr, $status, $ptitle, $lvl);
while (mysqli_stmt_fetch($st)) {
    $scap .= "
          <table id=\"test\" style=\"width:100%\">
              <tr>
                  <td> " . $ayr . " </td>
                  <td> " . $status . "</td>
                  <td> " . $ptitle . "</td>
                  <td> " . $lvl . "</td>
              </tr>
          </table>
          ";
}
echo $scap;
mysqli_stmt_close($st);
mysqli_close($link);
?>

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