CH 3
CH 3
MySQLi is one of the most popular relational database system being used
on the Web today. It is freely available and easy to install, however if you
have installed Wampserver/Xampp server it already there on your
machine. MySQL database server offers several advantages:
MySQLi is easy to use, yet extremely powerful, fast, secure, and scalable.
MySQLi runs on a wide range of operating systems, including UNIX or
Linux, Microsoft Windows, Apple Mac OS X, and others.
MySQLi supports standard SQL (Structured Query Language).
MySQLi is ideal database solution for both small and large applications.
MySQLi is developed, and distributed by Oracle Corporation.
MySQLi includes data security layers that protect sensitive data from
intruders
…counts
mysqli_connect(server,username,password);
Php and MySqli
The first step for interacting with MySQL connecting to the server
requires the appropriately named mysqli_connect( ) function:
$dbc = mysqli_connect (hostname, username, password,
db_name);
The first three arguments sent to the function (hostname,
username, and password) are based upon the users and
privileges established within MySQL.
The fourth argument is the name of the database to use. This is
the equivalent of saying USE databasename within the mysql
client.
If the connection was made, the $dbc variable, short for database
connection (but you can use any name you want, of course).
..count
We can connect to mysql database by using either object oriented or procedural methods.
Connecting using object oriented
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database= " DbName";
// Create connection
$conn = new mysqli($servername, $username, $password,$database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
$conn->close(); // for closing connction
Creating Database
// Create connection
$conn = mysqli_connect($servername, $username, $password,$database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
mysqli_close($conn); // for closing connection
..count’s
After the data type, you can specify other optional attributes for each
column:
NOT NULL - Each row must contain a value for that column, null values
are not allowed
DEFAULT value - Set a default value that is added when no other value is
passed
UNSIGNED - Used for number types, limits the stored data to positive
numbers and zero
AUTO INCREMENT - MySQL automatically increases the value of the
field by 1 each time a new record is added
PRIMARY KEY - Used to uniquely identify the rows in a table. The
column with PRIMARY KEY setting is often an ID number, and is often
used with AUTO_INCREMENT
Each table should have a primary key column (in this case: the "id"
column). Its value must be unique for each record in the table.
Insert data to database table
if (mysqli_query($conn, $sql)) {
echo “ one New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
Inserting Multiple values
if (mysqli_multi_query($conn, $sql)) {
echo "New records created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
Retrieving Data From Db
The SELECT statement is used to Extract data from one or more tables:
SELECT column_name(s) FROM table_name or
SELECT * FROM table_name
The * used to select all columns from the table.
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " Name: " . $row["firstname"]. " " .
$row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
Retrieving
MySQL provides a LIMIT clause that is used to specify the number of records
to return.
The LIMIT clause makes it easy to code multi page results or pagination with
SQL, and is very useful on large tables.
Returning a large number of records can impact on performance.
$sql = "SELECT * FROM Orders LIMIT 30"; // it will return the first 30 records.
$sql = "SELECT * FROM Orders LIMIT 10 OFFSET 15"; OR
$sql = "SELECT * FROM Orders LIMIT 15, 10";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " .
mysqli_error($conn);
}
Removing Data from Db Table
For the third objective, there are numerous steps you can
and should take, all based upon the premise of never trusting
user-supplied data.
First, validate that some value has been submitted, or that it is of the
proper type (number, string, etc.).
Second, use regular expressions to make sure that submitted data
matches what you would expect it to be.
Third, you can typecast some values to guarantee that they’re numbers.
A fourth recommendation is to run user-submitted data through the
mysqli_real_escape_string( ) function.
This function makes data safe to use in a query by escaping
what could be problematic characters.
$safe = mysqli_real_escape_string ($dbc, data);
End of The Chapter