Connection to the MySQL Server using PHP

Before  access data in a database, we must open a database connection to the MySQL server.

In PHP, using  the mysqli_connect() function.

mysqli_connect(host,username,password,dbname);

mysqli_connect() function
<?php
// Create a connection
$con=mysqli_connect(“example.com”,”user01″,”password”,”my_db”);// Check connection
if (mysqli_connect_errno())
{
echo “Failed to connect to MySQL: ” . mysqli_connect_error();
}
?>
Read more

Create Database using MySQL and PHP

The CREATE DATABASE statement is using to create a database table in MySQL.

The following example creates a database named “my_db”:

CREATE DATABASE
<?php

$con=mysqli_connect(“example.com”,”user01″,”password”);
// Check connection
if (mysqli_connect_errno())
{
echo “Failed to connect to MySQL: ” . mysqli_connect_error();
}

// Create database
$sql=”CREATE DATABASE my_db”;
if (mysqli_query($con,$sql))
{

Read more

Create a Table using MySQL and PHP

The CREATE TABLE statement is using to create a table in MySQL.

The following example helps to create a table named “users”, with three columns: “FirstName”, “LastName” and “Age”:

Create a Table using MySQL
<?php

< ?php $con=mysqli_connect("example.com","user01","password","my_db"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // Create table $sql="CREATE TABLE users(FirstName CHAR(30),LastName CHAR(30),Age INT)"; // Execute query if (mysqli_query($con,$sql)) { echo "Table users created successfully"; } else { echo "Error creating table: " . mysqli_error($con); }

?>

Read more