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

 

Related Posts

  • 69
      [root@mysqlDB]# mysqladmin -u root -p create eduguru Enter password:****** This will create a MySQL database eduguru. This is a simple example to create database called eduguru. Example: <?php $dbhost = 'localhost:3036'; //If mysql server is on same server $dbuser = 'root'; //mysql user who has the permission to create database $dbpass…
    Tags: database, create, $conn, echo, $sql, $conn-, $username, $password, example, php
  • 41
    If you omit the ENGINE option, the default storage engine is used. Normally, this is MyISAM, but you can change it by using the --default-storage-engine server startup option, or by setting the default-storage-engine option in the my.cnf configuration file. You may also want to change the default storage engine just for the current session. You can do this…
    Tags: $sql, database, code, php, table
  • 40
    The mysqli_connect() function opens a new connection to the MySQL server. Syntax: mysqli_connect(host,username,password,dbname,port,socket); Parameter Description host Specifies a host name or an IP address username Specifies the MySQL username password Specifies the MySQL password dbname Optional. Specifies the default database to be used port Optional. Specifies the port number to…
    Tags: password, username, database, connection, php
  • 31
    If MySQL reports an error number 1005 from a CREATE TABLE statement, and the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed. Similarly, if an ALTER TABLE fails and it refers to error 150, that means a foreign key definition…
    Tags: error, table, create, failed
  • 31
    If you get a too many connections error,Error: 1040 SQLSTATE: 08004 (ER_CON_COUNT_ERROR), when you try to connect to the mysqld server, this means that all available connections are in use by other clients. To increase the concurrent connections what are allowed to connect to the server, change the max_connections system…
    Tags: error, check, database, connection

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: