Phát triển ứng dụng mã nguồn mở - Bài 3.1: PHP và mysql

• Cài đặt php, mysql và php-mysql. • Kết nối PHP với MySQL khai thác cơ sở dữ liệu. • Phối hợp Netbeans và Web Server • Ví dụ.

pdf24 trang | Chia sẻ: thuychi16 | Lượt xem: 809 | Lượt tải: 1download
Bạn đang xem trước 20 trang tài liệu Phát triển ứng dụng mã nguồn mở - Bài 3.1: PHP và mysql, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Đoàn Thiện Ngân Bài 3.1 - 1/24 Bài 3.1 PHP & MySQL GV: ĐOÀN THIỆN NGÂN Đoàn Thiện Ngân Bài 3.1 - 2/24 Nội dung • Cài đặt php, mysql và php-mysql. • Kết nối PHP với MySQL khai thác cơ sở dữ liệu. • Phối hợp Netbeans và Web Server • Ví dụ. Đoàn Thiện Ngân Bài 3.1 - 3/24 Tài liệu tham khảo 1. PHP Manual. 2. W3schools 3. MySQL Manual. 4. Beginning PHP and MySQL: From Novice to Professional, 4th Edition, W. Jason Gilmore, 2010 5. PHP Application Development with NetBeans Beginner's Guide, M.A. Hossain Tonu, Packt Publishing, 2012 Đoàn Thiện Ngân Bài 3.1 - 4/24 PHP - MySQL Linux • Cài mysql và mysql-server. • Cài Apache web server, PHP • Để dùng MySQL kèm PHP, cài thêm php- mysql (yum tự động chọn thêm php-pdo) MS Windows: • Cài theo thứ tự Apache, php và MySQL • dùng WAMP, XAMP, APPSERVE, Kiểm tra với trang phpinfo() có mặt mysql và mysqli trong cấu hình PHP (hay Kiểm tra cấu hình /etc/php.ini) Đoàn Thiện Ngân Bài 3.1 - 5/24 Kiểm tra phpinfo() Đoàn Thiện Ngân Bài 3.1 - 6/24 Chuẩn bị MySQL User • MySQL có sẵn user root (dùng thử nghiệm kết nối PHP qua MySQL) • Phải đặt mật khẩu cho root để tăng tính bảo mật khi truy cập qua mạng (bắt buộc trước khi thử các trang web). • Có thể dùng lệnh hay Manage Security của MySQL Workbench . $ mysql –u root mysql> set password for root@localhost = password('secret'); Đoàn Thiện Ngân Bài 3.1 - 7/24 Manage Security • Hay dùng MySQL Workbench – Server Administration – Manage Security • Dùng gedit (Bluefish, Netbeans, ) thử soạn thảo các trang php kết nối với MySQL. Đoàn Thiện Ngân Bài 3.1 - 8/24 Đoàn Thiện Ngân Bài 3.1 - 9/24 Netbeans & WAMP server • Tạo thư mục trong wamp server (wwwroot của Apache server) hay tạo alias (xem Apache manual tạo alias) lưu Netbeans projects để dễ thử các chương trình php. • Netbeans (hay hơn Expression Web 4): – Tạo project PHP – Tạo files trong thư mục project – Có thể tạo index.html trong thư mục project để dễ gọi trang web cần thử. – Có cơ chế intellisense code hỗ trợ lập trình PHP rất tốt – dễ dàng cấu hình thử nghiệm với webserver Đoàn Thiện Ngân Bài 3.1 - 10/24 Application Programming Interface • An Application Programming Interface, or API, defines the classes, methods, functions and variables that your application will need to call in order to carry out its desired task. • In the case of PHP applications that need to communicate with databases the necessary APIs are usually exposed via PHP extensions. Đoàn Thiện Ngân Bài 3.1 - 11/24 What is an Extension? • An PHP extension typically exposes an API to the PHP programmer, to allow its facilities to be used programmatically. However, some extensions which use the PHP extension framework do not expose an API to the PHP programmer. • The PDO MySQL driver extension, for example, does not expose an API to the PHP programmer, but provides an interface to the PDO layer above it. • The terms API and extension should not be taken to mean the same thing, as an extension may not necessarily expose an API to the programmer. Đoàn Thiện Ngân Bài 3.1 - 12/24 PHP's MySQL Extension • Three main API options when considering connecting to a MySQL database server: – PHP's MySQL Extension (old, not good) – PHP's mysqli Extension – PHP Data Objects (PDO) • Each has its own advantages and disadvantages. Đoàn Thiện Ngân Bài 3.1 - 13/24 MySQLI Extension • The mysqli extension allows you to access the functionality provided by MySQL 4.1 and above. • The mysqli extension has a number of benefits, the key enhancements over the mysql extension being: – Object-oriented interface – Support for Prepared Statements – Support for Multiple Statements – Support for Transactions – Enhanced debugging capabilities – Embedded server support Đoàn Thiện Ngân Bài 3.1 - 14/24 Using the mysqli Extension • PHP’s mysqli extension offers all of the functionality provided by its predecessor, in addition to new features that have been added as a result of MySQL’s evolution into a full-featured database server. • This section shows you how to use the mysqli extension to – connect to the database server – query for and retrieve data – Đoàn Thiện Ngân Bài 3.1 - 15/24 Các hàm mysqli • mysqli_connect(); mysqli_connect_errno(); mysqli_connect_error(); mysqli_fetch_array(); mysqli_fetch_object(); mysqli_fetch_row(); mysqli_fetch_fields(); mysqli_fetch_field(); • Chú ý: với mysqli có thể dùng OOP hay dùng hàm (xem ví dụ kèm theo) Đoàn Thiện Ngân Bài 3.1 - 16/24 connectmysql.php <?php // Connect to the database server - OOP $mysqli = new mysqli('localhost', 'root', 'secret', 'sakila'); if ($mysqli->connect_errno) { printf("Unable to connect to the database:%s", $mysqli->connect_error); exit(); } else { echo "Good connection"; } ?> Đoàn Thiện Ngân Bài 3.1 - 17/24 conmysql2.php <?php $HOST='localhost'; $USER='root'; $PWD='secret'; $DB='sakila'; $mysqli=new mysqli($HOST,$USER,$PWD,$DB); //$mysqli = new mysqli('localhost', 'root', 'secret', 'sakila'); ?> Đoàn Thiện Ngân Bài 3.1 - 18/24 dbconfig.php • Thường dùng tập tin cấu hình db lưu dạng *.php và dùng require_once để gọi. <?php $HOST='localhost'; $USER='root'; $PWD='secret'; $DB='sakila'; ?> Đoàn Thiện Ngân Bài 3.1 - 19/24 mysqli_oop.php require_once 'dbconfig.php'; $mysqli = new mysqli($HOST,$USER,$PWD,$DB); if (! $mysqli->connect_errno) {// Create the query $query = 'SELECT * FROM actor ORDER by actor_id'; // Send the query to MySQL $result = $mysqli->query($query, MYSQLI_STORE_RESULT); // Iterate through the result set while(list($actor_id, $first_name, $last_name) = $result->fetch_row()) printf("(%s) %s: \$%s ", $actor_id, $first_name, $last_name); } else echo “Can't connect to $DB”.$mysqli->connect_error; Đoàn Thiện Ngân Bài 3.1 - 20/24 mysqli_function.php require_once 'dbconfig.php'; $mycon=mysqli_connect($HOST,$USER,$PWD,$DB); if (!mysqli_connect_errno()) { // Create the query $query = 'SELECT * FROM actor ORDER by actor_id'; // Send the query to MySQL $result = mysqli_query($mycon, $query); // Iterate through the result set while(list($actor_id, $first_name, $last_name) = $result->fetch_row()) printf("(%s) %s: \$%s ", $actor_id, $first_name, $last_name); } else echo “Can't connect to $DB”.mysqli_connect_error(); Đoàn Thiện Ngân Bài 3.1 - 21/24 Tabledatamysql.php $query = 'SELECT * FROM actor ORDER by actor_id'; // Send the query to MySQL $result = $mysqli->query($query, MYSQLI_STORE_RESULT); // Iterate through the result set echo "\n"; while($line = $result->fetch_row()) { echo "\t\n"; foreach ($line as $col_value) echo "\t\t$col_value\n"; echo "\t\n"; } echo "\n"; Đoàn Thiện Ngân Bài 3.1 - 22/24 mysqli_result::fetch_array if ($mysqli->connect_errno) { printf("Failed to connect to MySQL: %s\n", $mysqli->connect_error); exit(); } $sql = "select * from actor where actor_id < 10 order by actor_id;"; $result = $mysqli->query($sql, MYSQLI_STORE_RESULT); // Fetch while ($data = $result->fetch_array()) { // while ($data = $result->fetch_array(MYSQLI_BOTH)) // while ($data = $result->fetch_array(MYSQLI_ASSOC)) echo ""; print_r($data); } // Free result set $result->free(); $mysqli->close(); Đoàn Thiện Ngân Bài 3.1 - 23/24 mysqli_fetch_array $con=mysqli_connect($HOST, $USER, $PWD, $DB); // Check connection if (mysqli_connect_errno()) { printf("Failed to connect to MySQL: %s\n", mysqli_connect_error()); exit(); } $sql = "SELECT * FROM actor WHERE actor_id < 10 ORDER by actor_id;"; $result = mysqli_query($con,$sql, MYSQLI_STORE_RESULT); // Fetch while ($data = mysqli_fetch_array($result)) { // while ($data = mysqli_fetch_array($result,MYSQLI_ASSOC)) { // while ($data = mysqli_fetch_array($result, MYSQLI_BOTH)) { echo ""; print_r($data); } // Free result set mysqli_free_result($result); mysqli_close($con); Đoàn Thiện Ngân Bài 3.1 - 24/24 ??? • MySQL và PHP –Đặt mật khẩu cho mySQL DBA root – Cài đặt và kiểm tra sự hỗ trợ MySQL trong Apache và PHP – Kết nối với MySQL qua trang PHP – Sử dụng mysqli truy cập dữ liệu với MySQL qua trang PHP – Thử viết trang PHP: Insert, Update, Delete – Thử dùng Views, Stored routines với MySQL.
Tài liệu liên quan