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!
Retreiving Images from Database
Moderator: General Moderators
-
darren_plehn
- Forum Newbie
- Posts: 11
- Joined: Tue Oct 06, 2009 8:48 pm
Re: Retreiving Images from Database
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
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
-
darren_plehn
- Forum Newbie
- Posts: 11
- Joined: Tue Oct 06, 2009 8:48 pm
Re: Retreiving Images from Database
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.
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.
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>- Attachments
-
- The 2 rows at the bottom should have pictures next to them.
- error.jpg (71.8 KiB) Viewed 423 times
Re: Retreiving Images from Database
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
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
-
darren_plehn
- Forum Newbie
- Posts: 11
- Joined: Tue Oct 06, 2009 8:48 pm
Re: Retreiving Images from Database
Thank you for the reply. I ended up solving my problem by using the code below. (foreach) statement. Very similiar to your solution.
Thanks for the help!
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>";