+ All Categories
Home > Documents > PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate...

PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate...

Date post: 19-Jan-2016
Category:
Upload: eileen-gwen-payne
View: 232 times
Download: 0 times
Share this document with a friend
24
PHP Database Pemrograman Internet
Transcript
Page 1: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

PHP Database

Pemrograman Internet

Page 2: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

PHP MySQL Database

• With PHP, you can connect to and manipulate databases.

• MySQL is the most popular database system used with PHP

Page 3: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

What is MySQL?

• MySQL is a database system used on the web• MySQL is a database system that runs on a server• MySQL is ideal for both small and large applications• MySQL is very fast, reliable, and easy to use• MySQL uses standard SQL• MySQL compiles on a number of platforms• MySQL is free to download and use• MySQL is developed, distributed, and supported by

Oracle Corporation

Page 4: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

MySQL database

• The data in a MySQL database are stored in tables. A table is a collection of related data, and it consists of columns and rows.

• Databases are useful for storing information categorically. A company may have a database with the following tables:– Employees– Products– Customers– Orders

Page 5: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

PHP Connect to MySQL

PHP 5 and later can work with a MySQL database using:

• MySQLi extension (the "i" stands for improved)

• PDO (PHP Data Objects)

Page 6: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

MySQLi & PDO

• Both are object-oriented, but MySQLi also offers a procedural API

• PDO will work on 12 different database systems, MySQLi will only work with MySQL databases

Page 7: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

Open a Connection to MySQL

• Example (MySQLi Object-Oriented)• Example (MySQLi Procedural)• Example (PDO)

Page 8: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

Example (MySQLi Object-Oriented)

• <?php$servername = "localhost";$username = "username";$password = "password";

// Create connection$conn = new mysqli($servername, $username, $password);

// Check connectionif ($conn->connect_error) { die("Connection failed: " . $conn->connect_error);} echo "Connected successfully";?>

Page 9: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

Example (MySQLi Procedural)

<?php$servername = "localhost";$username = "username";$password = "password";

// Create connection$conn = mysqli_connect($servername, $username, $password);

// Check connectionif (!$conn) { die("Connection failed: " . mysqli_connect_error());}echo "Connected successfully";?>

Page 10: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

Example (PDO)• <?php

$servername = "localhost";$username = "username";$password = "password";

try { $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; }catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); }?>

Page 11: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

Close the Connection

Page 12: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

PHP Insert Data Into MySQL

• Insert Data Into MySQL Using MySQLi • Insert Data Into MySQL Using PDO

Page 13: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

Example (MySQLi Object-oriented)<?php

$servername = "localhost";$username = "username";$password = "password";$dbname = "myDB";

// Create connection$conn = new mysqli($servername, $username, $password, $dbname);// Check connectionif ($conn->connect_error) { die("Connection failed: " . $conn->connect_error);}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)VALUES ('John', 'Doe', '[email protected]')";

if ($conn->query($sql) === TRUE) { echo "New record created successfully";} else { echo "Error: " . $sql . "<br>" . $conn->error;}

$conn->close();?>

Page 14: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

Example (MySQLi Procedural)<?php

$servername = "localhost";$username = "username";$password = "password";$dbname = "myDB";

// Create connection$conn = mysqli_connect($servername, $username, $password, $dbname);// Check connectionif (!$conn) { die("Connection failed: " . mysqli_connect_error());}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)VALUES ('John', 'Doe', '[email protected]')";

if (mysqli_query($conn, $sql)) { echo "New record created successfully";} else { echo "Error: " . $sql . "<br>" . mysqli_error($conn);}

mysqli_close($conn);?>

Page 15: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

Example (PDO)<?php

$servername = "localhost";$username = "username";$password = "password";$dbname = "myDBPDO";

try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]')"; // use exec() because no results are returned $conn->exec($sql); echo "New record created successfully"; }catch(PDOException $e) { echo $sql . "<br>" . $e->getMessage(); }

$conn = null;?>

Page 16: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

Select Data With MySQLi

• Example (MySQLi Procedural)• Example (MySQLi Object-oriented)

Page 17: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

Example (MySQLi Object-oriented)<?php

$servername = "localhost";$username = "username";$password = "password";$dbname = "myDB";

// Create connection$conn = new mysqli($servername, $username, $password, $dbname);// Check connectionif ($conn->connect_error) { die("Connection failed: " . $conn->connect_error);}

$sql = "SELECT id, firstname, lastname FROM MyGuests";$result = $conn->query($sql);

if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; }} else { echo "0 results";}$conn->close();?>

Page 18: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

Example (MySQLi Procedural)<?php$servername = "localhost";$username = "username";$password = "password";$dbname = "myDB";

// Create connection$conn = mysqli_connect($servername, $username, $password, $dbname);// Check connectionif (!$conn) { die("Connection failed: " . mysqli_connect_error());}

$sql = "SELECT id, firstname, lastname FROM MyGuests";$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; }} else { echo "0 results";}

mysqli_close($conn);?>

Page 19: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

PHP Delete Data From MySQL

• Example (MySQLi Object-oriented)• Example (MySQLi Procedural)

Page 20: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

Example (MySQLi Object-oriented)<?php

$servername = "localhost";$username = "username";$password = "password";$dbname = "myDB";

// Create connection$conn = new mysqli($servername, $username, $password, $dbname);// Check connectionif ($conn->connect_error) { die("Connection failed: " . $conn->connect_error);}

// sql to delete a record$sql = "DELETE FROM MyGuests WHERE id=3";

if ($conn->query($sql) === TRUE) { echo "Record deleted successfully";} else { echo "Error deleting record: " . $conn->error;}

$conn->close();?>

Page 21: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

Example (MySQLi Procedural)<?php

$servername = "localhost";$username = "username";$password = "password";$dbname = "myDB";

// Create connection$conn = mysqli_connect($servername, $username, $password, $dbname);// Check connectionif (!$conn) { die("Connection failed: " . mysqli_connect_error());}

// sql to delete a record$sql = "DELETE FROM MyGuests WHERE id=3";

if (mysqli_query($conn, $sql)) { echo "Record deleted successfully";} else { echo "Error deleting record: " . mysqli_error($conn);}

mysqli_close($conn);?>

Page 22: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

PHP Update Data in MySQL

• Example (MySQLi Object-oriented)• Example (MySQLi Procedural)

Page 23: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

Example (MySQLi Object-oriented)<?php

$servername = "localhost";$username = "username";$password = "password";$dbname = "myDB";

// Create connection$conn = new mysqli($servername, $username, $password, $dbname);// Check connectionif ($conn->connect_error) { die("Connection failed: " . $conn->connect_error);}

$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

if ($conn->query($sql) === TRUE) { echo "Record updated successfully";} else { echo "Error updating record: " . $conn->error;}

$conn->close();?>

Page 24: PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.

Example (MySQLi Procedural)<?php

$servername = "localhost";$username = "username";$password = "password";$dbname = "myDB";

// Create connection$conn = mysqli_connect($servername, $username, $password, $dbname);// Check connectionif (!$conn) { die("Connection failed: " . mysqli_connect_error());}

$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

if (mysqli_query($conn, $sql)) { echo "Record updated successfully";} else { echo "Error updating record: " . mysqli_error($conn);}

mysqli_close($conn);?>


Recommended