i
PHP Variables
PHP Data Types
PHP Echo & Print
PHP Strings
PHP Numbers
PHP Constants
PHP Operators
PHP if...else...elseif Statements
Switch Statement
PHP Loops
PHP Arrays
Superglobals
PHP Coding Standards
PHP Form Handling
PHP Form Validation
PHP URLs Validation
PHP Form Required Validation
Complete Form Example
PHP File Functions Open/Read
PHP File Create/Write
PHP File Upload
PHP Cookies Handling
PHP Session Handling
PHP filter_var() Function
PHP Validation Filters
PHP Sanitization Filters
Using Filters
Filters Advanced
JSON
PHP Date and Time
MySQL Database
MySQL Connect
MySQL Commands-Creating a Table
MySQL Commands-Inserting The data
MySQL Commands-Prepared Statement
MySQL Commands-Selecting The Data
MySQL Commands-Where and Order By
MySQL Commands-Deleting And Updating The Data
PHP-OOP Introduction
PHP-Classes/Objects
PHP-Constructor/Destructor
PHP-Access Modifiers
PHP-Inheritance
PHP-Inheritance and Protected Access Modifier
PHP-Overriding Inherited Methods
PHP-Final keyword
PHP-Abstract Classes
PHP-Constants
PHP-Traits
PHP-Static Methods and Properties
Introduction to Functions
Defining A function
Returning Values From A Function
Dynamic Function Calls
Variable Scope
Understanding Arguments Or Parameters
Testing For A Function Existence
Returning Multiple Values From A Function
Making practical Use By Building Code Libraries For Code Re-usability
Using Include() And Require()
You can connect your PHP with MySQL database using PDO or MySQLi extension.
PDO stands for PHP Data objects and is object oriented. It works on 12 different database systems and if you want to later switch to another database, PDO is very useful.
MySQLi stands for MySQL improved. It only works with MySQL database and if you want to change database you need to write all the queries again. It offers a procedural API along with Object oriented features.
Before you access the database, you should open a connection to MySQL, and you can do it as below:
MySQLi Object Oriented
$server_name = "localhost";
$user = "username";
$pwd = "password";
// To Create connection use below statement
$conn = new mysqli($server_name, $user, $pwd);
// Check connection using below code
if ($conn->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
echo "Connected successfully";
?>
MySQLi Procedural
$server_name = "localhost";
$user = "username";
$pwd = "password";
// To Create connection use below statement
$conn = mysqli_connect($server_name, $user, $pwd);
// Check connection using below code
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
PDO
$server_name = "localhost";
$user = "username";
$pwd = "password";
try {
$conn = new PDO("mysql:host=$server_name;dbname=myDB", $user, $pwd);
// set 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();
}
?>
In PDO you need to mention a database to connect to as it can connect to various databases.
Closing the connection
When the script ends, the connection to database is automatically closed, but if you want to close it early you can use below statements.
For MySQLi Object oriented:
$conn ->close();
For MySQLi Procedural:
mysqli_close($conn);
For PDO:
$conn = null;
Don't miss out!