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`) )
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); ?>
Insertion Successful ...
If you enjoyed this post then why not add us on Google+? Add us to your Circles
Liked it! Share this Tutorial
Discuss: PHP SQL Insertion
Post your Comment