How to use PDO to manage databases?

autoload
Release: 2023-03-08 13:24:01
Original
1744 people have browsed it

Concept: Database management is divided intowrite operationsandquery operations.

, that is, usePDOto implement the database increase and deletion and modification operation. During the operation process, we must consider the error treatment ofSQLitself, and the operation processing of the results. .

Query operation, that is, obtaining corresponding data from the database by executingSQL instructions.

1. Initialize PDO, each operation requires PDO instantiation: encapsulated execution.

//创建pdo_function.php文件 exec("set names utf8"); echo "连接初始化数据库成功"; return $pdo; } ?>
Copy after login

2.SQLUsually should be passed in from the outside. What is needed externally is only the result, regardless of the process, so secondary encapsulation must be considered during actual development. That is,SQLis passed in externally, executed internally and controls errors, and finally returns the result.

在pdo_function.php中 //对数据库进行写操作 function pdo_exec($pdo,$sql) { $result=$pdo->exec($sql); if($result===false) { echo "SQL语句错误"."
"; echo "SQL错误代码:".$pdo->errorcode()."
"; echo "SQL错误编号:".$pdo->errorInfo()[2]."
"; exit; } return $result; } //对数据库进行读操作 function pdo_query($pdo,$sql) { $statement=$pdo->query($sql); if($statement===false) { echo "SQL语句错误"."
"; echo "SQL错误代码:".$pdo->errorcode()."
"; echo "SQL错误编号:".$pdo->errorInfo()[2]."
"; exit; } return $statement; }
Copy after login

3. Specific implementation of write operation

";//输出产生影响的行数。 ?>
Copy after login

4. Specific implementation of query operation

Copy after login

Recommendation:php tutorial,php video tutorial

The above is the detailed content of How to use PDO to manage databases?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!