-5

下面的代码结合我的数据库将一个图像呈现给浏览器,我想显示用户的所有图像,我应该如何修改下面的代码?

$coverpic = "";
$sql = "SELECT filename FROM photos WHERE user='$u'";
$query = mysqli_query($db_conx, $sql);
if(mysqli_num_rows($query) > 0){
    $row = mysqli_fetch_row($query);
    $filename = $row[0];
    $coverpic = '<img src="user/'.$u.'/'.$filename.'" alt="pic">';
}
4

2 回答 2

1

代替

if(mysqli_num_rows($query) > 0){
    $row = mysqli_fetch_row($query);

while($row = mysqli_fetch_row($query)){
于 2013-05-08T14:05:32.957 回答
0

您使用 while 循环来获取结果。

$coverpic = '';
$sql = "SELECT filename FROM photos WHERE user='$u'";
$query = mysqli_query($db_conx, $sql);
if(mysqli_num_rows($query) > 0){

   while($row = mysqli_fetch_row($query)) {
      $filename = $row[0];
      $coverpic .= '<img src="user/'.$u.'/'.$filename.'" alt="pic">'; //notice the .= to append to the string instead of overwrite
   }

}

但是如果你想用更少的检查让你的代码更短一点:

$coverpic = '';
$query = mysqli_query($db_conx, "SELECT filename FROM photos WHERE user='$u'")){
while($row = mysqli_fetch_row($query)) {
   $coverpic .= '<img src="user/'.$u.'/'.$row[0].'" alt="pic">';
}
于 2013-05-08T14:04:36.200 回答