Help retrieving images from a database
Posted: Tue Oct 06, 2009 9:12 pm
Hey I'm kind of new to PHP, so any help is appreciated. I have decided to put images inside my database (I know many feel strongly against this). I am able to put an image in the database with the code below:
<?php
$handle = fopen("$_FILES['images/test.jpg']", "rb");
$img = fread($handle, filesize('$_FILES['images/test/jpg']'));
fclose($handle);
//die($img);
$img = base64_encode($img);
$con = mysql_connect('host','user','pass');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db_name", $con); //Replace with your MySQL DB Name
$id=mysql_real_escape_string($_POST['id']); //This value has to be the same as in the HTML form file
$category=mysql_real_escape_string($_POST['category']); //This value has to be the same as in the HTML form file
$description=mysql_real_escape_string($_POST['description']); //This value has to be the same as in the HTML form file
$sql="INSERT INTO interiors (id,category,description,image) VALUES ('$id','$category','$description','$img')"; /*form_data is the name of the MySQL table where the form data will be saved.
name and email are the respective table fields*/
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "The form data was successfully added to your database.";
mysql_close($con);
?>
As you can see there are 4 fields. The first 3 are text and the last is the picture. Obviously when the picture is loaded its just a bunch of random characters. My problem is decoding the characters. The script below is what I use to query my results.
<html>
<head>
<title>Title</title>
</head>
<body>
<?php
$myvar = $_POST['formVar'];
$myvar2 = $_POST['formVar2'];
$myvar3 = $_POST['formVar3'];
$con = mysql_connect('host','user','pass');
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db_name", $con); //Replace with your MySQL DB Name
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>
As you can see the queries are pre-written and will not change. (besides the variables which were carried over from the form on the page before $myvar). How can I decode the picture? I know it has something to do with "$db_img = base64_decode($db_img);" but i cant quite figure out where it belongs and what else needs to be added where. (all images will be the same format- gif if that makes a difference). My tutorial came from http://mrarrowhead.com/php_tutorials_ma ... ql_php.php, but having to adjust it to my script is messing me up.
<?php
$handle = fopen("$_FILES['images/test.jpg']", "rb");
$img = fread($handle, filesize('$_FILES['images/test/jpg']'));
fclose($handle);
//die($img);
$img = base64_encode($img);
$con = mysql_connect('host','user','pass');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db_name", $con); //Replace with your MySQL DB Name
$id=mysql_real_escape_string($_POST['id']); //This value has to be the same as in the HTML form file
$category=mysql_real_escape_string($_POST['category']); //This value has to be the same as in the HTML form file
$description=mysql_real_escape_string($_POST['description']); //This value has to be the same as in the HTML form file
$sql="INSERT INTO interiors (id,category,description,image) VALUES ('$id','$category','$description','$img')"; /*form_data is the name of the MySQL table where the form data will be saved.
name and email are the respective table fields*/
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "The form data was successfully added to your database.";
mysql_close($con);
?>
As you can see there are 4 fields. The first 3 are text and the last is the picture. Obviously when the picture is loaded its just a bunch of random characters. My problem is decoding the characters. The script below is what I use to query my results.
<html>
<head>
<title>Title</title>
</head>
<body>
<?php
$myvar = $_POST['formVar'];
$myvar2 = $_POST['formVar2'];
$myvar3 = $_POST['formVar3'];
$con = mysql_connect('host','user','pass');
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db_name", $con); //Replace with your MySQL DB Name
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>
As you can see the queries are pre-written and will not change. (besides the variables which were carried over from the form on the page before $myvar). How can I decode the picture? I know it has something to do with "$db_img = base64_decode($db_img);" but i cant quite figure out where it belongs and what else needs to be added where. (all images will be the same format- gif if that makes a difference). My tutorial came from http://mrarrowhead.com/php_tutorials_ma ... ql_php.php, but having to adjust it to my script is messing me up.