Tài liệu Lập trình web với PHP - p12 - Pdf 87

Chương 3 : SỬ DỤNG PHP VỚI MYSQL

Ở chương 2, chúng ta đã tạo được trang Web với việc sử dụng những hàm. Nhưng
chúng ta chưa thực sự hiểu mối quan hệ giữa MySQL và PHP. Trong chương này chúng
ta sẽ xét kỹ điều này.
Trong chương này nói đến các vấn đề:
 Hiểu biết về cơ sở dữ liệu MYSQL.
 Những dữ liệu chứa trong MySQL.
 Tác động những thông tin đặc biệt, quyền từ Web site.
 Phần mềm quản lý bảng dễ dàng.
 Có thể sửa chữa code theo ý muốn.
Đọc, tạo cơ sở dữ liệu
Để tạo một cơ sở dữ liệu cho bạn cần 3 bảng:
 Một bảng movie: Ta sẽ lưu trữ tên và thông tin của movie.
 Một bảng movietype: Nơi lưu trữ những catalog của movie.
 Một bảng people: Ta sẽ lưu trữ tên của các diễn viên và đạo diễn.
Ví dụ tạo cơ sở dữ liệu và bảng
Tạo trang php với tên: createmovie.php
<?php
//connect to MySQL; note we‟ve used our own parameters-
you should use
//your own for hostname, user, and password
$connect = mysql_connect(“localhost”, “root”, “”) or
die (“Hey loser, check your server connection.”);
//create the main database if it doesn‟t already exist
$create = mysql_query(“CREATE DATABASE IF NOT
EXISTS moviesite”) or die(mysql_error());
//make sure our recently created database is the active one
mysql_select_db(“moviesite”);
//create “movie” table
$movie = “CREATE TABLE movie (

//connect to MySQL
$connect = mysql_connect(“localhost”, “root”, “”)
or die (“Hey loser, check your server connection.”);
//make sure we‟re using the right database
mysql_select_db(“moviesite”);
//insert data into “movie” table
$insert = “INSERT INTO movie (movie_id, movie_name,
movie_type, “ .
“movie_year, movie_leadactor, movie_director) “ .
“VALUES (1, „Bruce Almighty‟, 5, 2003, 1, 2), “ .
“(2, „Office Space‟, 5, 1999, 5, 6), “ .
“(3, „Grand Canyon‟, 2, 1991, 4, 3)”;
$results = mysql_query($insert)
or die(mysql_error());
//insert data into “movietype” table
$type = “INSERT INTO movietype (movietype_id,
movietype_label) “ .
“VALUES (1,‟Sci Fi‟), “ .
“(2, „Drama‟), “ .
“(3, „Adventure‟), “ .
“(4, „War‟), “ .
“(5, „Comedy‟), “ .
“(6, „Horror‟), “ .
“(7, „Action‟), “ .
“(8, „Kids‟)” ;
$results = mysql_query($type)
or die(mysql_error());
//insert data into “people” table
$people = “INSERT INTO people (people_id, people_fullname, “
.“people_isactor, people_isdirector) “ .

Bạn không thể sử dụng tham số AS với tham số WHERE, bởi vì nó bị giới hạn trong
MY SQL. Khi WHERE được thực thi thì cột giá trị không biết
 FROM: Tên bảng cần truy xuất thông tin
 WHERE: Danh sách điều kiện chọn lọc dữ liệu
 ORDER BY: Để sắp xếp dữ liệu trong vùng
 LIMIT: Giới hạn kết quả trả về


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status