Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



how to insert a image in mysql

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 28-04-2009
Member
 
Join Date: Apr 2009
Posts: 3
Exclamation how to insert a image in mysql
  

hi friends help me plzzzz

can any once send me a code for to save a image in mysql by a stored procedure in .net

Reply With Quote
  #2  
Old 28-04-2009
Member
 
Join Date: May 2008
Posts: 2,290
Re: how to insert a image in mysql

Code:
<?php        
$con=mysql_connect($host,$user,$pass);
mysql_select_db($db);

    if(isset($_REQUEST['submit']))
    {    

        $imgtype=$_FILES['uploadfile']['type'];
        $name=$_REQUEST['name'];

        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 table (name,image) values ('$name','$content')";
            $res=mysql_query($sql) or die (mysql_error());
       }
    }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Test</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
</HEAD>
<BODY>
<form name="form" method="post" ENCTYPE="multipart/form-data" action="image_upload.php">
<table>
    <tr>
        <td>Name: <input type="text" name="name"></td>
    </tr>
    <tr>
        <td >
            Upload image: <input type="file" name="uploadfile"> 
        </td>
    </tr>
    <tr>
        <td><input name="submit" value="submit" type="submit"> </td>
    </tr>
</table>
</form>
</BODY>
</HTML>
Reply With Quote
  #3  
Old 28-04-2009
Member
 
Join Date: Feb 2009
Posts: 105
Re: how to insert a image in mysql

Here I will provide you the PHP code using which you could access your Image stored in MySql database:

Code:
You could also use some very basic PHP.

<?php

$conn = mysql_connect(dbhost, dbuser, dbpass) or die ('Unable to connect to database');
mysql_select_db(whatever_you_db_is);

$query = "SELECT * FROM tablename";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<img src={$row['image']}>";
}

?>
Reply With Quote
  #4  
Old 28-04-2009
Member
 
Join Date: Apr 2009
Posts: 3
Re: how to insert a image in mysql

i want in mysql how can i
Reply With Quote
  #5  
Old 28-04-2009
Member
 
Join Date: Apr 2008
Posts: 2,000
To insert an image, you need to establish a connection with MySQL database. Lets' take a database hibernatetutorial, in our case. This database has a table (Image table). For inserting an image in table, we have used the SQL statement “INSERT INTO Image values(?,?,?)” with the prepareStatement() method. Whenever an image is inserted into the database, it displays “Inserting Successfully”. If it fails to insert an image then it throws an exception.

Try to use the following code i am sure it will work for you.

Code:
import java.sql.*;
import java.io.*;

public class insertImage{
  public static void main(String[] args) {
    System.out.println("Insert Image Example!");
    String driverName = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "hibernatetutorial";
    String userName = "root";
    String password = "root";
    Connection con = null;
    try{
      Class.forName(driverName);
      con = DriverManager.getConnection(url+dbName,userName,password);
      Statement st = con.createStatement();
      File imgfile = new File("images.jpg");
      FileInputStream fin = new FileInputStream(imgfile);
      PreparedStatement pre = con.prepareStatement("insert into Image values(?,?,?)");
      pre.setInt(1,5);
      pre.setString(2,"Durga");
      pre.setBinaryStream(3,fin,(int)imgfile.length());
      pre.executeUpdate();
      System.out.println("Inserting Successfully!");
      pre.close();
      con.close();  
    }
    catch (Exception e){
      System.out.println(e.getMessage());
    }
  }
}


The code is also attached with this post. You Can Download It From Here
Reply With Quote
  #6  
Old 28-04-2009
Member
 
Join Date: Apr 2009
Posts: 3
Re: how to insert a image in mysql

thank for ur reply ,but i need to do in .net
(i,e) i will hv a upload button, where i will browse the image and i will have upload button when i click that button that image has to save in Mysql

how can i
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "how to insert a image in mysql"
Thread Thread Starter Forum Replies Last Post
How to Insert data into MySQL table Mahendra varma Software Development 4 04-10-2009 04:42 AM
Insert Images in MySQL from FTP MeteoWatch Software Development 2 02-07-2009 01:06 AM
How to perform Insert on duplicate key update in MySQL Xena Software Development 3 18-05-2009 09:49 PM
How to insert GetDate() function in MySQL Shanbaag Software Development 3 18-05-2009 05:40 PM
Batch insert on MySQL is possible Shreevats Software Development 3 16-05-2009 06:13 PM


All times are GMT +5.5. The time now is 04:10 PM.