2 Replies - 1602 Views - Last Post: 12 March 2013 - 03:33 PM Rate Topic: -----

#1 Xaviorin   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 17-January 13

Referencing and retrieving images with php from mysql

Posted 12 March 2013 - 11:45 AM

I have created a search page in php that allows a user to select a table from a drop down menu and then type in a search phrase. When submit is pressed it searches the selected table with the search phrase. My code is not the cleanest as I'm new to php but it does work. Once I get it working I plan to clean up the code.

The issue:
I have read several different methods of referencing an image inside of your database and then using php to display the image in my results page but so far none of the methods work for me. I would prefer to not put the image inside of the database and instead use a reference link to an image folder server side. So basically adding a column to the tables I already have with the image urls seems like the best option. But with the code I currently have I have no idea how to reference a particular image to display along side the "names" and "information" columns that are already being displayed properly. Anyone have any suggestions on an easy way to do this without destroying the code I have already written?

This is 1/4 of the code (that queries the table "classes") as the rest is just else if statements to switch between tables.

    mysql_connect("localhost", "root", "") or die("Error connecting to database: ".mysql_error());
        localhost - it's location of the mysql server, usually localhost
        root - your username
        third is your password
        if connection fails it will stop loading the page and display an error
    mysql_select_db("xaviorin_skyrim") or die(mysql_error());
    /* tutorial_search is the name of database we've created */
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <title>Search results</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style2.css"/>
	 <embed src="Skyrim- Peter Hollens & Lindsey Stirling.mp3" autostart="true" loop="true" width="2" height="0">

$Options = $_POST['submit']; //value sent from submit drop down menu

$classes_status = 'unchecked'; //all possible values start as unchecked
$powers_status = 'unchecked';
$skills_status = 'unchecked';
$shouts_status = 'unchecked';

if (isset($_POST['submit'])) {//if submit pressed check

$Options = $_POST['Options'];//post from index radio value

if ($Options == 'classes') {//if radio = classes

$classes_status = 'classes checked'; //change variable to classes checked

$query = $_POST['query']; 
    // gets value sent over search form
    $min_length = 3;
    // you can set minimum length of the query if you want
    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
        $query = htmlspecialchars($query); 
        // changes characters used in html to their equivalents, for example: < to &gt;
        $query = mysql_real_escape_string($query);
        // makes sure nobody uses SQL injection
         $raw_results = mysql_query("SELECT * FROM classes
            WHERE (`ClassName` LIKE '%".$query."%') OR (`ClassInformation` LIKE '%".$query."%')") or die(mysql_error());


        // * means that it selects all fields, you can also write: `id`, `title`, `text`
        // articles is the name of our table
        // '%$query%' is what we're looking for, % means anything, for example if $query is Hello
        // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
        // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'
        if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
            while($results = mysql_fetch_array($raw_results)){
            // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
                echo "<div id='results' style='background-image: url(bigresultbox.png); border: 1px solid black;'><h3>".$results['ClassName']."</h3>".$results['ClassInformation']."</div>";
                // posts results gotten from database(title and text) you can also show id ($results['id'])
        else{ // if there is no matching rows do following
            echo "<div id='noresults'>No results</div>";
    else{ // if query length is less than minimum
        echo "<div id='noresults' style='background-image: url(bigresultbox.png); border: 1px solid black;'>
Minimum search length is $min_length characters</div>";

This is what my results page currently looks like displaying "ClassName" followed by "ClassInformation" for each query that matches results. If I insert a new table column to reference images what would be the best method of doing this to go along with each returned query as shown below?

Posted Image

Thanks everyone.

Is This A Good Question/Topic? 0
  • +

Replies To: Referencing and retrieving images with php from mysql

#2 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6549
  • View blog
  • Posts: 26,550
  • Joined: 12-December 12

Re: Referencing and retrieving images with php from mysql

Posted 12 March 2013 - 12:35 PM

You would create img tags setting their src to the image you wish to display:

<img src="whatever/folder/someimage.gif">

If the images are all stored in the same folder then you might just store the filename in the database.

It is preferable to also set the height and width attributes for the img tag. You could use PHP to determine the size of the image or, if you prefer, perhaps also store the height and width of the images as separate fields. This second approach seems a little redundant, and less flexible, as PHP is able to determine the size of the image before inserting it into the page.

Decide on your database structure firstly though. That is, will there be exactly one image per "classes".

.. and the mysql library is DEPRECATED.

This post has been edited by andrewsw: 12 March 2013 - 12:36 PM

Was This Post Helpful? 1
  • +
  • -

#3 Xaviorin   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 17-January 13

Re: Referencing and retrieving images with php from mysql

Posted 12 March 2013 - 03:33 PM

Thanks Andrew. Sounds pretty straight forward. And by clean up code I meant change MySQL to pdo etc. Realized I was learning with a deprecated way way too late. But again thank you and I will put your info to use when I'm not on mobile. Kudos
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1