0% found this document useful (0 votes)
2 views45 pages

Lesson-05-PHP-Working-with-Databases-in-PHP

This document provides an overview of working with databases in PHP, including the creation and use of functions, connecting to MySQL and MySQLi databases, and the advantages of using PDO for database access. It outlines the basic syntax for defining functions and connecting to databases, as well as the requirements for using the XAMPP web server. Additionally, it describes the process of creating a database and connecting to it using PHP code.

Uploaded by

JH OS ZE NT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views45 pages

Lesson-05-PHP-Working-with-Databases-in-PHP

This document provides an overview of working with databases in PHP, including the creation and use of functions, connecting to MySQL and MySQLi databases, and the advantages of using PDO for database access. It outlines the basic syntax for defining functions and connecting to databases, as well as the requirements for using the XAMPP web server. Additionally, it describes the process of creating a database and connecting to it using PHP code.

Uploaded by

JH OS ZE NT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 45

CS 6

ADVANCED WEB
PROGRAMMING
Unit 6:
Working with
Databases in PHP
PHP Functions

Functions are a type of procedure or


routine that gets executed whenever some
other code block calls it. PHP has over
1000 built-in functions.
The basic syntax of a function that we
create is:

<?php
function functionName($argument1, $argument2...) {
// code to be executed
}

functionName($argument1, $argument2...); // function


call
?>
Every function needs a name,
optionally has one or more arguments, and
most importantly, defines some kind of
procedure to be followed within the body,
that is, syntax/ code to be executed.
Some Basic Functions:

<?php
function showGreeting() {
echo "Hello Gon, Luffy, and Killua!";
}

showGreeting();
?>
Output:

Hello Gon, Luffy, and Killua!


Another example with
arguments:
<?php
function greetPerson($name) {
echo "Hi there, ".$name;
}

greetPerson(“Gon Freecss");
greetPerson(“Killua Zoldyck");
?>
Output:

Hi there, Gon Freecss


Hi there, Killua Zoldyck
More than one arguments:
<?php
function personProfile($name, $equipment, $job) {
echo "This person is ".$name." from ". $equipment.".";
echo “ ";
echo "His job is “ . $job . ".";
}

personProfile(“Killua", “Skateboard", “Assassin");


echo “ ";
personProfile(“Gon", “Jajanken", “Stealth Expert");
echo “ ";
personProfile(“Luffy", “Gear technique", “Pirate");
?>
Output:
This person is Killua from Skateboard.
His job is Assassin.
This person is Gon from Stealth Expert.
His job is Jajanken.
This person is Luffy from Pirate.
His job is Gear teachnique.
In PHP, just like in many other
languages, we can tell functions to return a
value upon executing the code.
Example

<?php
function difference($a, $b) {
$c = $a - $b;
return $c;
}

echo "The difference of the given numbers is: “ . difference(8, 3);


?>
Output:

The difference of the given numbers is: 5


MySQLi

The MySQLi Extension (MySQL


Improved) is a relational database driver
used in the PHP scripting language to
provide an interface with MySQL
databases.
Difference between
MySQL and MySQLi
• MySQLi supports both procedural interfaces
and object-oriented interfaces while MySQL
supports only procedural interfaces.
• MySQLi supports stored procedures but
MySQL does not.
Connecting to a Database

There are four ways you can


generally consider when you want to
connect to a previously created database.
Connecting to MySQL Databases

The
syntax for
connecting to a
MySQL
database:
Considering your entered information
is correct, you’d be successfully connected
to the right database and ready to start
writing and testing your queries.
Otherwise, the respective error message
would appear as defined by the die
function.
Connecting to MySQLi
Databases (Procedural)

The MySQLi stands for MySQL


improved. The syntax for connecting to a
database using MySQLi extension is:
Connecting to MySQLi Databases (Procedural)
This is a good way to start because it
is easy to understand and gets the job
done. However, the object-oriented logic
that we’ll see below is what everyone
should be getting into because of the other
components of programming being used in
this paradigm and also because it is kind
of a more structured way of doing things.
Connecting to MySQLi
databases (Object-Oriented)

Although the functionality is basically


the same, this is another way, object-
oriented way of connecting to a database
using the MySQLi extension.
Connecting to MySQLi databases
(Object-Oriented)
Even in this case, you can check to
see if the database was successfully
selected, but it is a matter of choice.
Object-oriented MySQLi is not a different
MySQLi as far as code functionality is
concerned, but rather a different way/logic
of writing it.
Connecting to PDO Databases
PDO stands for PHP Data Objects
and is a consistent way to access
databases, which promises much easier
portable code. PDO is more like a data
access layer that uses a unified API rather
than an abstraction layer. The syntax for
connecting to a database using PDO is:
Connecting to PDO Databases
PDO is widely used today for a bunch
of advantages it offers. Among them, we
can mention that PDO allows for prepared
statements and rollback functionality which
makes it really consistent, it throws
catchable exceptions which means better
error handling, and uses blind parameters
in statements which increases security.
PHP Database connection
The collection of
related data is called a
database. XAMPP stands
for cross-platform,
Apache, MySQL, PHP, and
Perl. It is among the simple
light-weight local servers
for website development.
Requirements: XAMPP web server procedure:

• Start XAMPP server by starting Apache,


MySQL, and FileZilla.
• Write PHP script for connecting to XAMPP.
• Run it in the local browser.
• Database is successfully created which is
based on the PHP code.
FileZilla
Is a free, open source File Transfer
Protocol (FTP) software tool that allows
users to set up FTP servers or connect to
other FTP servers in order to exchange
files. FileZilla traditionally supported File
Transfer Protocol over Transport Layer
Security (FTPS).
In PHP, we can connect to the
database using XAMPP web server by
using the following path.

"localhost/phpmyadmin"
Step by Step Process

• Open XAMPP and


start running the
Apache, MySQL
and FileZilla
PHP code to create a database:

• Now open your PHP file


and write your PHP code to
create database and a
table in your database.
• Save the file as
“data.php” in htdocs
folder under XAMPP
folder.
• Then open your web browser and type
localhost/data.php
Connect to a Database
First, we need this
basic syntax in
HTML and Save it
as index.php
The second, thing we need to do here is
we are going to create a database file, just click
the New File and Save it as dbh.php
$servername = “localhost";
$username = "root";
$password = "";
$name = "jorgedata";
$conn = mysqli_connect ($servername, $username, $password, $name);
We will include the database
connection in index.php at the top.
Self-Assessment

You might also like