Bài giảng Lập trình Web - Chapter 5: PHP and MySQL - Nguyễn Hữu Hiếu

Chapter 5  
PHP and MySQL  
Lectured by:  
Nguyễn Hữu Hiếu  
Objectives  
In this lesson, you will:  
• Connect to MySQL from PHP  
• Work with MySQL databases using PHP  
• Create, modify, and delete MySQL tables with  
PHP  
• Use PHP to manipulate MySQL records  
• Use PHP to retrieve database records  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
2
2
Connecting to MySQL with PHP  
• PHP has the ability to access and manipulate  
any database that is ODBC (Open Database  
Connectivity) compliant  
• PHP includes functionality that allows you to  
work directly with different types of databases,  
without going through ODBC  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
3
3
Which MySQL Package to Use  
• The mysqli (MySQL Improved) package became  
available with PHP 5 and is designed to work with  
MySQL version 4.1.3 and later  
• Earlier versions must use the mysql package  
• The mysqli package is the object-oriented equivalent of  
the mysql package but can also be used procedurally  
• Mysqli package has improved speed, security and  
compatibility with libraries.  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
4
4
Opening and Closing a Connection  
• Open a connection to a MySQL database server  
with the mysqli_connect() function  
• The mysqli_connect() function returns a positive  
integer if it connects to the database  
successfully or FALSE if it does not  
• Assign the return value from the  
mysqli_connect() function to a variable that you  
can use to access the database in your script  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
5
5
Opening and Closing a Connection  
• The syntax for the mysqli_connect()  
function is:  
$connection = mysqli_connect("host" [, "user",  
"password"[,”database”]]);  
• The host argument specifies the host name  
where your MySQL database server is installed  
• The user and password arguments specify a MySQL  
account name and password  
• You can optionally select the database when connecting.  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
6
6
Opening and Closing a Connection  
• The database connection is assigned to the $DBConnect  
variable  
$DBConnect = mysqli_connect("localhost",  
"billyeakus ", "hotdog");  
•
Close a database connection using the mysql_close()  
function  
mysqli_close($DBConnect);  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
7
7
Opening and Closing a Connection  
mysqli_get_client_info()  
mysqli_get_client_stats()  
mysqli_get_client_version()  
Returns the MySQL client library version  
Returns statistics about client per-process  
Returns the MySQL client library version as an integer  
mysqli_get_connection_stats() Returns statistics about the client connection  
mysqli_get_host_info()  
Returns the MySQL server hostname and the  
connection type  
mysqli_get_proto_info()  
mysqli_get_server_info()  
Returns the MySQL protocol version  
Returns the MySQL server version  
mysqli_get_server_version() Returns the MySQL server version as an integer  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
8
8
Opening and Closing a Connection  
version.php in a Web browser  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
9
9
Reporting MySQL Errors  
• Reasons for not connecting to a database  
server include:  
– The database server is not running  
– Insufficient privileges to access the data source  
– Invalid username and/or password  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
10  
10  
Reporting MySQL Errors  
• The mysqli_errno() function returns the error code from  
the last attempted MySQL function call or 0 if no error  
occurred  
• The mysqli_error() — Returns the text of the error  
message from previous MySQL operation  
• The mysqli_errno() and mysqli_error() functions return  
the results of the previous mysqli*() function  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
11  
11  
Selecting a Database  
• The syntax for the mysqli_select_db() function  
is:  
mysqli_select_db(connection, database);  
• The function returns a value of TRUE if it  
successfully selects a database or FALSE if it  
does not  
• For security purposes, you may choose to use  
an include file to connect to the MySQL server  
and select a database  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
12  
12  
Sample Code  
good  
$link = mysqli_connect("cs.mvnu.edu",  
“demo", “demo");  
mysqli_select_db($link, "nonexistentdb”);  
echo mysql_errno($link) . ": " .  
mysql_error($link). "<br>";  
bad  
mysqli_select_db( $link, “demo");  
mysqli_query($link,  
good  
"SELECT * FROM nonexistenttable");  
echo mysqli_errno($link) . ": " .  
mysqli_error($link) . "<br>";  
bad  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
13  
13  
Sample Code  
$host='localhost';  
$userName = 'demo';  
$password = 'demo';  
$database ='demo';  
$link = mysqli_connect ($host, $userName, $password) ;  
if (!$link) {  
die('Could not connect: ' . mysqli_error($link));  
}
echo 'Connected successfully';  
mysqli_close($link);  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
14  
14  
include(‘file.php’); include_once(‘file.php’)  
require(‘file.php’); require_once(‘file.php’);  
index.php  
<?php  
include_once(‘config/file.php’);  
echo “test”;  
include(‘config/file.php’);  
?>  
config/file.php  
<?php  
include(‘file2.php’);  
?>  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
15  
Sample Code  
<?php  
$link = mysqli_connect('localhost', 'mysql_user', 'mys  
ql_password');  
if (!$link) {  
die('Not connected : ' . mysqli_error($link));  
}
// make foo the current db  
$db_selected = mysqli_select_db($link,'foo');  
if (!$db_selected) {  
die ('Can\'t use foo : ' . mysqli_error($link));  
}
?>  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
16  
16  
Executing SQL Statements  
• Use the mysqli_query() function to send SQL  
statements to MySQL  
• The syntax for the mysqli_query() function is:  
mysqli_query(connection, query);  
• The mysqli_query() function returns one of three  
values:  
– For SQL statements that do not return results  
(CREATE DATABASE and CREATE TABLE  
statements) it returns a value of TRUE if the  
statement executes successfully  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
17  
17  
Executing SQL Statements  
– For SQL statements that return results (SELECT  
and SHOW statements) the mysqli_query()  
function returns a result pointer that represents  
the query results  
•A result pointer is a special type of variable that  
refers to the currently selected row in a resultset  
– The mysqli_query() function returns a value  
of FALSE for any SQL statements that fail,  
regardless of whether they return results  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
18  
18  
Sample Code  
<?php  
// This could be supplied by a user, for example  
$firstname = 'fred';  
$lastname = 'fox';  
//never trust user data  
$firstname= mysql_real_escape_string($firstname);  
$lastname= mysql_real_escape_string($lastname);  
// Formulate Query  
// For more examples, see mysql_real_escape_string()  
$query = "SELECT firstname, lastname, address, age FROM friends WHERE firstname=‘$firstname ‘  
AND lastname= ‘$lastname’”;  
// Perform Query  
$result = mysql_query($query);  
// Check result  
// This shows the actual query sent to MySQL, and the error. Useful for debugging.  
if (!$result) {  
$message = 'Invalid query: ' . mysql_error() . "<br>";  
$message .= 'Whole query: ' . $query;  
die($message);  
}
// Use result  
// Attempting to print $result won't allow access to information in the resource  
// One of the mysql result functions must be used  
// See also mysql_fetch_array(), mysql_fetch_row(), etc.  
while ($row = mysql_fetch_assoc($result)) {  
echo $row['firstname'];  
echo $row['lastname'];  
echo $row['address'];  
echo $row['age'];  
}
// Free the resources associated with the result set  
// This is done automatically at the end of the script  
mysql_free_result($result);  
?>  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
19  
19  
Adding, Deleting, and Updating Records  
• To add records to a table, use the INSERT and  
VALUES keywords with the mysqli_query()  
function  
• To add multiple records to a database, use the  
LOAD DATA statement with the name of the  
local text file containing the records you want to  
add  
• To update records in a table, use the UPDATE  
statement  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2020  
Lập Trình Web  
20  
20  
Tải về để xem bản đầy đủ
pdf 60 trang myanh 15660 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Lập trình Web - Chapter 5: PHP and MySQL - Nguyễn Hữu Hiếu", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_lap_trinh_web_chapter_5_php_and_mysql_nguyen_huu_h.pdf