Professional Documents
Culture Documents
$User = $_GET['username'];
$Password = $_GET['password'];
$DBConnect = @mysqli_connect("localhost", $User, $Password)
Or die("<p>Unable to connect to the database server.</p>"
. "<p>Error code " . mysqli_connect_errno()
. ": " . mysqli_connect_error()) . "</p>";
echo "<p>Successfully connected to the database server.</p>";
@mysqli_select_db($DBConnect, "flightlog")
Or die("<p>The database is not available.</p>");
echo "<p>Successfully opened the database.</p>";
// additional statements that access the database
mysqli_close($DBConnect);
Hình 9-4 Con số lỗi và thông báo lỗi username and password
$User = $_GET['username'];
$Password = $_GET['password'];
$DBConnect = @mysqli_connect("localhost", $User, $Password)
Or die("<p>Unable to connect to the database server.</p>"
. "<p>Error code " . mysqli_connect_errno()
. ": " . mysqli_connect_error()) . "</p>";
echo "<p>Successfully connected to the database server.</p>";
@mysqli_select_db($DBConnect, "flightplan")
Or die("<p>Unable to select the database.</p>"
. "<p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect)) . "</p>";
echo "<p>Successfully opened the database.</p>";
// additional statements that access the database
mysqli_close($DBConnect);
Hình 9-5 mã lỗi và thông báo khi cố gắng chọn CSDL không tồn tại
HÌnh 9-6 Kết Xuất của bảng kê trong một Web browser
PHP Programming with MySQL 25
Truy cập các bản ghi đến mảng kết hợp
(Retrieving Records into an Associative Array)
Hình 9-8 Kết xuất con số của các dòng và các trường
trả về từ một truy vấn
PHP Programming with MySQL 29
Đóng kết qủa Truy vấn
(Closing Query Results)
• Khi hoàn thành làm việc với những kết quả truy vấn truy
cập với hàm mysqli_query(), dùng hàm
mysqli_free_result() để đóng tập kết quả .
• Để đóng tập kết quả, dùng hàm
mysqli_free_result() các biến chứa con trỏ kết
quả từ hàm mysqli_query()
$DBName = "real_estate";
...
if (@!mysqli_select_db($DBConnect, $DBName))
echo "<p>The $DBName database does not exist!</p>";
else {
$SQLstring = "DROP DATABASE $DBName";
$QueryResult = @mysqli_query($DBConnect, $SQLstring)
Or die("<p>Unable to execute the query.</p>"
. "<p>Error code “ . mysqli_errno($DBConnect)
. ": “ . mysqli_error($DBConnect)) . "</p>";
echo "<p>Successfully deleted the database.</p>";
}
mysqli_close($DBConnect);
$DBName = "real_estate";
...
$SQLstring = "CREATE TABLE commercial (city VARCHAR(25), state
VARCHAR(25), sale_or_lease VARCHAR(25), type_of_use VARCHAR(40),
Price INT, size INT)";
$QueryResult = @mysqli_query($DBConnect, $SQLstring)
Or die("<p>Unable to execute the query.</p>"
. "<p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect)) . "</p>";
echo "<p>Successfully created the table.</p>";
mysqli_close($DBConnect);
HÌnh 9-17 Kết xuất của hàm mysqli_info() cho truy vấn
INSERT để thêm nhiều bản ghi .
PHP Programming with MySQL 47
Sử dụng hàm mysqli_info()
• Hàm mysqli_info() cũng trả về thông tin cho
các truy vấn LOAD DATA
$SQLstring = "LOAD DATA LOCAL INFILE 'c:/temp/inventory.txt'
INTO TABLE inventory;";
$QueryResult = @mysqli_query($DBConnect, $SQLstring)
Or die("<p>Unable to execute the query.</p>"
. "<p>Error code “ . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect)) . "</p>";
echo "<p>Successfully added the records.</p>";
echo "<p>" . mysqli_info($DBConnect) . "</p>";
Hình 9-18 Kết xuất của hàm mysqli_info() cho truy vấn LOAD DATA