Results 1 to 6 of 6

Thread: how to insert a image in mysql

  1. #1
    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

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    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>

  3. #3
    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']}>";
    }
    
    ?>

  4. #4
    Join Date
    Apr 2009
    Posts
    3

    Re: how to insert a image in mysql

    i want in mysql how can i

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005
    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

  6. #6
    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

Similar Threads

  1. How to Insert data into MySQL table
    By Mahendra varma in forum Software Development
    Replies: 4
    Last Post: 04-10-2009, 04:42 AM
  2. Insert Images in MySQL from FTP
    By MeteoWatch in forum Software Development
    Replies: 2
    Last Post: 02-07-2009, 01:06 AM
  3. How to perform Insert on duplicate key update in MySQL
    By Xena in forum Software Development
    Replies: 3
    Last Post: 18-05-2009, 09:49 PM
  4. How to insert GetDate() function in MySQL
    By Shanbaag in forum Software Development
    Replies: 3
    Last Post: 18-05-2009, 05:40 PM
  5. Batch insert on MySQL is possible
    By Shreevats in forum Software Development
    Replies: 3
    Last Post: 16-05-2009, 06:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,257,622.22190 seconds with 17 queries