简体   繁体   中英

Display some images with mysql_fetch_array()

I have to create some php pages that allow you to insert and display some random images (saved as blob in my mysql DB). Well, when i try it it give to me that error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource. And here my page:

 $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)

could someone be so kind to tell me where am I wrong? Thanks and sorry for my poor english!

EDIT:

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

}

don't pass img in array. you have to pass index number because you have used mysql_fetch_array so, replace this line from code,

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

to

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

Here i guess img url is store on 3rd index. please must check image index and replace the value of 3

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

Better way to use mysql_fetch_assoc . In that case you don't require to find index of column from database

simply replace while loop from your code. it will Definitely work

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

As per your code I see that you miss the statement of

$selected = mysql_query($selected);

above the line of

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

Check this should help you.

I think the problem here is $rand_n=rand(($ASC-1),($DESC+1)); , $ASC and $DESC are resource not integers, instead you should probably use $ASC = mysql_num_rows($ASC); $DESC = mysql_num_rows($DESC); $ASC = mysql_num_rows($ASC); $DESC = mysql_num_rows($DESC); then you can use $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--;
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM