| | |
Displaying images from mysql database
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
something along these lines...you'll need to tweak it to fit your script, but you should get the idea...
php Syntax (Toggle Plain Text)
//connect to mysql with your variables here... //execute your sql query(s), store result in $result //make a table to hold images and data $output = "<table width=\"300\">"; //fetch each row of data while($row = mysql_fetch_array($result)) { //do something with each row of data //refer to columns holding data as $row['columnName']; //for example... //fill table with data...be sure to refer to the column/field names in your database table! $output .= (" <tr> <td><img src =\" ". $row['imageURL'] . "\"></td> <td>". $row['imageTitle'] . "</td> </tr> ") } //close table tag $output .= "</table>"; //display output echo $output;
Last edited by johnsquibb; Feb 12th, 2008 at 3:17 pm. Reason: forgot to put image tag!
•
•
Join Date: Dec 2003
Posts: 7
Reputation:
Solved Threads: 0
Thanks Johnsquibb for the reply,
Please see my code that I used to insert the image and other data. The image is inserted directly into the database.
<?php
$errmsg = "";
if (! @mysql_connect("localhost","root","")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("adim");
if(isset($_REQUEST['submit']))
{
$imgtype=$_FILES['uploadfile']['type'];
$name=$_REQUEST['name'];
$address=$_REQUEST['address'];
$dateofbirth=$_REQUEST['dateofbirth'];
if($imgtype=="image/jpeg" || $imgtype=="image/jpg" || $imgtype=="image/pjpeg" || $imgtype=="image/gif" || $imgtype=="image/x-png" || $imgtype=="image/bmp")
{
$image=$_FILES['uploadfile']['tmp_name'];
$fp = fopen($image, 'r');
$content = fread($fp, filesize($image));
$content = addslashes($content);
fclose($fp);
$sql="insert into img_tab1 (name,image,address,dateofbirth) values ('$name','$content','$address','$dateofbirth')";
$res=mysql_query($sql) or die (mysql_error());
}
}
?>
Please further suggestions to display the whole row data including the image will be appreciated.
Austin
Please see my code that I used to insert the image and other data. The image is inserted directly into the database.
<?php
$errmsg = "";
if (! @mysql_connect("localhost","root","")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("adim");
if(isset($_REQUEST['submit']))
{
$imgtype=$_FILES['uploadfile']['type'];
$name=$_REQUEST['name'];
$address=$_REQUEST['address'];
$dateofbirth=$_REQUEST['dateofbirth'];
if($imgtype=="image/jpeg" || $imgtype=="image/jpg" || $imgtype=="image/pjpeg" || $imgtype=="image/gif" || $imgtype=="image/x-png" || $imgtype=="image/bmp")
{
$image=$_FILES['uploadfile']['tmp_name'];
$fp = fopen($image, 'r');
$content = fread($fp, filesize($image));
$content = addslashes($content);
fclose($fp);
$sql="insert into img_tab1 (name,image,address,dateofbirth) values ('$name','$content','$address','$dateofbirth')";
$res=mysql_query($sql) or die (mysql_error());
}
}
?>
Please further suggestions to display the whole row data including the image will be appreciated.
Austin
You can insert in the image table the path where you have saved your picture.Then display it like:
php Syntax (Toggle Plain Text)
<img src =\" ". $row['imageURL'] . "\"> //where the value of $row['imageURL'] is the path of the image like "www.domain.com/pics/pic1.jpg"
i usually just insert the path to the location of the image, so I'm not well versed in storing the actual image data in and retrieving it from the database...I stumbled across this tutorial on google, however, so maybe it will be of use to you...
http://www.spoono.com/php/tutorials/tutorial.php?id=42
http://www.spoono.com/php/tutorials/tutorial.php?id=42
yes we can insert the image data but it slows down the execution of the script and have high data filesize.
Its not a good idea to store images in the database. As Ryan_vietnow has mentioned, it will add on to the server load, slows down the execution of the script and takes a lot of time to display it if you have more (say 100 images ) per page!
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
![]() |
Similar Threads
- displaying image from database (PHP)
- Displaying Images( buffered data ) from the Database using Java (Java)
- help needed in coloring html tables (JavaScript / DHTML / AJAX)
- PHP Members HELP W/DB Images (PHP)
- Pagination - not displaying results properly, please help! (PHP)
Other Threads in the PHP Forum
- Previous Thread: Output Images using PHP
- Next Thread: Populating a MySQL table with data from a csv file
| Thread Tools | Search this Thread |
# 5.2.10 alexa apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date directory display dissertation dynamic echo echo$_get[x]changingitintovariable... email encode error fairness file files folder form forms function functions google href htaccess html image images include indentedsubcategory insert ip javascript joomla legislation limit link local login mail memberships menu mlm multiple multipletables mysql mysqlquery newsletters oop open paypal pdf persist php problem provider query radio random recursion remote rss script search server sessions simple sms sockets source space spam sql syntax system table tutorial update upload url validator variable video web youtube






