Cara membuat tabel di MySQL menggunakan script php

Cara membuat tabel di MySQL menggunakan script php

Cara membuat tabel di MySQL menggunakan script php

Cara membuat tabel di MySQL menggunakan script php, Untuk membuat tabel di MySQL digunakan statement CREATE TABLE. Sebelum menjalankan script buat tabel di MySQL, terlebih dahulu buat database di MySQL.

Contoh berikut adalah tentang cara membuat tabel di mysql tanpa membuka phpmyadmin atau MySQL.

Contoh (MySQLi Object-oriented)

<?php

$servername = "localhost";

$username = "root";

$password = "";/* Put your password */

$dbname = "admin";/* Put your database name */

/* Create connection */

$conn = new mysqli($servername, $username, $password, $dbname);

/* Check connection*/

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

}

/* sql to create table */

$sql = "CREATE TABLE Student

(

ID int NOT NULL AUTO_INCREMENT,

FirstName varchar(50),

LastName varchar(50),

RollNo varchar(50),

City varchar(50),

PRIMARY KEY (ID)

)";

if ($conn->query($sql) === TRUE) {

echo "Table test created successfully";

}

else {

echo "Error creating table: " . $conn->error;

}

$conn->close();

?>

 

Contoh (MySQLi Prosedural)

<?php

$servername = "localhost";

$username = "username";

$password = "password";

$dbname = "admin";/* put your database name here */

/* Create connection */

$conn = mysqli_connect($servername, $username, $password, $dbname);

/* Check connection */

if (!$conn) {

die("Connection failed: " . mysqli_connect_error());

}

/* sql query to create table */

$sql = "CREATE TABLE Student

(

ID int NOT NULL AUTO_INCREMENT,

LastName varchar(50),

FirstName varchar(50),

RollNo varchar(50),

City varchar(50),

PRIMARY KEY (ID)

);

if (mysqli_query($conn, $sql)) {

echo "Table test created successfully";

}

else {

echo "Error creating table: " . mysqli_error($conn);

}

mysqli_close($conn);

?>

 

 

Baca Juga  Bekerja dengan Event pada vba excel

Tinggalkan Komentar

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

Need Help? Chat with us