PHP DatabaseConnection
PHP DatabaseConnection
PHP DatabaseConnection
3
Using PHP-MyAdmin to work with MySQL database
This tutorial illustrates how to create a new database and a new user to access that database
on a MySQL Server using phpMyAdmin.
Introduction
phpMyAdmin is a popular web-based administration tool used to administrate MySQL servers
and databases.
1.0 PhpMyAdmin
phpMyAdmin is an open-source software tool introduced on September 9, 1998, which is written
in PHP. Basically, it is a third-party tool to manage the tables and data inside the database.
PhpMyAdmin supports various type of operations on MariaDB and MySQL. The main purpose
of phpMyAdmin is to handle the administration of MySQL over the web.
It is the most popular application for MySQL database management. We can create, update,
drop, alter, delete, import, and export MySQL database tables by using this software.
phpMyAdmin also supports a wide range of operation like managing databases, relations, tables,
columns, indexes, permissions, and users, etc., on MySQL and MariaDB. These operations can
be performed via user interface, while we still have the ability to execute any SQL statement.
Web server - phpMyAdmin's interface is based on our web browser, we need a web server to
keep phpMyAdmin's files inside it. Although we already have phpMyAdmin installed by default
as it is amongst the bundles that comes with the XAMPP server which we have already installed
in the beginning of this course.
You can use the links below for directives on how to install XAMPP SERVER:
Video link: https://www.youtube.com/watch?v=-f8N4FEQWyY
XAMPP download directives: https://www.javatpoint.com/phpMyAdmin
XAMPP server download link: https://www.apachefriends.org/download.html
After the installation, test phpMyAdmin follow the steps below:
i. Launch XAMPP and start Apache and MySQL on the XAMPP control panel.
ii. Open up your browser
iii. Type in the url: http://localhost/phpMyAdmin/
iv. The below site should be open if installation is correct and complete
What is MySQL?
MySQL is a database system used on the web
MySQL is a database system that runs on a server
MySQL is ideal for both small and large applications
MySQL is very fast, reliable, and easy to use
MySQL uses standard SQL
MySQL compiles on a number of platforms
MySQL is free to download and use
MySQL is developed, distributed, and supported by Oracle Corporation
MySQL is named after co-founder Monty Widenius's daughter: My
The data in a MySQL database are stored in tables.
A table is a collection of related data, and it consists of columns and rows. Databases are useful
for storing information categorically.
A company may have a database with the following tables:
Employees
Products
Customers
Orders
Study Session 3.4
Create and use connection using PHP Data Access Object (PDO) and MySQL
Enter the table name, number of columns, and click on Go. A message will show that the
table is created successfully.
Now enter the field name, type, their size, and any constraint here and save it.
The table is created successfully. We can make changes in the table from here.
2.0 PHP Connection to MySQL
PHP 5 and later can work with a MySQL database using:
MySQLi or PDO?
Both MySQLi and PDO have their advantages:
i. PDO will work on 12 different database systems, whereas MySQLi will only work with
MySQL databases. So, if you have to switch your project to use another database, PDO
makes the process easy. You only have to change the connection string and a few queries.
With MySQLi, you will need to rewrite the entire code - queries included.
ii. Both are object-oriented, but MySQLi also offers a procedural API.
iii. Both support Prepared Statements. Prepared Statements protect from SQL injection, and
are very important for web application security.
MySQLi (object-oriented)
MySQLi (procedural)
PDO
Open a Connection to MySQL
Before we can access data in the MySQL database, we need to be able to connect to the server:
2.1 MySQL with PDO
PHP PDO is a database access layer that provides a uniform interface for working with multiple
databases.
PDO simplifies the common database operations including:
Creating database connections
Executing queries using prepared statements
Calling stored procedures
Performing transactions
And handling errors
PDO allows you to work with any database that has a PDO driver available. PDO relies on
database-specific drivers, e.g., PDO_MYSQL for MySQL, PDO_PGSQL for PostgreSQL,
PDO_OCI for Oracle database, etc.
Therefore, to use PDO for a specific database, you need to have a corresponding database driver
available.
PDO makes it easy to deploy PHP applications because it doesn’t require you to manually
include any script files in your application like other libraries.
Note: In the PDO example above we have also specified a database (myDB). PDO require a
valid database to connect to. If no database is specified, an exception is thrown.
Tip: A great benefit of PDO is that it has an exception class to handle any problems that may
occur in our database queries. If an exception is thrown within the try{ } block, the script stops
executing and flows directly to the first catch(){ } block.
We can create the database and tables using phpMyAdmin graphical user interface as well as
with coding in php.
$conn->close();
2.4 How to Create Table in the database:
);
Here we will delete the 1st record from the emp5 database table using the mysqli delete query
Output
Output:
PDO approach for fetching data:
Output: