Đang chuẩn bị liên kết để tải về tài liệu:
php solutions dynamic web design made easy phần 10
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
. Nếu nó trỏ đếni hoặcT, bạn nên quay trở lại Chương 2 và làm theo những lời khuyên về loại bỏ một phiên bản cũ của PHP. Thật không may, điều này không luôn luôn làm việc,Giả sử tất cả mọi thứ diễn ra tốt đẹp, bỏ qua phần Nếu bạn không thể tìm thấy mysql và mysqli trong cấu hình | SOLUTIONS TO COMMON PHP MYSQL PROBLEMS If you run this query in the SQL tab of phpMyAdmin it produces the following result When you list the tables as a comma-separated list in the FROM clause MySQL performs a full join between the tables and the SELECT query succeeds only if there is a full match. However when you perform a left join MySQL includes records that have a match in the left table but not in the right one. Left and right refer to the order in which you perform the join. So rewrite the SELECT query like this SELECT title article filename caption FROM journal LEFT JOIN images ON journal.image_id images.image_id When you run it in phpMyAdmin you get all four articles like this As you can see MySQL populates the empty fields from the right table images with NULL. The LEFT JOIN syntax is as follows FROM column_name LEFT JOIN column_name ON matching_condition When the column names of the matching condition are the same in both tables you can use this alternative syntax FROM column_name LEFT JOIN column_name USING column_name Any WHERE clause comes after the LEFT JOIN. So to find the details for article_id 1 regardless of whether it has a match in image_id you rewrite the original SELECT query like this SELECT title article filename caption FROM journal LEFT JOIN images USING image_id WHERE article_id 1 So now you can rewrite the SQL query in details.php like this sql SELECT title article filename caption FROM journal LEFT JOIN images USING image_id WHERE journal.article_id article_id 14 415 PHP SOLUTIONS DYNAMIC WEB DESIGN MADE EASY If you click the More link to view the article that doesn t have an associated image you should now see the article correctly displayed as shown in Figure 14-12. The other articles should still display correctly too. The finished code is in details_lj_mysql.php details_lj_mysqli.php and details_lj_pdo.php. Figure 14-12. Using a left join also retrieves articles that don t have a matching image_id as a foreign key. Creating an .