0

拜托,这是我第一次使用 php 尝试 mysqli。我正在尝试将结果打印到表格中,但表格中没有显示任何数据。

我之前试过

printf("%s %s\n", $c_id, $ctitle);

我得到了正确的结果。但我想要表格中的结果。

我打算这是一个详细信息页面,将链接到主页项目。我的代码:

<?php
$mysqli = new mysqli("localhost", "joseph", " ", "collectionsdb");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
    if ($stmt = $mysqli->prepare("SELECT c_id,ctitle,csubject,creference,cyear,cobjecttype,cmaterial,ctechnic,cwidth,cheight,cperiod,cmarkings,cdescription,csource,cartist,cfilename FROM collections ORDER BY c_id")) {   
    /* Execute the prepared Statement */
    $stmt->execute();

    /* Bind results to variables */
    $stmt->bind_result($c_id,$ctitle,$csubject,$creference,$cyear,$cobjecttype,$cmaterial,$ctechnic,$cwidth,$cheight,$cperiod,$cmarkings,$cdescription,$csource,$cartist,$cfilename);

?>  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB">
<body>  

<?php
    /* fetch values */
    while ($rows = $stmt->fetch()) {

    // print image and data into a html table ?? 
?>
    <table border="1" align="left">
<tr>
    <td rowspan=16> <?php echo '<img src="./images/'.$rows['cfilename'].'" width="300" height="400" />';  ?> </td>
</tr>

<tr><td>ID</td><td><?php echo $rows['c_id']; ?></td></tr>
<tr><td>TITLE</td><td><?php echo $rows['ctitle']; ?></td></tr>
<tr><td>SUBJECT</td><td><?php echo $rows['csubject']; ?></td></tr>
<tr><td>REFERENCE No.</td><td><?php echo $rows['creference']; ?></td></tr>
<tr><td>YEAR</td><td><?php echo $rows['cyear']; ?></td></tr>
<tr><td>OBJECT TYPE</td><td><?php echo $rows['cobjecttype']; ?></td></tr>
<tr><td>MATERIAL USED</td><td><?php echo $rows['cmaterial']; ?></td></tr>
<tr><td>TECHNIC</td><td><?php echo $rows['ctechnic']; ?></td></tr>
<tr><td>WIDTH</td><td><?php echo $rows['cwidth']; ?></td></tr>
<tr><td>HEIGHT</td><td><?php echo $rows['cheight']; ?></td></tr>
<tr><td>PERIOD</td><td><?php echo $rows['cperiod']; ?></td></tr>
<tr><td>MARKINGS</td><td><?php echo $rows['cmarkings']; ?></td></tr>
<tr><td>DESCRIPTION</td><td><?php echo $rows['cdescription']; ?></td></tr>
<tr><td>SOURCE</td><td><?php echo $rows['csource']; ?></td></tr>
<tr><td>ARTIST</td><td><?php echo $rows['cartist']; ?></td></tr>
</table>

<?php
    }

    /* Close the statement */
    $stmt->close();
}
else {
    /* Error */
    printf("Prepared Statement Error: %s\n", $mysqli->error);
}

/* close our connection */
$mysqli->close();
?>

谢谢你。

约瑟夫

4

1 回答 1

0

您必须使用之前绑定的变量而不是 $rows 数组 - 即 $c_id,$ctitle,$csubject
您还放错了<table>标签。将其移出循环。

如您所见,mysqli 对于准备好的语句是非常不可用的。
将其与手动占位符一起使用或移至 PDO。
例如,它将使您的代码清洁(这是您需要的所有代码):

<?php
$db = new DB();
$data = $db->getAll("SELECT * FROM collections ORDER BY c_id");
?>  
<table border="1" align="left">
<?php foreach ($data as $rows) { ?>
  <tr>
    <td rowspan=16>
      <img src="/images/<?php echo $rows['cfilename'] ?>" width="300" height="400" />
    </td>
  </tr>
  <tr><td>ID</td><td><?php echo $rows['c_id']; ?></td></tr>
  <tr><td>TITLE</td><td><?php echo $rows['ctitle']; ?></td></tr>
  <tr><td>SUBJECT</td><td><?php echo $rows['csubject']; ?></td></tr>
  <tr><td>REFERENCE No.</td><td><?php echo $rows['creference']; ?></td></tr>
  <tr><td>YEAR</td><td><?php echo $rows['cyear']; ?></td></tr>
  <tr><td>OBJECT TYPE</td><td><?php echo $rows['cobjecttype']; ?></td></tr>
  <tr><td>MATERIAL USED</td><td><?php echo $rows['cmaterial']; ?></td></tr>
  <tr><td>TECHNIC</td><td><?php echo $rows['ctechnic']; ?></td></tr>
  <tr><td>WIDTH</td><td><?php echo $rows['cwidth']; ?></td></tr>
  <tr><td>HEIGHT</td><td><?php echo $rows['cheight']; ?></td></tr>
  <tr><td>PERIOD</td><td><?php echo $rows['cperiod']; ?></td></tr>
  <tr><td>MARKINGS</td><td><?php echo $rows['cmarkings']; ?></td></tr>
  <tr><td>DESCRIPTION</td><td><?php echo $rows['cdescription']; ?></td></tr>
  <tr><td>SOURCE</td><td><?php echo $rows['csource']; ?></td></tr>
  <tr><td>ARTIST</td><td><?php echo $rows['cartist']; ?></td></tr>
<?php } ?>
</table>
于 2013-02-12T05:50:56.117 回答