在 php 代码中,我试图检索数据库中一行的值。我希望我的查询是安全的,所以我使用 mysqli 和准备好的语句。但是,在获取行并尝试回显结果后,无论我回显什么,我都会得到空值。可能是什么原因?
下面是我的代码中获取并回显结果的部分。
// get value of id that sent from address bar
$id=$_GET['id'];
////$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$sql = $mysqli -> prepare("SELECT topic, detail, name, email, datetime FROM $tbl_name WHERE id=?");
$sql -> bind_param ("s", $id);
$sql -> execute();
/* store result */
// $sql->store_result();
// echo "NUMBER IS : " . "$sql->num_rows";
$sql -> bind_result($topic, $detail, $name, $email, $datetime);
$rows = $sql -> fetch();
////$result=mysql_query($sql);
////$rows=mysql_fetch_array($result);
$sql -> close();
?>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="30%" align="left" bgcolor="#FFFFFF"><IMG name="pictureposition1" border="0" alt="" SRC="images/concordia_logo.jpg" ><br></td>
<td width="30%" colspan="5" align="center" bgcolor="#FFFFFF"><h1><font COLOR=#800517>PHP Forum</font></h1> </td>
<td width="30%" colspan="5" align="right" bgcolor="#FFFFFF"><h3><font COLOR=#800517>CMPE and SOFE 495 Computer Security</font></h3> </td>
</tr>
<tr>
<td><font COLOR="#FFFFFF">.</font></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bordercolor="1" bgcolor="#FFFFFF">
<tr>
<td bgcolor="#F8F7F1"><strong><?php echo $rows['topic']; ?></strong></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><?php echo $rows['detail']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>By :</strong> <?php echo $rows['name']; ?> <strong>Email : </strong><?php echo $rows['email'];?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Date/time : </strong><?php echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>
<BR>