• support@answerspoint.com

How to Create Tables in MySQL Database Using PHP?

401

I am trying to create a table in MYSQL through php, But this occur some error. can anyone explain it? 

1Answer


0

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

These Code create a table named "my_table", with five columns: "id", "firstname", "lastname", "email" and "mobile"

 

<html>
<head>
<title>Create table</title>
</head>
<body>
	<?php
		$servername = "localhost";
		$username = "root";
		$password = "";
		$dbname = "my_database";
		// 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 my_table (
		id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
		firstname VARCHAR(50) NOT NULL,
		lastname VARCHAR(50) NOT NULL,
		email VARCHAR(80),
		mobile TIMESTAMP
		)";
		if (mysqli_query($conn, $sql)) {
			echo "my_table created successfully";
		} else {
			echo "Error creating table: " . mysqli_error($conn);
		}
		mysqli_close($conn);
	?>
</body>
</html>

 

  • answered 9 months ago
  • Community  wiki

Your Answer

    Facebook Share