簡體   English   中英

使用mysql_fetch_array()顯示一些圖像

[英]Display some images with mysql_fetch_array()

我必須創建一些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--;
        } 

根據您的代碼,我看到您錯過了

$ selected = mysql_query($ selected);

上面的線

while($ row = mysql_fetch_array($ selected)){........}

檢查這應該對您有幫助。

我認為這里的問題是$rand_n=rand(($ASC-1),($DESC+1)); $ASC and $DESC是資源而不是整數,相反,您應該使用$ASC = mysql_num_rows($ASC); $DESC = mysql_num_rows($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--;
    } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM