PDO
v2.2.1
只是另一个PDO数据库库
使用作曲家
$ composer require faapz/ PDO
从users
表中选择、插入、更新和删除数据的示例。
require_once ' vendor/autoload.php ' ;
$ dsn = ' mysql:host=your_db_host;dbname=your_db_name;charset=utf8 ' ;
$ usr = ' your_db_username ' ;
$ pwd = ' your_db_password ' ;
$ database = new FaaPz PDO Database ( $ dsn , $ usr , $ pwd );
// SELECT * FROM users WHERE id = ?
$ select = $ database -> select ()
-> from ( ' users ' )
-> where ( new FaaPz PDO Clause Conditional ( ' id ' , ' = ' , 1234 ));
if ( $ insert -> execute ()) {
$ data = $ stmt -> fetch ();
}
// INSERT INTO users (id , username , password) VALUES (? , ? , ?)
$ insert = $ database -> insert (
' id ' ,
' username ' ,
' password '
)
-> into ( ' users ' )
-> values (
1234 ,
' user ' ,
' passwd '
);
if ( $ insert -> execute ()) {
$ insertId = $ database -> lastInsertId ();
}
// UPDATE users SET pwd = ? WHERE id = ?
$ update = $ database -> update ([ " pwd " => " your_new_password " ])
-> table ( " users " )
-> where ( new FaaPz PDO Clause Conditional ( " id " , " = " , 1234 ));
if (( $ result = $ insert -> execute ()) !== false ) {
$ affectedRows = $ result -> rowCount ();
}
// DELETE FROM users WHERE id = ?
$ delete = $ database -> delete ()
-> from ( " users " )
-> where ( new FaaPz PDO Clause Conditional ( " id " , " = " , 1234 ));
if (( $ result = $ delete -> execute ()) !== false ) {
$ affectedRows = $ result -> rowCount ();
}
使用错误模式
PDO ::ERRMODE_EXCEPTION
(默认)时,sqlsrv
扩展将无法连接。要连接,您需要将array( PDO ::ATTR_ERRMODE => PDO ::ERRMODE_WARNING)
(或PDO ::ERRMODE_SILENT
)显式传递到构造函数中,或者在使用sqlsrv
时重写getDefaultOptions()
方法。
请参阅文档
请参阅变更日志
查看许可证