Page 1 of 1

Retreiving Images from Database

Posted: Wed Oct 07, 2009 12:50 pm
by darren_plehn
Hey all, I'm new to php and have a question. Any help would be appreciated. I am able to get images into my database using the help from http://www.phpbuilder.com/columns/flori ... hp3?page=1 but I'm having trouble implementing the retrieval of the image in my script below.
<html>

<head>
<title>Custom Foam Interior Templates</title>
</head>

<body>

<?php

$myvar = $_POST['formVar'];
$myvar2 = $_POST['formVar2'];
$myvar3 = $_POST['formVar3'];

$con = mysql_connect('host','username','password');
if (!$con){
die('Could not connect: ' . mysql_error());
}

mysql_select_db("caseclu_foaminterior", $con);

if(isset($_POST["q"]))
$selection = $_POST["q"];
else
$selection = null;
switch ($selection)
{
case "all":
$query = "SELECT * FROM interiors";
break;
case "id":
$query = "SELECT * FROM interiors WHERE id like '%$myvar3%'";
break;
case "custom":
$query = $_POST["cq"];
break;
case "category":
$query = "SELECT * FROM interiors WHERE category='$myvar2'";
break;
case "search":
$query = "SELECT * FROM interiors WHERE description like '%$myvar%'";
break;
default:
echo 'Nothing to do here';
}

$q = mysql_query($query)
or die("This is not a valid query: " . $query);

$fieldCount = mysql_num_fields($q);

echo "You selected the query <i>$query</i>.<br />
Here are your results: <br /><br />
<table border='1'>
<tr>";

// print table headers
for($i=0; $i<$fieldCount; $i++)
{
$field = mysql_fetch_field($q);
echo "<th>{$field->name}</th>";
}
echo "</tr>\n";

// Print table rows
while($record = mysql_fetch_row($q))
{
echo "<tr>";
foreach($record as $cell)
echo "<td>$cell</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);

?>

</body>

</html>


Does anyone know how to display the image field with the actual images rather than all the random characters. I am able to follow his tutorial http://www.phpbuilder.com/columns/flori ... hp3?page=2 on the next page to view the image, but no luck implementing it into my pre-exsisting queries. Any help is appreciated!

Re: Retreiving Images from Database

Posted: Thu Oct 08, 2009 12:34 am
by godwinsam
Hello,

I have mentioned below how to image that retried from the db,

<html>

<head>
<title>Custom Foam Interior Templates</title>
</head>

<body>

<?php

$myvar = $_POST['formVar'];
$myvar2 = $_POST['formVar2'];
$myvar3 = $_POST['formVar3'];

$con = mysql_connect('host','username','password');
if (!$con){
die('Could not connect: ' . mysql_error());
}

mysql_select_db("caseclu_foaminterior", $con);

if(isset($_POST["q"]))
$selection = $_POST["q"];
else
$selection = null;
switch ($selection)
{
case "all":
$query = "SELECT * FROM interiors";
break;
case "id":
$query = "SELECT * FROM interiors WHERE id like '%$myvar3%'";
break;
case "custom":
$query = $_POST["cq"];
break;
case "category":
$query = "SELECT * FROM interiors WHERE category='$myvar2'";
break;
case "search":
$query = "SELECT * FROM interiors WHERE description like '%$myvar%'";
break;
default:
echo 'Nothing to do here';
}

$q = mysql_query($query)
or die("This is not a valid query: " . $query);

$fieldCount = mysql_num_fields($q);

echo "You selected the query <i>$query</i>.<br />
Here are your results: <br /><br />
<table border='1'>
<tr>";

// print table headers
for($i=0; $i<$fieldCount; $i++)
{
$field = mysql_fetch_field($q);
echo "<th>{$field->name}</th>";
}
echo "</tr>\n";

// Print table rows
while($record = mysql_fetch_row($q))
{
echo "<tr>";
foreach($record as $cell)
echo "<td>$cell</td>";
?>
<td> <img src="siteurl/imagepath/<?php $imagename from db ?>"/> </td> //// put this code in which please you
need to display the image.

<?php

echo "</tr>";
}
echo "</table>";

mysql_close($con);

?>

</body>

</html>

Thanks & Regards

Re: Retreiving Images from Database

Posted: Thu Oct 08, 2009 11:14 pm
by darren_plehn
I tried your solution, but the pictures are not showing up. (the little thumbnails that represent pictures that are not connected seem to be in the right spot next to each row), but still no pictures.

Code: Select all

 
<html>
 
<head>
<title>Custom Foam Interior Templates</title>
</head>
 
<body>
 
<?php
    
    $myvar = $_POST['formVar'];
    $myvar2 = $_POST['formVar2'];
    $myvar3 = $_POST['formVar3'];
    
$con = mysql_connect('root','username','password');
if (!$con){
  die('Could not connect: ' . mysql_error());
}
 
mysql_select_db("caseclu_foaminterior", $con); //Replace with your MySQL DB Name
 
if(isset($_POST["q"]))
$selection = $_POST["q"];
else
$selection = null;
switch ($selection)
{
case "":
$query = "SELECT * FROM interiors";
break;
case "id":
$query = "SELECT * FROM interiors WHERE id like '%$myvar3%'";
break;
case "custom":
$query = $_POST["cq"];
break;
case "category":
$query = "SELECT * FROM interiors WHERE category='$myvar2'";
break;
case "search":
$query = "SELECT * FROM interiors WHERE description like '%$myvar%'";
break;
default:
echo 'Nothing to do here';
}
 
$q = mysql_query($query)
or die("This is not a valid query: " . $query);
 
$fieldCount = mysql_num_fields($q);
 
echo "You selected the query <i>$query</i>.<br />
Here are your results: <br /><br />
<table border='1'>
<tr>";
 
// print table headers
for($i=0; $i<$fieldCount; $i++)
{
$field = mysql_fetch_field($q);
echo "<th>{$field->name}</th>";
}
echo "</tr>\n";
 
// Print table rows
while($record = mysql_fetch_row($q))
{
echo "<tr>";
foreach($record as $cell)
echo "<td>$cell</td>";
 
?>
<td> <img src="http://caseclub.com/<?=$path;?>"> </td>
<?php
 
echo "</tr>";
}
echo "</table>";
 
mysql_close($con);
 
?>
 
</body>
 
</html>
Below is a screen capture of what's occurring. Pay special attention to the 2 on the bottom as those are the ones that should have pictures.

Re: Retreiving Images from Database

Posted: Tue Oct 13, 2009 2:02 am
by godwinsam
Hello,

1.can you please check the images are available in the corresponding folder?
2.Is this <?=$path;?> foldername/imagename.jpg or gif etc.?
3.imagename.jpg===> from the db.
4.for example <img src="http://caseclub.com/foldername[if you have the separate folder]/$cell->image field name from db">

Please try the steps above details.

Thanks & regards

Re: Retreiving Images from Database

Posted: Tue Oct 13, 2009 10:14 am
by darren_plehn
Thank you for the reply. I ended up solving my problem by using the code below. (foreach) statement. Very similiar to your solution.

Code: Select all

<table border='1'>
<tr>";
 
// Print table rows
while($record = mysql_fetch_row($q))
{
    echo "<tr>";{
        foreach ($record as $field => $cell) {
                if ($field == "path") { 
                        echo "<td><img src=$cell></td>"; 
                }
                else { 
                        echo "<td>$cell</td>";
                }
        }
}       
    echo "</tr>";
}
echo "</table>";
Thanks for the help!