Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

PHP MySQL Create Table


PHP mysql_query() function is used to create table. Since PHP 5.5, mysql_query() function is deprecated. Now it is recommended to use one of the 2 alternatives.

  • mysqli_query()
  • PDO::__query()

PHP MySQLi Create Table Example

Example

  1. <?php
  2. $host = ‘localhost:3306’;
  3. $user = ;
  4. $pass = ;
  5. $dbname = ‘test’;
  6. $conn = mysqli_connect($host$user$pass,$dbname);
  7. if(!$conn){
  8.   die(‘Could not connect: ‘.mysqli_connect_error());
  9. }
  10. echo ‘Connected successfully<br/>’;
  11. $sql = “create table emp5(id INT AUTO_INCREMENT,name VARCHAR(20) NOT NULL,
  12. emp_salary INT NOT NULL,primary key (id))”;
  13. if(mysqli_query($conn$sql)){
  14.  echo “Table emp5 created successfully”;
  15. }else{
  16. echo “Could not create table: “. mysqli_error($conn);
  17. }
  18. mysqli_close($conn);
  19. ?>

Output:

Connected successfully
Table emp5 created successfully

Comments

comments