Giải pháp thiết kế web động với PHP - p 41

MANAGING CONTENT You can rewrite it like this: $result = @ $conn-query($sql); if (!$result) { // redirect to custom error page } You should also remove the conditional statements surrounding MySQLi prepared statements once you have verified that they don t generate SQL syntax errors. For example, your development code might look like this: if ($stmt-prepare($sql)) { $stmt-bind_param('s', $searchterm); $stmt-bind_result($image_id, $filename, $caption); $stmt-execute(); $stmt-store_result(); $numRows = $stmt-num_rows; } else { echo $stmt-error; } To deploy it on a live website, change it to this: $stmt-prepare($sql); $stmt-bind_param('s', $searchterm); $stmt-bind_result($image_id, $filename, $caption); $stmt-execute(); $stmt-store_result(); $numRows = $stmt-num_rows; Chapter review Content management with a database involves. | MANAGING CONTENT You can rewrite it like this result @ conn- query sql if result redirect to custom error page You should also remove the conditional statements surrounding MySQLi prepared statements once you have verified that they don t generate SQL syntax errors. For example your development code might look like this if stmt- prepare sql stmt- bind_param s searchterm stmt- bind_result image_id filename caption stmt- execute stmt- store_result numRows stmt- num_rows else echo stmt- error To deploy it on a live website change it to this stmt- prepare sql stmt- bind_param s searchterm stmt- bind_result image_id filename caption stmt- execute stmt- store_result numRows stmt- num_rows Chapter review Content management with a database involves inserting selecting updating and deleting records. Each record s primary key plays a vital role in the update and delete processes. Most of the time generating the primary key is handled automatically by MySQL when a record is first created. Thereafter finding a record s primary key is simply a matter of using a SELECT query either by displaying a list of all records or by searching for something you know about the record such as a title or words in an article. MySQLi and PDO prepared statements make database queries more secure by removing the need to ensure that quotes and control characters are properly escaped. They also speed up your application if the same query needs to be repeated during a script using different variables. Instead of validating the SQL every time the script needs do it only once with the placeholders. Although this chapter has concentrated on content management the same basic techniques apply to most interaction with a database. Of course there s a lot more to SQL and to PHP. In the next chapter I ll address some of the most common problems such as displaying only the first sentence or so of a long text field and handling dates. Then in Chapter 15 and 16 we ll explore working with more than one table in

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
50    110    5    18-05-2024
98    102    2    18-05-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.