Web Design Forum  
 
Go Back   Web Design Forum > Web Software > Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-15-2010, 09:44 PM   #1
Jan
New Member
 
Join Date: Nov 2010
Posts: 12
Default need some help to display images from mysql with php

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 Code:
<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.
Jan is offline   Reply With Quote


Old 12-22-2010, 02:48 AM   #2
Bronze Member
 
Join Date: Jun 2009
Posts: 51
Default

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
Denno is offline   Reply With Quote
Old 12-22-2010, 03:12 AM   #3
Gold Member
 
smoovo's Avatar
 
Join Date: Oct 2010
Posts: 388
Default

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 Code:
echo "<img src=\"".$stripslashes($row['image'])."\" alt=\"\" />"
But still, screenshot will be helpful.
__________________
SMooVo - Web Design & Development
contact@smoovo.com
www.SMooVo.com
smoovo is offline   Reply With Quote
Old 12-23-2010, 06:54 AM   #4
New Member
 
Join Date: Dec 2010
Location: Kuwait
Posts: 10
Default

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

}
sacrine is offline   Reply With Quote
Old 12-23-2010, 10:42 AM   #5
Jan
New Member
 
Join Date: Nov 2010
Posts: 12
Default

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.
Jan is offline   Reply With Quote


Reply

Tags
mysql, php, search engine

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 05:55 PM.


Camera Forum - Computer Forum - Web Design Forum

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Content Relevant URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.