Concept: Database management is divided intowrite operations
andquery operations
.
, that is, usePDO
to implement the database increase and deletion and modification operation. During the operation process, we must consider the error treatment ofSQL
itself, 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; } ?>
2.SQL
Usually 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,SQL
is 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; }
3. Specific implementation of write operation
";//输出产生影响的行数。 ?>
4. Specific implementation of query operation
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!