PHP connect to MYSQL
From PHP 5 and later versions can work with a MySQL database using:
1. MySQLi extension ("i" stands for improved)
2. PDO (PHP Data Objects)
Earlier versions of PHP used the MySQL extension was deprecated in 2012.
Which one are better MySQLi or PDO?
Both MySQLi and PDO have their advantages:
PDO will work on 12 different database systems, but MySQLi will only work with MySQL databases. Both PDO and MYSQLi are object-oriented, but MySQLi also will offer a procedural API.
MySQL Examples in Both MySQLi and PDO Syntax
we will example three ways of working with PHP and MySQL:
- MySQLi (object-oriented)
- MySQLi (procedural)
- PDO
( MySQLi Object-Oriented ) PHP connect to MYSQL - examples
Before we have to access data in the MySQL database, we need to be able to connect to the server: - Open a Connection to MySQL
<?php
$server_name = "localhost";
$user_name = "username";
$password = "password";// Create connection to SQL myadmin
$conn = new mysqli($server_name, $user_name, $password);// Check connection to SQL myadmin
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?><!-- your PHP, HTML, CSS code -->
<?php
<!-- Close the Connection
The connection will be closed automatically when the script ends.
To close the connection before, use the following php code: -->
$conn->close();
?>
( MySQLi Procedural ) PHP connect to MYSQL - examples
<?php
$server_name = "localhost";
$user_name = "username";
$password = "password";// Create connection myadmin
$conn = mysqli_connect($server_name, $user_name, $password);// Check connection myadmin
if (!$conn) {
die("Connection failed to Database: " . mysqli_connect_error());
}
echo "Connected successfully to Database";
?><!-- your PHP, HTML, CSS code -->
<!-- Close the Connection
The connection will be closed automatically when the script ends.
To close the connection before, use the following php code: --><?php
mysqli_close($conn);
?>
( PDO ) PHP connect to MYSQL - examples
<?php
$server_name = "localhost";
$user_name = "username";
$password = "password";try {
$conn = new PDO("mysql:host=$server_name;dbname=myDataBase", $user_name, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully to DataBase";
} catch(PDOException $e) {
echo "Connection failed to DataBase: " . $e->getMessage();
}
?><!-- your PHP, HTML, CSS code -->
<!-- Close the Connection
The connection will be closed automatically when the script ends.
To close the connection before, use the following php code: --><?php
$conn = null;
?>
PHP connect to MYSQL PDO, connect to database code, MySQL connection, PDO connection, MySQL database connect, Query in php, HOW to create mysql database in php
PHP connect to MYSQL ? - php mysqli
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.