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

Share:

Create MySQLi & PHP To Insert, Select, Update, Delete In MySQL Database Table. Given some time to come mysql_ query will be deleted in php program then we can start trying to use other php script to access MySQL database. One php script is to use MySQLi.
Create MySQLi & PHP To Insert, Select, Update, Delete In MySQL Database TableWe need to know, MySQLi is much better than the mysql_ query. By using MySQLi, as a PHP programmer. You can enjoy a variety of advantages that exist in MySQL. Some of the benefits and advantages of using MySQLi, namely:
• Using the Interface / Object Oriented methods
• Support the use of Prepared Statement
• Support the use of Multiple Statement
• Support the use of Transaction
• Increasing the ability of fault-finding program (Debugging)
• Supports merging with server

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

In this post we’ll explore the use of mysqli + PHP to do the calling process to the MySQL database. Basically, MySQLi basically similar with mysql_ query but when we use the prepare() function, the script will be a little more complicated. The purpose of adding the prepare() function is to make a database call process is much more secure from SQL injection attacks. Immediately, following the tutorial ….

Create MySQL Database Table
If you ever practice tutorial in this post and had created a database table. In this step, you no longer need to create a database table because we use is the same table that the table people. Here are the table script :

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')
) ;

Save the database named databasename.

Create MySQLi & PHP to Connect in MySQL Database Server

Before performing the various processes in a MySQL database table, the first thing to be done is to connect the various commands to process the MySQL database can be executed.
Here’s the script of the MySQLi & PHP :

<?php
// server info
$host = 'localhost';
$user = 'root';
$password = '';
$database_name = 'databasename'; 

// connect to the database
$mysqli = new mysqli($host, $user, $password, $database_name); 

// show errors (remove this line if on a live site)
mysqli_report(MYSQLI_REPORT_ERROR);
?>

Obviously, the data connection using mysqli much easier and simpler when compared with mysql_ query.
Save this into a MySQLi connection in specific file such as : mysqli-connect.php. When do the access to the MySQL database, you just need to write a php script below before writing another command.

include(“mysqli-connect.php”);
or
require(“mysqli-connect.php”);

The next tutorial will be more complicated, please study well and carefully …

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

<?php
//load database connection
include("mysqli-connect.php");
// MySQL data
$name = "Daniel";
$email = "daniel@world.edu";
$country = "India";

$stmt = $mysqli->prepare("INSERT INTO people (name,email,country) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $name,
$email,
$country
);
$stmt->execute();
$stmt->close();
?>

Create MySQLi & 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 MySQLi and PHP update statement. Here’s the script :

<?php
//load database connection
include("mysqli-connect.php");
// MySQL data
$email = "daniel@world.edu";
$country = "Indonesia";

$stmt = $mysqli->prepare("UPDATE people SET country = ? WHERE email = ?");
$stmt->bind_param('ss', $country,
   $email
);
$stmt->execute();
$stmt->close();
?>

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

<?php
//load database connection
include("mysqli-connect.php");
// MySQL data
$name = "Daniel";

$stmt = $mysqli->prepare("SELECT name, email, country FROM people WHERE name = ?");
$stmt->bind_param('s',$name);
$stmt->execute();
mysqli_stmt_bind_result($stmt,$name,$email,$country);

    /* now we want to fetch the data from the database */
    while (mysqli_stmt_fetch($stmt)) {
        printf("%s %s %s\n", $name,$email,$country);
    }
    /* close statement */
mysqli_stmt_close($stmt);
?>

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

<?php
//load database connection
include("mysqli-connect.php");
// MySQL data
$email = "daniel@world.edu";

$stmt = $mysqli->prepare("DELETE FROM people WHERE email = ?");
$stmt->bind_param('s', $email);
$stmt->execute();
$stmt->close();
?>

All we wanted to convey about MySQLi and PHP has a complete written in the above tutorial. Please be learned, if you are experiencing problems please expressed in the comments below. We hope, this tutorial can be useful for you.
Thanks for reading Create MySQLi & PHP To Insert, Select, Update, Delete In MySQL Database Table

[Tutorial.World.Edu]

5 thoughts on “Create MySQLi & PHP To Insert, Select, Update, Delete In MySQL Database Table

  1. SELECT doeasn’t work.

    Why in $stmt->bind_param(‘sssdii’,$name); you put “sssdii”. Table has onlu three string fileds.
    Do you try code before you post it?
    I doubt that.

Leave a Reply

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