PDO
v2.2.1
Hanya perpustakaan database PDO lainnya
Gunakan Komposer
$ composer require faapz/ PDO
Contohnya memilih, menyisipkan, memperbarui dan menghapus data dari atau ke dalam tabel 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 ();
}
Ekstensi
sqlsrv
akan gagal terhubung saat menggunakan mode kesalahanPDO ::ERRMODE_EXCEPTION
(default). Untuk terhubung, Anda perlu meneruskanarray( PDO ::ATTR_ERRMODE => PDO ::ERRMODE_WARNING)
secara eksplisit (atauPDO ::ERRMODE_SILENT
) ke konstruktor, atau mengganti metodegetDefaultOptions()
saat menggunakansqlsrv
.
Lihat DOKUMENTASI
Lihat GANTI LOG
Lihat LISENSI