使用mysql_fetch_array()显示一些图像

时间:2014-02-08 10:42:13

标签: php mysql sql

我必须创建一些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";
        }
    }
}

}

3 个答案:

答案 0 :(得分:0)

不要在数组中传递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--;
        } 

答案 1 :(得分:0)

根据您的代码,我发现您错过了

的陈述

$ selected = mysql_query($ selected);

以上

而($行= mysql_fetch_array($选择)){ ........ }

检查这应该对你有帮助。

答案 2 :(得分:0)

我认为这里的问题是$rand_n=rand(($ASC-1),($DESC+1));$ASC and $DESC是资源而不是整数,相反你应该使用 $ASC = mysql_num_rows($ASC); $DESC = mysql_num_rows($DESC); 然后你可以使用$rand_n=rand(($ASC-1),($DESC+1));

 $selected='SELECT id_product,name, price, img
     FROM products
     WHERE id_product='.$rand_n;
 $selected = mysql_query($selected);

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--;
    }