need some help to display images from mysql with php

Jan

New Member
Hello!

I am making a search engine in my website. The code is taken from the book PHP and MYSQL Web Developing written by Luke Welling and Laura Thompson.
It's a very useful book.
The problem is that I don't know how to display the image that I have uploaded in my mysql table. The results.php file displays the image like a bunch of numbers and symbols...

I have uploaded the image in the mysql database and store it as longblob type.
Here is the code I use:

search.html:
HTML:
<html>
<head>
  <title>Book-O-Rama Catalog Search</title>
</head>

<body>
  <h1>Book-O-Rama Catalog Search</h1>

  <form action="results.php" method="post">
    Choose Search Type:<br />
    <select name="searchtype">
      <option value="author">Author
      <option value="title">Title
      <option value="isbn">ISBN
      <option value="price">Price
      <option value="image">Image
 </select>
    <br />
    Enter Search Term:<br />
    <input name="searchterm" type="text" size="40">
    <br />
    <input type="submit" name="submit" value="Search">
  </form>

</body>
</html>

results.php:
Code:
<html>
<head>
  <title>Book-O-Rama Search Results</title>
</head>
<body>
<h1>Book-O-Rama Search Results</h1>
<?php
  // create short variable names
  $searchtype=$_POST['searchtype'];
  $searchterm=trim($_POST['searchterm']);

  if (!$searchtype || !$searchterm) {
     echo 'You have not entered search details.  Please go back and try again.';
     exit;
  }

  if (!get_magic_quotes_gpc()){
    $searchtype = addslashes($searchtype);
    $searchterm = addslashes($searchterm);
  }
@ $db = new mysqli('localhost', 'username', 'password', 'database');

  if (mysqli_connect_errno()) {
     echo 'Error: Could not connect to database.  Please try again later.';
     exit;
  }

  $query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
  $result = $db->query($query);

  $num_results = $result->num_rows;

  echo "<p>Number of books found: ".$num_results."</p>";

  for ($i=0; $i <$num_results; $i++) {
     $row = $result->fetch_assoc();
     echo "<p><strong>".($i+1).". Title: ";
     echo htmlspecialchars(stripslashes($row['title']));
     echo "</strong><br />Author: ";
     echo stripslashes($row['author']);
     echo "<br />ISBN: ";
     echo stripslashes($row['isbn']);
     echo "<br />Price: ";
     echo stripslashes($row['price']);
     echo "<br />Image: ";
     echo stripslashes($row['image']);
   	 echo "</p>";
  }

  $result->free();
  $db->close();

?>
</body>
</html>

Can anyone help me to make the image display normally? Thanks very much.
 

Denno

New Member
From your wording, I'm assuming that you're storing the actual image in the database? I would suggest that you store the images in a folder in your images directory on your server, and just store the link to the image in the database. Then you can pull the link, echo it between <img> tags, and it will display as intended.

Denno
 

smoovo

New Member
Please post your output, screenshot, or text of what appear on your screen. If it's a link url, like "images/image.jpg" it should be coded like this...

PHP:
echo "<img src=\"".$stripslashes($row['image'])."\" alt=\"\" />";

But still, screenshot will be helpful.
 

sacrine

New Member
Storing raw image information in a database is generally a big no no. Like Denno said try moving your images to a folder and saving the filename in the database.

Otherwise if you really want to store the image in the database and have it work you will have to create another php file (ie image.php?bookID=45) and echo ONLY the image column information there... then use the php file as your image tag source '<img src="image.php?bookID=45" />'. You might have to output some header information in image.php but I can't remember off the top of my head... google is your friend!

Also, try a while loop for looping through sql results
while($row = mysqli_fetch_array($query)) {

}
 

Jan

New Member
I have followed the sugestion of Denno and it works fine now. I saved the images in a folder and put the links in the database...I didn't know it is possible to store html code in the mysql database and then execute it through php...
nice.
Thanks to all.
 
Top