PHP Database Connectivity With MySql | PHP MySql Connect Database - Codetextpro

PHP with MSql


With PHP, you can connect to and manipulate databases. MySQL is the most popular database system used with PHP.

What is MySQL?

 MySQL is a database system used on the web
 MySQL is a database system that runs on a server
 MySQL is ideal for both small and large applications
 MySQL is very fast, reliable, and easy to use
 MySQL uses standard SQL
 MySQL compiles on a number of platforms
 MySQL is free to download and use
 MySQL is developed, distributed, and supported by Oracle Corporation
 MySQL is named after co-founder 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

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.

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.

MySQL Examples in Both MySQLi and PDO Syntax

In this, and in the following chapters we demonstrate three ways of working with

PHP and MySQL:

 MySQLi (object-oriented)

 MySQLi (procedural)


Open a Connection to MySQL

Before we can access data in the MySQL database, we need to be able to connect to the server:

Example (MySQLi Object-Oriented)


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


echo "Connected successfully";

// Create database


if ($conn->query($sql) === TRUE) {

echo "Database created successfully";

} else {

echo "Error creating database: " . $conn->error;




Tip: If you have to use a specific port, add an empty string for the database-name argument, like this: new mysqli("localhost", "username", "password", "", port)

Example (MySQLi Procedural)

$servername = "localhost";
$username = "username";
$password = "password";

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

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
echo "Connected successfully";

// Create database
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);

Example (PDO) (PHP Data Objects)

$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername", $username, $password);

// set the PDO error mode to exception

// use exec() because no results are returned
echo "Database created successfully<br>";
catch(PDOException $e)
echo $sql . "<br>" . $e->getMessage();
$conn = null;

PHP include and require Statements

The include and require statements are identical, except upon failure:

require will produce a fatal error (E_COMPILE_ERROR) and stop the
include will only produce a warning (E_WARNING) and the script will continue

Including files saves a lot of work. This means that you can create a
standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.

<div class="menu">
<?php include 'menu.php';?>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>

Post a Comment