Sunday, October 8, 2017

PHP Create MySQL Table


In this article, we are going to learn how to create database table in MySQL using PHP Script. To create database table using PHP script we require mysqli_query() function, Which help us to perform query operation in database.
Check out our previous tutorial to Create MySQL database using php.

NOTE : mysqli_connect() function is not available form PHP 7

we are creating below table from our script:
         Table : companyrecord
          ============================
          || id        ||    empname   ||  empsalary   ||
          ============================

PHP MySQLi Create Table Example

<?php  
$host = 'localhost';// set the hostname
$user = '';  // set the mysql username
$pass = '';  // set the mysql password
$dbname = 'companyrecord'; // set the database name 
// establish database connection  
$conn = mysqli_connect($host, $user, $pass,$dbname);  
if(!$conn){  
  die('Could not connect: '.mysqli_connect_error());  
}  
echo 'Connected to database successfully<br/>';  

// query to perform  
$sql = "create table employee(id INT AUTO_INCREMENT,empname VARCHAR(20) NOT NULL,  
empsalary INT NOT NULL,primary key (id))";
// performing the query in database  
if(mysqli_query($conn, $sql)){  
 echo "Table employee created successfully";  }else{  
echo "Could not create table: ". mysqli_error($conn);  
}  
  
mysqli_close($conn);  
?>

Output:
----------------------------
Connected to database successfully
Table employee created successfully



No comments:

Post a Comment