Home Sql Mysql-table PHP SQL Insertion
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

PHP SQL Insertion

Advertisement
PHP SQL Insertion is used to execute MySQL queries in PHP script. It is used to send insert query or command that adds the records to MySQL.

PHP SQL Insertion

     

PHP SQL Insertion is used to execute MySQL queries in PHP script. It is used to send insert query or command that adds the records to MySQL.

Understand with Example

The Tutorial illustrate an example from 'PHP SQL Insertion'. To understand and grasp the example we create a table 'Stu' that create the required fields and datatypes respectively. The Table 'stu' has a Primary Key ID.

Create Table Stu:

CREATE TABLE `stu` (                     
          `id` int(11) NOT NULL auto_increment,  
          `name` varbinary(10) default NULL,     
          `class` int(11) default '12',          
          PRIMARY KEY  (`id`)                    
        )

phpsqlinsertion.php:

The PHP SQL insertion is saved with .php extension. The server side scripting include a local host, user, password and name of the database to which records are added. To execute the queries, the connection would be built between the front-end and MySQL in the backend. Once your connection is built successfully, you can add the records to the database. The die function( ) illustrate  that connection could not established and exists from the current script.

<?php
$host = "localhost";
$user = "root";
$password = "root";
$database = "komal";
$connection = mysql_connect($host,$user,$password) 
or die("Could not connect: ".mysql_error());
mysql_select_db($database,$connection) 
or die("Error in selecting the database:".mysql_error());
$sql="insert into stu(name,class) values('komal',12)";
echo "Insertion Successful ...";

echo "</table>";
mysql_close($connection);
?>

Output

Insertion Successful ...
Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 6, 2010

Ask Questions?    Discuss: PHP SQL Insertion  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com