473,788 Members | 2,856 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to view image from mysql usin php in this case

2 New Member
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 3523
Atli
5,058 Recognized Expert Expert
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
11766
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 table in MySQL with the path & filename to the image. I have successfully uploaded and performed an update query on the database, but the problem I have is I cannot retain the primary key field in a variable which is then used in a SQL update...
11
3252
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 just display it below: The following code contains the script to process the uploaded file, the form to upload the file and then a few links to view the file from
2
9324
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 a db table. Please note, I am using a PostgreSQL database I have written all the code out below that deals with the submission processing:
10
7406
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 recommend storing the image to the filesystem and only keeping a pointer to that in the table. I want to dump the image to a table. My code dumps the data into the table, however, I get the following error when trying to view the image "the image ......
3
6738
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 would like to create a record in mysql database. I've tried to find some tutorials but I've had trouble finding any good ones. Wondering if anybody has good examples or link to a good tutorial. Thanks in advance.
0
2400
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 View I want the form populated from there profile on the sql server. The code to save the profile works fine. But when the user logs back in they data doesn't load back to the form. The multiview is located inside the LoginView's Logged-In View ....
3
745
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 explained below. here i declare 2 arrays of char and int to store the values i want,
1
1686
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 my MYSQL table. So, guys help me.
22
4056
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 my forum actually works! Well, almost. It does query the correct places, lists the information, and I have even figured out how to see a full path to where the photo is stored. But is never actually SHOWS the image. My eventual goal is to...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10373
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10118
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2897
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.