拜托,这是我第一次尝试使用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();
?>
谢谢。
约瑟夫
答案 0 :(得分: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>