PHP Lesson: MySQL

 MySQL adalah sistem database yang digunakan dengan PHP.

Create Database.

<?php
 $servername = "localhost";
 $username = "username";
 $password = "password";
 $conn = new mysqli($servername, $username, $password);

 if ($conn->connect_error)
 {
 die("Connection failed: " . $conn->connect_error);
 } 
 else
 {
 $sql = "CREATE DATABASE myDB";
 if ($conn->query($sql) === TRUE)
 {
 echo "Database created successfully";
 }
 else
 {
 echo "Error: " . $sql . "<br>" . $conn->error;
 } 
 }
 $conn->close();
?>

Create Table.

<?php
 $servername = "localhost";
 $username = "username";
 $password = "password";
 $dbname = "myDB";
 $conn = new mysqli($servername, $username, $password, $dbname);

 if ($conn->connect_error) 
 {
 die("Connection failed: " . $conn->connect_error);
 } 
 else
 {
 $sql = "CREATE TABLE Table1(PersonName varchar(255),City varchar(255),Phone varchar(255));";
 if ($conn->query($sql) === TRUE) 
 {
 echo "Table1 is created";
 } 
 else 
 {
 echo "Error: " . $sql . "<br>" . $conn->error;
 }
 }
 $conn->close();
?>

 Insert data.

<?php
 $servername = "localhost";
 $username = "username";
 $password = "password";
 $dbname = "myDB";
 $conn = new mysqli($servername, $username, $password, $dbname);

 if ($conn->connect_error) 
 {
 die("Connection failed: " . $conn->connect_error);
 } 
 else
 {
 $name = $_GET['name'];
 $city = $_GET['city'];
 $phone = $_GET['phone'];
 $sql = "INSERT INTO Table1 (PersonName,City,Phone) VALUES ('$name','$city','$phone');";
 if ($conn->query($sql) === TRUE) 
 {
 echo "New data added";
 } 
 else 
 {
 echo "Error: " . $sql . "<br>" . $conn->error;
 }
 }
 $conn->close();
?>

Update data.

<?php
 $servername = "localhost";
 $username = "username";
 $password = "password";
 $dbname = "myDB";
 $conn = new mysqli($servername, $username, $password, $dbname);

 if ($conn->connect_error) 
 {
 die("Connection failed: " . $conn->connect_error);
 } 
 else
 {
 $name = $_GET['update'];
 $city = $_GET['city'];
 $phone = $_GET['phone'];
 $sql = "UPDATE Table1 SET City='$city',Phone='$phone' WHERE PersonName='$name';";
 if ($conn->query($sql) === TRUE) 
 {
 echo "Data is updated";
 } 
 else 
 {
 echo "Error: " . $sql . "<br>" . $conn->error;
 }
 }
 $conn->close();
?>

 Delete data.

<?php
 $servername = "localhost";
 $username = "username";
 $password = "password";
 $dbname = "myDB";
 $conn = new mysqli($servername, $username, $password, $dbname);

 if ($conn->connect_error) 
 {
 die("Connection failed: " . $conn->connect_error);
 } 
 else
 {
 $name = $_GET['delete'];
 $sql = "DELETE FROM Table1 WHERE PersonName='$name';";
 if ($conn->query($sql) === TRUE) 
 {
 echo "Data is deleted";
 } 
 else 
 {
 echo "Error: " . $sql . "<br>" . $conn->error;
 } 
 }
 $conn->close();
?>

Delete All data.

<?php
 $servername = "localhost";
 $username = "username";
 $password = "password";
 $dbname = "myDB";
 $conn = new mysqli($servername, $username, $password, $dbname);

 if ($conn->connect_error) 
 {
 die("Connection failed: " . $conn->connect_error);
 } 
 else
 {
 $sql = "DELETE FROM Table1;";
 if ($conn->query($sql) === TRUE) 
 {
 echo "All Data is deleted";
 } 
 else 
 {
 echo "Error: " . $sql . "<br>" . $conn->error;
 } 
 }
 $conn->close();
?>

Delete Table.

<?php
 $servername = "localhost";
 $username = "username";
 $password = "password";
 $dbname = "myDB";
 $conn = new mysqli($servername, $username, $password, $dbname);

 if ($conn->connect_error) 
 {
 die("Connection failed: " . $conn->connect_error);
 } 
 else
 {
 $sql = "DROP TABLE Table1;";
 if ($conn->query($sql) === TRUE) 
 {
 echo "Table1 is deleted";
 } 
 else 
 {
 echo "Error: " . $sql . "<br>" . $conn->error;
 } 
 }
 $conn->close();
?>

Contoh

  1. http://shahrulnizam.com/web/mysql.php
  2. http://shahrulnizam.com/web/mysql.php?create
  3. http://shahrulnizam.com/web/mysql.php?name=Ali&city=Melaka&phone=111-1111111
  4. http://shahrulnizam.com/web/mysql.php?update=Ali&city=Pahang&phone=222-2222222
  5. http://shahrulnizam.com/web/mysql.php?delete=Ali
  6. http://shahrulnizam.com/web/mysql.php?all
  7. http://shahrulnizam.com/web/mysql.php?drop

Penerangan lanjut di

  1. http://www.w3schools.com/php/php_mysql_create_table.asp
  2. http://www.w3schools.com/php/php_mysql_insert.asp
  3. http://www.w3schools.com/php/php_mysql_update.asp
  4. http://www.w3schools.com/php/php_mysql_delete.asp

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.