封装代码
header ( "Content-type: text/html; charset: utf-8" ) ;
class DBManager {
public $mysqli= null ;
public $result= null ;
public $dsn = null ;
public $pdo = null ;
function __construct ( ) {
$dbms = 'mysql' ;
$user = 'root' ;
$pass = 'root' ;
$host = 'localhost' ;
$port = '3306' ;
$dbname = 'one' ;
$charset = 'utf8' ;
$this - > dsn = "$dbms:host=$host;port=$port;dbname=$dbname;charset=$charset" ;
try {
return $this - > pdo = new PDO ( $this - > dsn, $user, $pass) ;
$this - > dsn = null ;
} catch ( PDOException $e) {
die ( "error!: " . $e- > getMessage ( ) . "<br/>" ) ;
}
}
function getResult ( $query) {
return $this - > result= $this - > pdo- > query ( $query) ;
}
function insert ( $query) {
return $this - > pdo- > exec ( $query) ;
}
function delete ( $query) {
return $this - > pdo- > exec ( $query) ;
}
function update ( $query) {
return $this - > pdo- > exec ( $query) ;
}
function autocommitS ( ) {
$this - > pdo- > beginTransaction ( ) ;
}
function commitS ( ) {
$this - > pdo- > commit ( ) ;
}
function rollbackS ( ) {
$this - > pdo- > rollback ( ) ;
}
function error_unlock ( ) {
$this - > pdo- > setAttribute ( PDO : : ATTR_CASE , PDO : : CASE_NATURAL ) ;
$this - > pdo- > setAttribute ( PDO : : ATTR_ERRMODE , PDO : : ERRMODE_EXCEPTION ) ;
}
function lastInsertIdlast ( $e) {
echo $e- > getMessage ( ) . "<br/>" ;
echo $e- > getCode ( ) . "<br/>" ;
echo $e- > getFile ( ) . "<br/>" ;
echo $e- > getLine ( ) . "<br/>" ;
die ( "error" ) ;
}
function dbclose ( ) {
$pdo = null ;
}
}