473,405 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to view image from mysql usin php in this case

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //$file_dir="/htdocs/upload";
  3. // Connect to database
  4. $db_name1="test";// Database name 
  5.             $conn1=mysql_connect("localhost","","") or die("I Couldn't connect"); 
  6.             $db=mysql_select_db($db_name1,$conn1) or die("I Couldn't select your database");    
  7. data base
  8. //$q = <<<CREATE
  9. //create table pix (
  10.    //pid int primary key not null auto_increment,
  11.     //title text,
  12.   //  imgdata longblob)
  13. //CREATE;
  14. //(@mysql_query($q);
  15.  
  16.  
  17. $imgdir="../image/latest.jpg";
  18. if ($_REQUEST['completed'] == 1) {
  19.         // Need to add - check for large upload. Otherwise the code
  20.         // will just duplicate old file ;-)
  21.         // ALSO - note that latest.img must be public write and in a
  22.         // live appliaction should be in another (safe!) directory.
  23.          move_uploaded_file($_FILES['imagefile']['tmp_name'],$imgdir);
  24.         $instr = fopen($imgdir,"rb");
  25.         $image = addslashes(fread($instr,filesize($imgdir)));
  26.         //echo "jamal khan  =".$fupload;
  27.         if (strlen($instr) < 149000) {
  28.                 mysql_query ("insert into pix (title, imgdata) values (\"".
  29.                 $_REQUEST['whatsit'].
  30.                 "\", \"".
  31.                 $image.
  32.                 "\")");
  33.         } else {
  34.                 $errmsg = "Too large!";
  35.         }
  36. }
  37.  
  38.  
  39. $gotten = @mysql_query("select * from pix order by pid desc limit 1");
  40. if ($row = @mysql_fetch_assoc($gotten)) {
  41.         $title = htmlspecialchars($row['title']);
  42.         $bytes = $row['imgdata'];
  43.         //echo"if executed succefully";
  44. } else {
  45.         $errmsg = "There is no image in the database yet";
  46.         $title = "no database image available";
  47.         // Put up a picture of our training centre
  48.         $instr = fopen($imgdir,"rb");
  49.         $bytes = fread($instr,filesize($imgdir));
  50. }
  51.  
  52.  
  53. if ($_REQUEST["gim"] ==1) {
  54.       header("Content-type: image/jpeg");
  55.       echo "sdfds";
  56.     print $bytes;
  57.     exit ();
  58.   }
  59.  
  60. ?>
  61.  
  62. <html><head>
  63. <title>Upload an image to a database</title>
  64. <body bgcolor=white><h2>Here`s the latest picture</h2>
  65. <font color=red><?= $errmsg ?></font>
  66. <center><img src=?gim=1 width=144><br>
  67. <b><?= $title ?></center>
  68. <hr>
  69. <h2>Please upload a new picture and title</h2>
  70. <form enctype=multipart/form-data method=post>
  71. <input type=hidden name=MAX_FILE_SIZE value=150000>
  72. <input type=hidden name=completed value=1>
  73. Please choose an image to upload: <input type=file name=imagefile><br>
  74. Please enter the title of that picture: <input name=whatsit><br>
  75. then: <input type=submit></form><br>
  76. <hr>
  77. </body>
  78. </html>
  79.  
This my source code it insert picture into data base but cannot read data from data base.
The Question is now that how i can i access Image using the above scnario
The Actual Problem here <body bgcolor=white><h2>Here's the latest picture</h2>
<font color=red><?= $errmsg ?></font>
<center><img src=?gim=1 width=144><br>
<b><?= $title ?></center>
<hr>
in <img >tage but cannot understand.
Please help me, i will be very thankfull to all of u?
Nov 13 '07 #1
1 3493
Atli
5,058 Expert 4TB
Hi.

What are you trying to do with the source parameter of the <img> tag?
Expand|Select|Wrap|Line Numbers
  1. <img src=?gim=1 width=144>
  2.  
The src parameter should contain an URL to an image, or a CGI script that outputs the contents of an Image.

The way you use it is as you want it to load the same page it is on, adding the 'gim=1' GET value.

If that is the case, and you want this code to both output the image data, and output the HTML form to add a new image, I would recommend against it.

It would be far easier to create two .php files. One of them to print and process the form data, as well as fetch the image title and such info, and the second .php file to fetch and print the image data. Then you could reference the second .php file from the first file.

P.S.
Please use [code] tags when posting code. It is impossible to read without them.
Nov 13 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
11
by: bissatch | last post by:
Hi, I am trying to upload an image, create a new file based on that image and then store the base64 encoded image data in a database. I dont really know where my code is going wrong so I will...
2
by: bissatch | last post by:
Hi, I am trying to write script that is run when a form is submitted. The form contains an image input field and when submitted, the image is uploaded, resized and added as binary information to...
10
by: John Smith | last post by:
I know that uploading an image to a database has been covered, oh, about 3 trillion times. However, I haven't found anything covering uploading to a MySQL database with .net. Please don't...
3
by: gsoguerrilla | last post by:
Hi, I have limited knowledge in php and I am having trouble with uploading an image to a remote directory and resizing it if it's larger and renaming it to a unique id, while at the same time I...
0
by: Jacob Donajkowski | last post by:
Once the user logs in I want to have the users switch from the Roster View to the Profile View and enter their profile infomation and save it. Then the next time they login and go to the Profile...
3
by: Waruna | last post by:
Is there a way to block insert into mysql(5.0) using c api of mysql db.. i.e. say there is a table with 2 columns, one contains char other int then i want to insert 500 records at once,, as i...
1
by: brothers | last post by:
I am intermediate user of PHP. I want to create mine search page which allows user to view one or more than one image. I can make the search page to view one image but i cannot views all the image in...
22
by: skysober | last post by:
I have read and read about mysql, which I am sure is just like the quoted, "linux is very friendy, but it just chooses who it wants to be friends with..." My first attempt to read the database in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.