I am trying to display a picture from Mysql, but all the time an error comes out (screen below). There are two files in total.

First articles.php :

 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Статьи</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container"> <h1>Статьи</h1> <a href='admin' id="adm">Панель администратора</a> <p> <h3>Загруженные изображения</h3> <?php mysql_connect ( 'localhost', 'root', '' ); mysql_query( 'utf8_general ci' ); mysql_select_db ( 'blog' ); $query = "SELECT id FROM images"; $res = mysql_query( $query ); while( $img = mysql_fetch_array( $res ) ) { echo '<img src="image.php?id='.$img['id'].'" />'; } ?> </p> <footer> <p>тестовая CRM<br>Copyright &copy; 2015 </p> </footer> </div> </body> </html> 

And the image.php handler image.php :

 <?php // Соединяемся с сервером БД mysql_connect ( 'localhost', 'root', '' ); mysql_query( 'SET NAMES cp1251' ); mysql_select_db ( 'blog' ); if ( isset( $_GET['id'] ) ) { // Здесь $id номер изображения $id = (int)$_GET['id']; if ( $id > 0 ) { $query = "SELECT `content` FROM `images` WHERE `id`=".$id; // Выполняем запрос и получаем файл $res = mysql_query($query); if ( mysql_num_rows( $res ) == 1 ) { $image = mysql_fetch_array($res); // Отсылаем браузеру заголовок, сообщающий о том, что сейчас будет передаваться файл изображения header("Content-type: image/jpeg"); // И передаем сам файл echo $image['content']; } } } ?> 

Pictures are already available in the database. I keep them in BLOB. Screen error

  • And when you request localhost / crm / image.php? Id = 10 directly from the browser that gives? - Dmitriy Simushev
  • @Dmitriy Simushev writes object not found! - Alex
  • So the problem is not in the script. Check whether the name of the image.php file is image.php and why it is not available for the web server. - Dmitriy Simushev

1 answer 1

Most likely you are using a version of PHP in which the functions used to work with MySQL are marked obsolete, which PHP reports. But for some reason you do not notice it. Perhaps you have turned off warnings, maybe you do not read the web server logs, maybe you simply did not run image.php directly. In any case, the document headers change. As a result, the command header("Content-type: image/jpeg"); does not work.

A simple and incorrect solution would be to suppress the warning output by the mysql_connect() function. To do this, in the file image.php put @ before mysql_connect ( 'localhost', 'root', '' ); It should turn out like this:

 @mysql_connect ( 'localhost', 'root', '' ); 

A more correct and more complicated solution would be to avoid using the function header("Content-type: image/jpeg"); . Along the way, this will save the code from many additional connections to the database, but it will make it less universal.

The image.php file image.php not needed. In the articles.php file, replace

this:

 $query = "SELECT id FROM images"; $res = mysql_query( $query ); while( $img = mysql_fetch_array( $res ) ) { echo '<img src="image.php?id='.$img['id'].'" />'; } 

on this:

 $query = "SELECT `content` FROM `images`"; $res = mysql_query( $query ); if ( mysql_num_rows( $res ) > 0 ) while( $img = mysql_fetch_array( $res ) ) echo '<img src="data:image/jpeg;base64,'.base64_encode($img['content']).'" />'; 

Even more correct and complex solution will be the replacement of the outdated functions of working with MySQL with modern ones .

  • Thanks for the advice, I just don’t put the image.php folder there and it was all because of that. @GVA - Alex