0% found this document useful (0 votes)
75 views16 pages

Chapter 5

The document provides steps for accessing a MySQL database from a PHP page: 1. Create a connection to the MySQL database 2. Select the database to use 3. Send a SQL query to the database 4. Retrieve and process the results of the query 5. Close the database connection

Uploaded by

Kidus Seleshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views16 pages

Chapter 5

The document provides steps for accessing a MySQL database from a PHP page: 1. Create a connection to the MySQL database 2. Select the database to use 3. Send a SQL query to the database 4. Retrieve and process the results of the query 5. Close the database connection

Uploaded by

Kidus Seleshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Accessing mysql Database using PHP

How Web Database Architectures Work


1.A user’s web browser issues an HTTP request for a particular php web
page.
2.The web server receives the request for php file, retrieves the file, and
passes it to the PHP engine for processing.
3.The PHP engine begins parsing the script. Inside the script is a
command to connect to the database and execute a query. PHP opens a
connection to the MySQL server and sends on the appropriate query.
4.The MySQL server receives the database query, processes it, and sends
the results back to the PHP engine.
5.The PHP engine finishes running the script. This usually involves
formatting the query results nicely in HTML. It then returns the
resulting HTML to the web server.
6.The web server passes the HTML back to the browser, where the user
can see the requested file.
2
Querying a Database from the Web
In any script used to access a database from the Web, you follow
some basic steps:
1. Check and filter data coming from the user.
2. Set up a connection to the appropriate database.
3. Query the database.
4. Retrieve the results.
5. Present the results back to the user.

3
Querying a Database from the Web

Steps to access MySQL database from PHP


page:
1. Create connection
2. Select a database to use
3. Send query to the database
4. Retrieve the result of the query
5. Close the connection

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy