PHP with MySQL
What is MySQL?
MySQL is a relational database management system (RDBMS)
developed by Oracle that is based on structured query language
(SQL).
MySQL is a very popular open-source relational database management
system (RDBMS).
Characteristics
MySQL is a relational database management system
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 supported by Oracle Corporation
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
To build a web site that shows data
from a database, you will need:
• An RDBMS database program (like MySQL)
• A server-side scripting language, like PHP
• To use SQL to get the data you want
• To use HTML / CSS to style the page
PHP Connect to MySQL
PHP 5 and later can work with a MySQL database using:
MySQLi extension (the "i" stands for improved)
PDO (PHP Data Objects)
Earlier versions of PHP used the MySQL extension. However, this extension was
deprecated in 2012.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$db = " my_db“;
// Create connection
$conn = mysqli_connect($servername, $username, $password, $my_db);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Mytable (firstname, lastname, email)
VALUES (‘Ali', ‘Ahmed', '
[email protected]')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>