0% found this document useful (0 votes)
8 views8 pages

IWT unit-V

MySQL is an open-source relational database management system ideal for various applications, known for its speed, reliability, and ease of use. It supports PHP through MySQLi and PDO, allowing developers to connect and interact with databases using different methods. phpMyAdmin is a user-friendly tool for managing MySQL databases, offering features like database creation, query execution, and data export, but it has limitations in backup functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

IWT unit-V

MySQL is an open-source relational database management system ideal for various applications, known for its speed, reliability, and ease of use. It supports PHP through MySQLi and PDO, allowing developers to connect and interact with databases using different methods. phpMyAdmin is a user-friendly tool for managing MySQL databases, offering features like database creation, query execution, and data export, but it has limitations in backup functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIT-V

MY SQL

MySQL is an open-source relational database management


system (RDBMS).[5][6] Its name is a combination of "My", the name of co-
founder Michael Widenius's daughter My,[7] and "SQL", the acronym
for Structured Query Language.

 MySQL is a widely used relational database management system


(RDBMS).
 MySQL is free and open-source.
 MySQL is ideal for both small and large applications.
 MySQL is open-source
 MySQL is free
 MySQL is ideal for both small and large applications
 MySQL is very fast, reliable, scalable, and easy to use
 MySQL is cross-platform
 MySQL is compliant with the ANSI SQL standard
 MySQL was first released in 1995
 MySQL is developed, distributed, and supported by Oracle Corporation
 MySQL is named after co-founder Ulf Michael "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

Who Uses MySQL?

 Huge websites like Facebook, Twitter, Airbnb, Booking.com, Uber,


GitHub, YouTube, etc.
 Content Management Systems like WordPress, Drupal, Joomla!,
Contao, etc.
 A very large number of web developers around the world.
PHP + MySQL Database System
 PHP combined with MySQL are cross-platform (you can develop in
Windows and serve on a Unix platform)

How to connect PHP with MySQL Database?

PHP 5 and later can work with a MySQL database using:


1. MySQLi extension.
2. PDO (PHP Data Objects).

Difference Between MySQLi and PDO


 PDO works on 12 different database systems, whereas MySQLi works only
with MySQL databases.
 Both PDO and MySQLi are object-oriented, but MySQLi also offers a
procedural API.
 If at some point of development phase, the user or the development team
wants to change the database then it is easy to that in PDO than MySQLi
as PDO supports 12 different database systems.He would have to only
change the connection string and a few queries. With MySQLi,he will need
to rewrite the entire code including the queries
Should I Use MySQLi or PDO?
 If you need a short answer, it would be "Whatever you like".
 Both MySQLi and PDO have their advantages:
 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.
 Both are object-oriented, but MySQLi also offers a procedural API.
 Both support Prepared Statements. Prepared Statements protect from
SQL injection, and are very important for web application security.

There are three ways of working with MySQl and PHP


1. MySQLi (object-oriented)
2. MySQLi (procedural)
3. PDO

Connecting to MySQL database using PHP


There are 3 ways in which we can connect to MySQl from PHP .
 Using MySQLi object-oriented procedure: We can use the MySQLi
object-oriented procedure to establish a connection to MySQL database
from a PHP script.
Syntax:
<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Creating connection
$conn = new mysqli($servername, $username, $password);

// Checking connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Output:

 Using MySQLi procedural procedure : There is also a procedural


approach of MySQLi to establish a connection to MySQL database from a
PHP script as described below.
Syntax:
<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Creating connection
$conn = mysqli_connect($servername, $username, $password);

// Checking connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Output:
 Using PDO procedure: PDO stands for PHP Data Objects. That is, in this
method we connect to the database using data objects in PHP as
described below:
Syntax:
<?php
$servername = "localhost";
$username = "username";
$password = "password";

try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username,
$password);
// setting the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
Output:

 Create a MySQL Database Using MySQLi and PDO


The CREATE DATABASE statement is used to create a database in MySQL.
The following examples create a database named "myDB":

Example (MySQLi Object-oriented)Get your own PHP Server


<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}

$conn->close();
?>

 Selecting a database

PHP uses mysqli_select_db function to select the database on which queries are
to be performed. This function takes two parameters and returns TRUE on
success or FALSE on failure.

Syntax:
mysqli_select_db ( mysqli $link , string $dbname ) : bool
Parameter & Description

 $link
 Required - A link identifier returned by mysqli_connect() or
mysqli_init().

 $dbname
 Required - Name of the database to be connected.

Exampl:
<html>
<head>
<title>Selecting MySQL Database</title>
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root@123';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);

if(! $conn ) {
die('Could not connect: ' . mysqli_error($conn));
}
echo 'Connected successfully<br />';
$retval = mysqli_select_db( $conn, 'TUTORIALS' );

if(! $retval ) {
die('Could not select database: ' .
mysqli_error($conn));
}
echo "Database TUTORIALS selected successfully\n";
mysqli_close($conn);
?>
</body>
</html>

 Listing a database

We can write the following PHP script to get the list of available MySQL
databases −

Example:

<?php
$con = mysql_connect("localhost", "userid", "password");

if (!$con) {
die('Could not connect: ' . mysql_error());
}
$db_list = mysql_list_dbs($con);

while ($db = mysql_fetch_object($db_list)) {


echo $db->Database . "<br />";
}
mysql_close($con);
?>

PHP MY Admin

phpMyAdmin is the most trusted and user-friendly database managers and


mostly used for web-based applications or programs. In the following article,
we will be learning about the importance of the phpMyAdmin tool in the web
world.
phpMyAdmin | Pre-requisites: To install phpMyAdmin software, you need a
server running platform like Windows or Linux supports operating systems.
 Web Browser: You need a web browser interface to run the tool.
 PHP scripting language: You need a server side language.
 Apache Web server: You need a web server to store phpMyAdmin files.
 MySQL or MariaDB Database: You need a database to manage
application data.

Features of phpMyAdmin
phpMyAdmin supports several features that are given below:

o phpMyAdmin can create, alter, browse, and drop databases, views, tables,
columns, and indexes.
o It can display multiple results sets through queries and stored procedures.
o phpMyAdmin use stored procedure and queries to display multiple results sets.
o It supports foreign keys and InnoDB tables.
o phpMyAdmin can track the changes done on databases, views, and tables.
o We can also create PDF graphics of our database layout.
o phpMyAdmin can be exported into various formats such as XML, CSV, PDF,
ISO/IEC 26300 - OpenDocument Text and Spreadsheet.
o It supports mysqli, which is the improved MySQL extension.
o phpMyAdmin can interact with 80 different languages.
o phpMyAdmin can edit, execute, and bookmark any SQL-statements and even
batch-queries.
o By using a set of pre-defined functions, it can transform stored data into any
format. For example - BLOB-data as image or download-link.
o It provides the facility to backup the database into different forms.

phpMyAdmin | Advantages:

 Its very simple to set up the tool.


 The GUI is very convenient and easy to understand for developers and
users.
 As its a web-based tool, it can be accessed it from any computer system.
 Its a intuitive web based interface which runs on any server.
 It is advantageous over console as many tasks like cut, copy and run
queries becomes very easy due to its GUI.
 It provides automatic MySQL backup facility.
 It don’t require a network connectivity as its installed on the computer
already having MySQL server.
 It provides maximum level of data security and efficiency for its client.
 It is supported by multi-language community.

Data Backup problem with phpMyAdmin


phpMyAdmin lacks a lot of features in import/export functionality. There are some backup
problems with phpMyAdmin that are given below:

o Scheduling - There is no way to export the data of the database in phpMyAdmin


automatically.
o Storage media support - As we have discussed earlier, phpMyAdmin is web-based
software, so it runs only on the browser. We can take backups only to local drives of our
system.
o Compression, Encryption, and other option - The files which are exported with
phpMyAdmin are saved as common text files, with any additional processing. Whereas
storing these files in the original form usually takes a lot of disk storage.

Prerequisite

o Web server - Apache, Nginx, IIS


o PHP
o Database - MySQL, MariaDB
o Web Browser

You might also like