Mysql_fetch_assoc()不显示任何内容

时间:2014-03-11 16:38:25

标签: php mysql rows

这次我遇到mysql_fetch_assoc()的问题,使用后我在屏幕上看不到任何东西。它应该在屏幕上打印一堆记录,相反,我只有一个屏幕。

所以,这里是我的代码:

$selected="SELECT name,namep,price,time
         FROM img
         INNER JOIN products on img.id_img=products.id_img
         WHERE img.id_img='$rand_n'";
  $selected = mysql_query($selected)
        or die ("Impossible execute the query SEL"). mysql_error();
  //____________________________________________________________________
    while($row=mysql_fetch_assoc($selected))
        {
            if(isset($row) && !empty($row))
                {
                    printf("Product name: &nbsp %s", $row['namep']);
                echo '<br />';
                echo "Price: &nbsp"; echo $row['price'];
                echo '<br />';
                echo "Data: &nbsp"; echo $row['time'];
                echo '<br />';
                echo "Immage: <img src='".$row['name']."' alt='Image'>";
                echo '<hr> <br />';

                $value--;
            }
            else
            {
                echo "Yep, qualcosa decisamente non va!";
            }
        }

有人可以这么好解释我为什么不起作用?非常感谢。

1 个答案:

答案 0 :(得分:0)

试试这个

$selected="SELECT ........";
$selected2 = mysql_query($selected)
    or die ("Impossible execute the query SEL"). mysql_error();
if(mysql_num_rows($selected2) > 0){
    while($row=mysql_fetch_assoc($selected2))
     {

       printf("Product name: &nbsp %s", $row['namep']);
            echo '<br />';
            echo "Price: &nbsp"; echo $row['price'];
            echo '<br />';
            echo "Data: &nbsp"; echo $row['time'];
            echo '<br />';
            echo "Immage: <img src='".$row['name']."' alt='Image'>";
            echo '<hr> <br />';
        }
}
else
        {
            echo "Yep, qualcosa decisamente non va!";
        }