我必须创建一些php页面,以允许您插入和显示一些随机图像(在我的mysql DB中保存为blob)。
好吧,当我尝试它时,会给我该错误:警告:mysql_fetch_array():提供的参数不是有效的MySQL结果资源。
这是我的页面:

 $n="SELECT COUNT('id_product')
 FROM 'products'";
$value=mysql_query($n);

do
{
 $selectionASC='SELECT id_product
        FROM products
        ORDER BY id_product ASC
        LIMIT 1';
 $selectionDESC='SELECT id_product
         FROM products
         ORDER BY id_product DESC
         LIMIT 1';
  $ASC=mysql_query($selectionASC)
    or die ('Impossible execute the query <br />').mysql_error();
  $DESC=mysql_query($selectionDESC)
    or die ('Impossible execute the query <br />').mysql_error();

  //____________________________________________________________________
  $rand_n=rand(($ASC-1),($DESC+1));

  //____________________________________________________________________
  $selected='SELECT id_product,name, price, img
         FROM products
         WHERE id_product='.$rand_n;
  //____________________________________________________________________
  while($row=mysql_fetch_array($selected))
        {
            echo "Product'id: &nbsp"; echo $row[0];
            echo '<br />';
            echo "Name: &nbsp"; echo $row[1];
            echo '<br />';
            echo "Price:: &nbsp"; echo $row[2];
            echo '<br />';
            echo "Immage: <img src='images/".$row['img']."' alt='Image'>";;
            echo '<hr> <br />';

            $value--;
        }
}
while ($value==0)


有人会这样告诉我我哪里错了吗?谢谢,抱歉我的英语不好!

编辑:

$allowedExts=array("gif", "jpeg", "jpg", "png");

if(isset($_POST['submit']))
  {
   $temp=explode(".", $_FILES["file"]["name"]);
$extension=end($temp);

if(isset($_FILES['file']['name']))
{
    if(!empty($_FILES['file']['name']))
    {
        $directory='\www\v1.2\loaded';
        $uploadfile = $directory . basename($_FILES['file']['name']);

        if ((($_FILES["file"]["type"] == "image/gif")
             || ($_FILES["file"]["type"] == "image/jpeg")
             || ($_FILES["file"]["type"] == "image/jpg")
             || ($_FILES["file"]["type"] == "image/pjpeg")
             || ($_FILES["file"]["type"] == "image/x-png")
             || ($_FILES["file"]["type"] == "image/png"))
             && ($_FILES["file"]["size"] < 20000)
             && in_array($extension, $allowedExts))
        {
                if ($_FILES["file"]["error"] > 0)
                {
                 echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
                }
                else
                {
                //$nameImage=$_FILES(["file"]["name"]);
                $typeImage=$_FILES["file"]["type"];
                $sizeImage=$_FILES["file"]["size"];

                    if (file_exists("upload/" . $_FILES["file"]["name"]))
                     {
                          echo $_FILES["file"]["name"] . " already exists. ";
                     }
                    else
                    {
                          if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
                          {
                              $insert="INSERT INTO 'img'
                               VALUES ('','$uploadfile','$typeImage','$sizeImage')";
                              echo "File's extension valid, upload executed";
                          }
                          else
                          {
                           echo "Upload failed: ceck the size and the extension";
                          }
                        }
                }
        }
        else
        {
        echo "Invalid file";
        }
    }
}


}

最佳答案

不要在数组中传递img。您必须传递索引号,因为您已使用mysql_fetch_array
因此,请从代码中替换此行,

  echo "Immage: <img src='images/".$row['img']."' alt='Image'>";;




echo "Immage: <img src='images/".$row['3']."' alt='Image'>";


在这里,我想img url存储在第三索引上。请检查图像索引并替换3的值

================================================== ==============================

使用mysql_fetch_assoc的更好方法。在这种情况下,您不需要从数据库中查找列的索引

只需从代码中替换while循环即可。它一定会工作

while($row=mysql_fetch_assoc($selected))
        {
            echo "Product'id: &nbsp"; echo $row['id_product'];
            echo '<br />';
            echo "Name: &nbsp"; echo $row['name'];
            echo '<br />';
            echo "Price:: &nbsp"; echo $row['price'];
            echo '<br />';
            echo "Immage: <img src='images/".$row['img']."' alt='Image'>";
            echo '<hr> <br />';

            $value--;
        }

09-11 20:25