PHP Lesson: MySQL

 MySQL adalah sistem database yang digunakan dengan PHP.

Create Table.

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

 $sql = "CREATE TABLE Table1(PersonName varchar(255),City varchar(255),Phone varchar(255));";
 if(mysqli_query($conn, $sql) == TRUE) echo "Table1 is created";
 else echo "Error: " . $sql . mysqli_error($conn);
 mysqli_close($conn);

 Insert data.

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

 $name = $_GET['name'];
 $city = $_GET['city'];
 $phone = $_GET['phone'];
 $sql = "INSERT INTO Table1 (PersonName,City,Phone) VALUES ('$name','$city','$phone');";
 if(mysqli_query($conn,$sql) == TRUE) echo "New data added";
 else echo "Error: " . $sql . mysqli_error($conn);
 mysqli_close($conn);

Update data.

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

 $name = $_GET['update'];
 $city = $_GET['city'];
 $phone = $_GET['phone'];
 $sql = "UPDATE Table1 SET City='$city',Phone='$phone' WHERE PersonName='$name';";
 if(mysqli_query($conn,$sql) == TRUE) echo "Data is updated";
 else echo "Error: " . $sql . mysqli_error($conn);
 mysqli_close($conn);

 Delete data.

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

 $name = $_GET['delete'];
 $sql = "DELETE FROM Table1 WHERE PersonName='$name';";
 if(mysqli_query($conn,$sql) == TRUE) echo "Data is deleted";
 else echo "Error: " . $sql . mysqli_error($conn);
 mysqli_close($conn);

Delete All data.

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

 $sql = "DELETE FROM Table1;";
 if(mysqli_query($conn,$sql) == TRUE) echo "All Data is deleted";
 else echo "Error: " . $sql . mysqli_error($conn);
 mysqli_close($conn);

Delete Table.

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

 $sql = "DROP TABLE Table1;";
 if(mysqli_query($conn, $sql) == TRUE) echo "Table1 is deleted";
 else echo "Error: " . $sql . mysqli_error($conn);
 mysqli_close($conn);

View Data.

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

 $sql = "SELECT *FROM Table1;";
 $result = mysqli_query($conn, $sql);

 if (mysqli_num_rows($result) > 0) 
 {
  while($row = mysqli_fetch_array($result)) 
  {	
  }
 }
 mysqli_close($conn);

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.

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