KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones

Insert binary file into MySQL (BLOB) using PHP progaming

Before you start this lesson, I assume that you finished reading the following tutorials:

  1. install phpmyAdmin, mysql , php
  2. Create Database by using phpMyAdmin
  3. Working with forms
  4. Insert Form Data into Database
  5. Retrieve data from Database
  6. Insert binary file into MySQL using phpMyAdmin

This lession will demonstrate how to use PHP programming insert binary files into MySQL database BLOB column.

Insert Images into MySQL

There are 3 steps to insert binary files into MySQL BLOB columns:

  1. Open database connection
  2. Get binary file content
  3. Insert file into database

Connect to Database

From lesson: Insert Form Data into Database and Retrieve data from Database we already knew how to open database connection. We repeat here:

<?php
$username="root";
$password="yourpassword"; //input your password here.
$database="phonebook"; //your database here

//connect to database
$connect=mysql_connect(localhost,$username,$password);
//select database
@mysql_select_db($database) or die("<b>Unable to specified database</b>");

Get binary file content

//get images
$myimage = "c:\yourpath\yourimagefile.gif";
$image = file_get_contents($myimage);
$content=addslashes($image);

file_get_contents get $myimage binary file content in string. addslashes() function will returns a string with backslashes before characters that need to be quoted in database queries. So those special characters in image file could be properly stored in database.

You may give a URL to $myimage like the following

$myimage = "http://www.yourwebsite.com/images/yourimage.gif";

Insert into databse

//query databae
$query="INSERT INTO gallery (image) VALUES ('$content')";
@mysql_query($query) or die('Error, insert query failed');
//close connection
mysql_close($connect);

INSERT INTO gallery (image) VALUES ('$content') will insert $contenst into gallery table image column.

Put it together:

<?php
$username="root";

$password="yourpassword"; //input your password here.
$database="phonebook"; //your database here

//connect to database
$connect=mysql_connect(localhost,$username,$password);
//select database
@mysql_select_db($database) or die("<b>Unable to specified database</b>");

//get images
$myimage = "c:\yourpath\yourimagefile.gif";;
$image = file_get_contents($myimage);
$content=addslashes($image);

//query databae
$query="INSERT INTO gallery (image) VALUES ('$content')";
@mysql_query($query) or die('Error, insert query failed');
//close connection
mysql_close($connect);
?>

There are other alternative ways to insert binary files into MySQL:

  1. Using php programming insert binary files into MySQL
  2. Using HTML Form and php progamming insert binary files into MySQL(local and remote)
  3. Using PhpMyAdmin insert binary files.

 

PHP Calendar
Comprehensive PHP Web Calendar
Configurable PHP Calendar
PHP Calendar
PHP Image Processing
PHP grahic softwares
PHP create real-time dynamic chart
create antispam image(image verification)
Add water mark to image
PHP Image processing introduction
Add text on Image
Create thumbnail for image
Create image from text on fly
PHP code sample
PHP create dynamic PDF with FPDF
PHP create dynamic PDF with PDFLib
PHP create dynamic PDF with ClibPDF
Improved PHP configurable hit counter
Install phpMyAdmin,MySql, PHP
PhpMyAdmin export/import data
A simple PHP RSS feed reader
PHP include (SSI)
Working with Forms
PHP formmail Generator
Using phpMyAdmin Create Database
Insert Form Data into Database
Retrieve data from Database
Retrieve multiple records
Batch insert text using phpMyAdmin
Using phpMyAdmin insert binary file
Insert binary file using PHP code
Using form insert binary file into MySQL
Retrieve binary file from MySQL
Image hosting business in minutes
Send HTML mail in PHP
Send bulk emails in PHP
Polling/Rating System in PHP
Polling/Rating System in PHP(radio button)
Check visitor's IP address
Check reciprocal link
PHP tutorial
What is PHP?
Install PHP on Windows XP (part1)
Install PHP on Windows XP (part2)
PHP open and closing tags
PHP types
Variables
Constants
Operators(part1)
Operators(part2)
Logic Structures
Include files
Loops
Set cookie and delete cookie
Most often used MySQL functions

 

 

Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer