-3

I have a php page that has blobs from my database

i need 404 image to display when there is no blob

https://i.stack.imgur.com/cZc34.jpg https://i.stack.imgur.com/1xU1B.jpg

<?php
$host = "localhost";
$username = "root";
$password = "12345678";
$db = "sis";
$PicNum = $_GET["PicNum"];

mysql_connect($host,$username,$password) or die("Impossível conectar ao banco."); 
@mysql_select_db($db) or die("Impossível conectar ao banco."); 
$result=mysql_query("SELECT * FROM usuarios WHERE id=$PicNum") or die("Impossível executar a query "); 
$row=mysql_fetch_object($result); 

Header("Content-type: image/gif"); 
echo $row->avatar;?>
Matrix
  • 25
  • 7

1 Answers1

-1

Check the blob column is empty or NULL on the query.


$result = mysql_query("SELECT * FROM usuarios WHERE id=$PicNum AND WHERE length(avatar) != 0 AND avatar IS NOT NULL") or die("Impossível executar a query "); 
$row=mysql_fetch_object($result); 

if($row)
  echo $row->avatar;
else 
  {
    // return 404 page or print as 404 error
    "<h1>404 Not Found</h1>"; 
    echo "The page that you have requested could not be found."; 
  }

?>

Nishal K.R
  • 1,090
  • 11
  • 21
  • how to add an image to the echo of else? ex: echo $row->"/img/404.png"; – Matrix Jun 24 '19 at 07:49
  • You can redirect to a **404 page** or `header("HTTP/1.0 404 Not Found"); echo "

    404 Not Found

    "; echo "The page that you have requested could not be found."; exit(); `
    – Nishal K.R Jun 24 '19 at 08:05
  • The functions you use do not exist in PHP. – Dharman Jun 24 '19 at 10:16
  • @Dharman you can avoid the header and try again. – Nishal K.R Jun 24 '19 at 10:56
  • @Dharman Can you specify which isn't worked? – Nishal K.R Jun 24 '19 at 11:02
  • `mysql_query` doesn't exist. neither does `mysql_fetch_object` – Dharman Jun 24 '19 at 11:56
  • which version of php you are using – Nishal K.R Jun 24 '19 at 12:06
  • @Dharman Check [this](https://www.php.net/manual/en/function.mysql-fetch-object.php) – Nishal K.R Jun 24 '19 at 12:08
  • I know that this extension used to exist in PHP version about 10-15 years ago. I am merely pointing out that your answer is incorrect, which is why I downvoted, because this extension has been removed and no longer exists in PHP. You should fix your answer so that it works on current PHP version or make a big note in the beginning of your answer saying that this is legacy code which is not going to work today and should not be used. – Dharman Jun 24 '19 at 12:10
  • @Dharman: I changed only in the query and the DB connect and other code I copied from the question. I didn't check more because the question contains an older version of php – Nishal K.R Jun 24 '19 at 12:52