Chapter 5
Chapter 5
3
Querying a Database from the Web
4
What is MySQLi?
Since the mid-90s, Mysql extension has served as the major
bridge between PHP and MySQL.
Although it has performed its duty quite well, situation has
changed since the introduction of PHP 5 and MySQL 4.1
5
What is MySQLi?
To correct the issues of MySQL extension, a new extenstion
has been created for PHP5
It is called MySQLi
It supports all the latest features in MySQL server 4.1 or
higher
The ‘i’ stands for any one of: improved, interface, ingenious,
incompatible or incomplete.
6
Major Features
Procedural Interface
An object-oriented interface
Support for the new MySQL binary protocol that was introduced
in MySQL 4.1.
Support for the full feature set of the MySQL C client library
7
Why Make the Switch?
Maintainable
Similar Syntax
New Interface
Advanced Options
Speed
Security
8
Setting Up a Connection
Before you can access data in a database, you must create a
connection to the database.
Procedural way
$con=mysqli_connect(“server_name",“user_name",“password
") or die ($con -> connect_error);
Object oriented way
$con=new mysqli(“server_name“,”user_name”,“password") or
die (mysqli_error($con));
9
Setting up a connection
Server_name Optional:
Specifies the server to connect to. Default value is "localhost“
User_name Optional:
Specifies the username to log in with. Default value is the name of
the user that owns the server process. Default is “root”
Password Optional:
Specifies the password to log in with. Default is ““
10
Setting up a connection
$con=new mysqli("localhost","root","") or
die ($con -> connect_error);
or in the procedural way
$con=mysqli_connect(“localhost”,”root”,””)
or die (mysqli_error($con));
11
Select Database
A database must be selected before a table can be created. We can
select the database using the mysqli_select_db() function
mysqli_select_db($con,“database_name") or die(mysqli_error($con));
or we can also include the database name, while we are creating
the database.
Procedural Way
$con=mysql_connect(“localhost”,”root”,””,”database_name”) or die
(mysql_error($con));
object orineted way
$con=new mysqli("localhost","root","",”database_name”) or die ($con -
> connect_error); 12
Query database
To query database, we will need to construct and execute a
SQL query.
Procedural way
Mysqli_query($con,”sql statement”);
Object Oriented way
$con -> query(“sql_statements”) or die ($con -> error);
13
Inserting data to mysql
Example
The below code inserts some values to the score table found in the test
database, in localhost.
<?php
$con=new mysqli("localhost","root","","class") or die ($con ->
connect_error);
$con->query("INSERT INTO test (id, fname, lname, score) values ('06/07',
'eyouel', 'fitsum', '50')") or die ($con -> error);
$con -> close();
?>
14
Selecting data from mysql
<?php
$con=new mysqli("localhost","root","","class") or die ($con ->
connect_error);
$result=$con -> query ("SELECT * FROM test");
while($row =$result->fetch_assoc()) {
echo "id: " . $row["ID"]. " - Name: " . $row["Fname"]. "
" . $row["lname"]." - score: ". $row["score"]. "<br>";
}
$con -> close();
?>
15
Thank You!
?Compiled by Azmeraw D 16