Kalai Selvan
Kalai Selvan

Reputation: 21

Here i want to retrive and display the images from db

when i run the below code it shows output as encrypted symbols like this [[�ѥ����1}j�i�Ƥ�^i�췼ܨ5���l�r�Ejק�r:��}=��""]�Q[n�C6���ZVv{����!���Y�j��>|�2��o��#\�T���hz`��d�vnW�KF���ih�ҍz�2����]

i have more than 5 images in the database.

post your suggestions

    define('MYSQL_ASSOC',MYSQLI_ASSOC);

    error_reporting(E_ALL);
    $db = mysqli_connect("localhost", "root", "","phpapi");
    $query = "SELECT image FROM images";
    $result = mysqli_query($db,$query);
    $num =mysqli_num_rows($result);

    while($row = mysqli_fetch_array($result, MYSQL_ASSOC))
    {

    echo "<img src=\"$row[image]\" alt=\"img\" />";

                                    }
    ?>

Upvotes: 0

Views: 53

Answers (1)

fjf2002
fjf2002

Reputation: 882

I suppose $row[image] is the image content, but the image src attribute needs an url. You could

A) Convert the image content to a data url, see https://en.wikipedia.org/wiki/Data_URI_scheme (only suitable for small images), or

B) Specify an url that points to another php file that actually fetches the image from the database, i.e. something like

<img src="fetch-my-image.php?id=736">

and in fetch-my-image.php something like:

// ...
$result = mysqli_query($db, "SELECT image FROM images WHERE id = /* id GET parameter here */");
// ...
echo $row[image]

Of course you should use prepared statement, which I haven't detailed here. You should also set the correct MIME type in the HTTP response headers.

fetch-my-image.php should exactly echo the image contents to the output, nothing more.

Upvotes: 2

Related Questions