What is MySQLi?What does it do?
- MySQLi is an extension for PHP, often referred to as MySQL
improved. It was introduced in PHP 5.0, and will be in
every version following (until something better comes
along). It allows you to use all of the MySQL database
(version 4.1.3 or newer) servers features. It’s object
oriented interface, making it easier to use
- Support for prepared statements, helping secure you
code
- Support for multiple statements, allowing you to run more
than one query at a time.
- MySQLi not only has an object oriented interface but also
a procedural one. Making it even more widely usable.
Connection to MySQL
---------------OR-----------------
<?php
// Create connection
$conn = mysqli_connect("localhost", "username", "password",
"myDB"); // Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
?>
5.
Connection to MySQL
ParameterDescription
host / servername Specifies a host name or an IP address
username Specifies the MySQL username
password Specifies the MySQL password
dbname Specifies the default database to be used
mysqli_connect
The mysqli_connect() function opens a new
connection to the
MySQL server.
die()
The die() function prints a message and exits the
current script.
mysqli_connect_error()
The mysqli_connect_error() function returns the error
description
from the last connection error, if any.
6.
Close the Connection
Theconnection will be closed automatically
when the script ends. To close the connection before,
use mysqli_close(connection name);
Example:
mysqli_close($conn);
Insert Data Froma Form Into a Database
Let’s take a look on the following Database and Table
Database Name : MyDbase
Table Name : tblStudent
Fields Datatype (size)
IDNumber Varchar(20) PRIMARY KEY
StudName Varchar(50) NOT NULL
Grade Int NOT NULL
CREATE TABLE tblStudent
( IDNumber varchar(20) NOT NULL,
StudName varchar(50) NOT NULL,
Grade int NOT NULL,
PRIMARY KEY (IDNumber)
);
9.
Insert Data Froma Form Into a Database
Let’s take a look on the following FORM
(insertstud.php)
<form action = "insertstud.php" method = "POST">
IDNumber (required) <br>
<input type = "text" name = "txtIDNumber" /> <br>
StudName (required) <br>
<input type = "text" name = "txtStudName" /> <br>
Grade (required)<br>
<input type = "number" name = "txtGrade" /> <br>
<p>
<input type = "submit" name="submit">
<input type = "reset" name="reset">
<p>
</form>
Note: Insert the code above inside the <body> tag
10.
Insert Data Froma Form Into a Database
Insert the following php scripts after the </form> tag.
<?php
//DATABASE CONNECTION
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDbase";
// Create connection
$conn=mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
11.
Insert Data Froma Form Into a Database
CONT……
if (isset($_REQUEST['submit']))
{
extract($_REQUEST);
$IDNumber=mysqli_real_escape_string($conn,$_POST['txtIDNumber']);
$StudName=mysqli_real_escape_string($conn,$_POST['txtStudName']);
$Grade = mysqli_real_escape_string($conn,$_POST['txtGrade']);
if (mysqli_query($conn, "insert into tblstudent set IDNumber='$IDNumber',
StudName='$StudName', Grade='$Grade'"))
{ echo 'Student Successfully Inserted'; }
else
{ echo 'Cannot Insert Student'; }
}
mysqli_close($conn);
?>
12.
Insert Data Froma Form Into a Database
mysqli_real_escape_string
- The mysqli_real_escape_string() function escapes special characters
in a string for use in an SQL statement.
Syntax:
mysqli_real_escape_string(connection,escapestring);
connection
- Specifies the MySQL connection to use
escapestring
- The string to be escaped. Characters encoded are NUL
(ASCII 0), n, r, , ', ", and ControlZ.
13.
Insert Data Froma Form Into a Database
mysqli_query
The mysqli_query() function performs a query against the database
Syntax:
mysqli_query(connection,query);
connection
- Specifies the MySQL connection to use
query
- Specifies the query string