PHP MySQL Database Connection

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.

Open a Connection to MySQL

 

Example:

<?php
$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 “DB Connected successfully”;
?>

Other Method:

<?php
$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”;
?>

Close the Connection

<?php

//Write your code to Database connect and run SQL command

//To close Database connection, you need to code as follows:

$conn->close();

?>

Other Method:

<?php

//Write your code to Database connect and run SQL command

//To close Database connection, you need to code as follows:

mysqli_close($conn);

?>

Let’s have few example:

  • Below example will create a Database from php code

<?php
$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);
}

// Create database
$sql = “CREATE DATABASE myDB”;
if ($conn->query($sql) === TRUE) {
echo “Database created successfully”;
} else {
echo “Error creating database: ” . $conn->error;
}

$conn->close();
?>

Other Method:

<?php
$servername = “localhost”;
$username = “username”;
$password = “password”;

// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die(“Connection failed: ” . mysqli_connect_error());
}

// Create database
$sql = “CREATE DATABASE myDB”;
if (mysqli_query($conn, $sql)) {
echo “Database created successfully”;
} else {
echo “Error creating database: ” . mysqli_error($conn);
}

mysqli_close($conn);
?>

  • Example to create table from php code

<?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 to create table
$sql = “CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)”;

if ($conn->query($sql) === TRUE) {
echo “Table MyGuests created successfully”;
} else {
echo “Error creating table: ” . $conn->error;
}

$conn->close();
?>

Other Method:

<?php
$servername = “localhost”;
$username = “username”;
$password = “password”;
$dbname = “myDB”;

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die(“Connection failed: ” . mysqli_connect_error());
}

// sql to create table
$sql = “CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)”;

if (mysqli_query($conn, $sql)) {
echo “Table MyGuests created successfully”;
} else {
echo “Error creating table: ” . mysqli_error($conn);
}

mysqli_close($conn);
?>

 

Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.

2 thoughts on “PHP MySQL Database Connection

  • Pingback: Homepage

  • February 10, 2016 at 8:54 pm
    Permalink

    123840 87463Aw, this was an exceptionally good post. In concept I would like to spot in writing such as this moreover – spending time and actual effort to create a exceptional article but so what can I say I procrastinate alot by way of no indicates uncover a strategy to go completed. 711107

Leave a Reply