How to create table in mysql using php

How to create table in mysql using php

First we need to define our database and connect

Note: make sure that you have already created database in PhpMyadmin
$host = "localhost";
$username = "username";
$password = "password";
$databasename = "DBname";

// First Create connection
$connection = mysql($host, $username, $password);
// Check connection stablished or not
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}

// sql query to create table
$sql_qry = "CREATE TABLE First_table (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL
)";

if (mysql_query($sql_qry,$connection)) {
echo "Table First_table created successfully";
} else {
echo "Error creating table: " . mysql_error($connection);
}

Vijay maurya

Vijay maurya is a web developer having several years experience and passionate about programming. Vijay worked in many project along with reputed companies. The main aim of blogging is to spread knowledge who need help in development and also to improve knowledge and skills.

Leave a Reply

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