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

CONNECTING TO MYSQL WITH PHP AND SQL In both cases, the result of the query is stored in $stmt. Error messages can be accessed in the same way as with a PDO connection. However, instead of calling the errorInfo() method on the connection object, use it on the PDO statement like this: $error = $stmt-errorInfo(); if (isset($error[2])) { echo $error[2]; } To bind the results of a SELECT query to variables, each column needs to bound separately using the bindColumn() method before calling execute(). The bindColumn() method takes two arguments. The first argument can be either the name of the column. | CONNECTING TO MYSQL WITH PHP AND SQL In both cases the result of the query is stored in stmt. Error messages can be accessed in the same way as with a PDO connection. However instead of calling the errorInfo method on the connection object use it on the PDO statement like this error stmt- errorInfo if isset error 2 echo error 2 To bind the results of a SELECT query to variables each column needs to bound separately using the bindColumn method before calling execute . The bindColumn method takes two arguments. The first argument can be either the name of the column or its number counting from 1. The number comes from its position in the SELECT query not the order it appears in the database table. So to bind the result from the filename column to filename either of the following is acceptable stmt- bindColumn filename filename stmt- bindColumn 2 filename PHP Solution 11-9 Using a PDO prepared statement in a search This PHP solution shows how to embed the user-submitted value from a search form into a SELECT query with a PDO prepared statement. It uses the same search form as the MySQLi versions in PHP Solutions 11-7 and 11-8. 1. Copy from the ch11 folder and save it in the mysql folder as . 2. Add the following code in a PHP block above the DOCTYPE declaration if isset _GET go require_once . includes conn dbConnect read pdo sql SELECT image_id filename caption FROM images WHERE caption LIKE search searchterm . _GET search . stmt conn- prepare sql stmt- bindParam search searchterm PDO PARAM_STR stmt- bindColumn image_id image_id stmt- bindColumn filename filename stmt- bindColumn 3 caption stmt- execute numRows stmt- rowCount When the form is submitted this includes the connection file and creates a PDO connection to MySQL. The prepared statement uses search as a named parameter in place of the user-submitted value. Like MySQLi prepared statements you need to add the wildcard characters to the search term before .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã 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.