Create PHP To Insert, Select, Update, Delete In MySQL Database Table

Share:

Create PHP To Insert, Select, Update, Delete In MySQL Database Table. MySQL is a database server processing system. With the help of php scripts, we can perform a variety of data processing in the website server with easy, such as creating a database table, delete the database table, storing a database table, to display the database table in the pages of our website.
Create PHP To Insert, Select, Update, Delete In MySQL Database TableFor example, it is used for registration application, verification applications, applications on a cms and others. If you create own a website, you will be in desperate need of a MySQL database processing system.

Create PHP To Insert, Select, Update, Delete In MySQL Database Table

Inside this post, we will learn about the various types of processing using a MySQL database from php programming language. Read this tutorial :

Create PHP Connect to the MySQL Database Server
Each summoning the MySQL database, then we have to open a connection to the database server first. Therefore, processing the MySQL database we must first master is on the MySQL database connection. The following php script to call the database server :

<?php
//these variables are used for database connection
$host = "localhost";
$user = "root";
$password = "";
$database_name = "databasename"; 

//connect to the database.
mysql_connect($host, $user, $password);
mysql_select_db($database_name);
?>

Each data processing, call php connection and put it on the database MySQL script processing. Another way is to put the php connection to another file and do the calling php file with the following script :

include("php-connect.php");
or
require("php-connect.php");

Create PHP INSERT Statement To Save Data in MySQL Database Table
To save the data into a MySQL database table, we can use php insert statement. Here’s the script:

<?php
//load database connection
include("php-connect.php");
$name = "Elisa";
$email = "elisa@mail.com";
$country = "Canada";

//Command to insert into table
$query = "INSERT INTO people (name,email,country) VALUES ('$name','$email','$country')"; 

//run the query to insert the person.
$result = mysql_query($query) OR die(mysql_error()); 

//let them know the person has been added.
echo "Data successfully inserted into the database table ... ";
?>

Create PHP UPDATE Statement For Editing Data in MySQL Database Table
To edit the data that has been stored in a MySQL database table, we can use the php update statement. Here’s the script :

<?php
//load database connection
include("php-connect.php");
$name = "Elisa";
$email = "elisa@mail.com";
$country = "Indonesia";

//Make the query to update the table.
$query = "UPDATE people SET country='$country'  WHERE email='$email'";

//run the query to update the person.
$result = mysql_query($query) OR die(mysql_error()); 

//let them know the person has been added.
echo "Data successfully updated into the database table ... ";
?>

Create PHP SELECT Statement For Displaying Data in MySQL Database Table
To display data from a MySQL database table, we can use php select statement. Here’s the php script :

<?php
//load database connection
include("php-connect.php"); 

//make the query to run.
//Sort the last name in an ascending order (A-Z)
$query = "SELECT * FROM people ORDER BY name ASC";
$result = mysql_query($query) OR die(mysql_error());

//now we turn the results into an array and loop through them.
while($row = mysql_fetch_array($result))
{
$name = $row['name'];
$email = $row['email'];
$country = $row['country'];
echo "Data successfully selected in the database table ... ";
echo "<br/>name :".$name;
echo "<br/>email :".$email;
echo "<br/>country :".$country;
}
?>

Create PHP DELETE Statement To Delete Data in MySQL Database Table
To delete data from a MySQL database table, we can use php delete statement. Here’s the php script :

<?php
//load database connection
include("php-connect.php"); 

//get the userid of the person we're deleting.
$email = "elisa@mail.com";

//write the query to delete the person.
$query = "DELETE FROM people WHERE email='$email'";

//run the query to delete the person.
$result = mysql_query($query) OR die(mysql_error());

echo "Data successfully deleted in the database table ... ";
?>

Please practice on localhost server or website hosting. Don’t forget to create a table in the MySQL database. The table name is the people and in full :

CREATE TABLE 'people' (
  'id' int(11) NOT NULL auto_increment,
  'name' varchar(100) NOT NULL,
  'email' varchar(100) NOT NULL,
  'country' varchar(25) NOT NULL,
  PRIMARY KEY  ('id')
) ;

I hope this tutorial can help you in understanding how to call a MySQL database table through php script. If you have questions or other, please feel free to submit it via the comment box below. We will be waiting for your participation …
Thanks for reading Create PHP To Insert, Select, Update, Delete In MySQL Database Table

[Tutorial.World.Edu]

8 thoughts on “Create PHP To Insert, Select, Update, Delete In MySQL Database Table

  1. I am creating a table wsp but its not creat and it is give error in varibale name of table
    $sql = “CREATE TABLE wsp
    (
    SNO INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(PID),
    ‘LCO’ DOUBLE(90),
    ‘NO’ FLOAT(50),
    ‘NO2′ FOLAT(50),
    ‘NOX’ FLOAT(50),
    ‘SO2′ FLOAT(50),
    ‘PM10′ FLOAT(50)
    )”;

    • Maybe you can try this code :
      $q="CREATE TABLE wsp (
      SNO INT NOT NULL AUTO_INCREMENT,
      PRIMARY KEY(SNO),
      LCO DOUBLE,
      NO FLOAT(50, 6),
      NO2 FLOAT(50, 6),
      NOX FLOAT(50, 6),
      SO2 FLOAT(50, 6),
      PM10 FLOAT(50, 6)
      ) ENGINE = MYISAM ";
      In addition, when we use AUTO_INCREMENT on the SNO, should be the primary key is SNO. if we use FLOAT, then the length will be a decimal number. For example : 49.123456, to get 6 decimal digits and a maximum of 50, how to write is (50, 6). In setting up a double, we are not allowed to make the request length / value.
      Hopefully can help you




Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>