AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

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 ) 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

Example: ( MySQLi Object-Oriented ) PHP connect to MYSQL
Example, MySQLi Object-Oriented code

<?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 -->
<!-- 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->close();
?>


( MySQLi Procedural ) PHP connect to MYSQL - examples


Example: (MySQLi Procedural)
Example, ( MySQLi Procedural ) code

<?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


Example: (PDO)
Example, ( PDO ) code

<?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;
?>

Note: In the PDO example above we have also specified a database (myDB).



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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


CSS background color - opacity, gradient...
CSS flex - display: flex - CSS Flexbox L...
CSS border padding...
HTML text center horizontally and vertic...
HTML text input <input type="text"&g...
HTML link color...
How to Link CSS to HTML, external file n...
HTML link - hyperlink...
jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...
jQuery stop()...
jQuery animate()...
jQuery SlideToggle()...
jQuery slideDown()...