Top Banner
Lecture 14 Kanida Sinmai [email protected] http://mis.csit.sci.tsu.ac.th/kanida PHP: MySQL
16

PHP: MySQL. 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.

Jan 21, 2016

Download

Documents

Eric O'Brien
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: PHP: MySQL. 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.

Lecture 14Kanida Sinmai

[email protected]://mis.csit.sci.tsu.ac.th/kanida

PHP: MySQL

Page 2: PHP: MySQL. 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.

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)

• Earlier versions of PHP used the MySQL extension. However, this extension was deprecated in 2012.

Page 3: PHP: MySQL. 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.

Example<?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 4: PHP: MySQL. 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.

Create a database<?php$servername = "localhost";$username = "username";$password = "password";

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

// Create database$sql = "CREATE DATABASE myDB";if (mysqli_query($conn, $sql)) { echo "Database created successfully";} else { echo "Error creating database: " . mysqli_error($conn);}

mysqli_close($conn);?>

Page 5: PHP: MySQL. 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.

Create a table<?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 create table$sql = "CREATE TABLE MyGuests (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL,lastname VARCHAR(30) NOT NULL,email VARCHAR(50),reg_date TIMESTAMP)";

if (mysqli_query($conn, $sql)) { echo "Table MyGuests created successfully";} else { echo "Error creating table: " . mysqli_error($conn);}

mysqli_close($conn);?>

Page 6: PHP: MySQL. 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.

Close the Connection• mysqli_close($conn);

Page 7: PHP: MySQL. 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.

PHP Insert Data Into MySQL<?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 8: PHP: MySQL. 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.

PHP Get ID of Last Inserted Record<?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)) { $last_id = mysqli_insert_id($conn); echo "New record created successfully. Last inserted ID is: " . $last_id;} else { echo "Error: " . $sql . "<br>" . mysqli_error($conn);}mysqli_close($conn);?>

Page 9: PHP: MySQL. 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.

PHP Select Data From MySQLSELECT column_name(s) FROM table_name

or we can use the * character to select ALL columns from a table:SELECT * FROM table_name

Page 10: PHP: MySQL. 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.

<?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 11: PHP: MySQL. 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.

PHP Delete Data From MySQLDELETE FROM table_nameWHERE some_column = some_value

Page 12: PHP: MySQL. 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.

<?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 13: PHP: MySQL. 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.

PHP Update Data in MySQLUPDATE table_nameSET column1=value, column2=value2,...WHERE some_column=some_value

Page 14: PHP: MySQL. 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.

<?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);?>

Page 15: PHP: MySQL. 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.

PHP Limit Data Selections From MySQL

$sql = "SELECT * FROM Orders LIMIT 30";

$sql = "SELECT * FROM Orders LIMIT 10 OFFSET 15";

Page 16: PHP: MySQL. 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.

แบบฝึ�กหั�ด• สร้�างฐานข้�อมู�ล 1 ฐานข้�อมู�ล (myDB)• สร้�างตาร้าง 1 ตาร้าง (user) ปร้ะกอบด้�วย Field ที่��จำ�าเป�นเช่�น

username, password, ว�นที่��สมู�คร้สมูาช่!ก, ปร้ะเภที่ข้องสมูาช่!ก, …..

• สร้�างฟอร้$มูร้�บข้�อมู�ลด้�งกล�าว• บ�นที่�กข้�อมู�ลลงฐานข้�อมู�ลโด้ยใช่� php script• แสด้งข้�อมู�ลผู้��ใช่�ที่� )งหมูด้• ผู้��ใช่�สามูาร้ถเปล��ยนร้ห�สผู้�านได้�