Trong đoạn mã này sửa đổi, chỉ một phần năm của các cuộc gọi được thực hiện cho một hàm MySQL-gọi (một 80 toàn phần trăm ít), vì mỗi hàng được lấy toàn bộ bằng cách sử dụng chức năng mysql_fetch_row. Điều này trả về một dòng dữ liệu trong một mảng, sau đó được gán cho biến $ row. | Fetching a row It was important to show how you can fetch a single cell of data from MySQL but now let s look at a much more efficient method. So replace the for loop of in Example 10-5 with the new loop in Example 10-6 and you will find that you get exactly the same result that was displayed in Figure 10-1. Example 10-6. Replacement for loop for fetching results one row at a time php for j 0 j rows j row mysql_fetch_row result echo Author . row o . . br echo Title . row 1 . . br echo Category . row z . . br echo Year . row s . . br echo ISBN . row 4 . br br In this modified code only one-fifth of the calls are made to a MySQL-calling function a full 80 percent less because each row is fetched in its entirety using the mysql_fetch_row function. This returns a single row of data in an array which is then assigned to the variable row. All that s necessary then is to reference each element of the array row in turn starting at an offset of zero . Therefore row 0 contains the Author data row 1 the Title and so on because each column is placed in the array in the order in which it appears in the MySQL table. Also by using mysql_fetch_row instead of mysql_result you use substantially less PHP code and achieve much faster execution time due to simply referencing each item of data by offset rather than by name. Closing a connection When you have finished using a database you should close the connection. This is done by issuing the command in Example 10-7. Example 10-7. Closing a MySQL database connection php mysql_close db_server We have to pass the identifier returned by mysql_connect back in Example 10-2 which we stored in the variable db_server. Querying a MySQL Database with PHP 231 All database connections are automatically closed when PHP exits so it doesn t matter that the connection wasn t closed in Example 10-5. 1 But in longer programs where you may continually open and close database connections you are strongly advised to close each one as soon as .