books
(books)
高级会员

老会员
UID 23239
精华
0
积分 546
帖子 562
金钱 546 喜悦币
威望 0
人脉 0
阅读权限 50
注册 2003-9-16
状态 离线
|
发一个MYSQL::PDO类,其实和函数差不多,全是静态方法
<?php
class Mysql {
// introspect public static $classname = __CLASS__;
// members public static $debug = 1;
// methods public static function connect(&$conn, &$error) { try { $conn = new PDO('mysql:host=localhost;dbname=HP', 'root', 'books', array(PDO::ATTR_PERSISTENT => true)); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // $conn->exec('SET NAMES UTF8'); // $conn->beginTransaction(); return true; } catch (PDOException $e) { if (self::$debug == 1) { $error['msg'] = $e->getMessage(); } else { $error['code'] = 'error_mysql:connect'; $error['msg'] = '数据库连接错误。'; } $conn = null; return false; } }
// simple select public static function query(&$conn, &$stmt, &$error, &$bind, &$res, &$sql, $mode = '', $prepare = true) { try { if ($prepare) { $stmt = $conn->prepare($sql); } if ($bind['mode'] == 'yes') { foreach ($bind['vari'] as $key => &$val) { $stmt->bindValue($key, $val); } } $stmt->execute(); $res = $stmt->fetchAll(PDO::FETCH_ASSOC); if (!$res) { $error['code'] = 'nodata'; $error['msg'] = '查无数据'; $error['cnt'] = 0; } return true; } catch (PDOException $e) { if (self::$debug == 1) { $error['msg'] = $e->getMessage(); $error['sqltext'] = $sql; } else { $error['code'] = 'error_mysql:query'; $error['msg'] = '数据库查询错误。'; } // $conn->rollBack(); $stmt = null; $conn = null; return false; } }
// simple update insert delete public static function update(&$conn, &$stmt, &$error, &$bind, &$sql, $mode = '', $prepare = true) { try { // if (!$conn) { // self::connect($conn, $error); // } if ($prepare) { $stmt = $conn->prepare($sql); } if ($bind['mode'] == 'yes') { foreach ($bind['vari'] as $key => &$val) { $stmt->bindValue($key, $val); } } $stmt->execute(); $error['cnt'] = $stmt->rowCount(); if ($error['cnt'] === 0) { $error['code'] = 'nodata'; $error['msg'] = '没有更新'; } return true; } catch (PDOException $e) { if ($error['catch'] == 1) { $error['code'] = ''; $error['msg'] = $e->getMessage(); } elseif (self::$debug == 1) { $error['msg'] = $e->getMessage(); $error['sqltext'] = $sql; } else { $error['code'] = 'error_mysql:query'; $error['msg'] = '数据库更新错误。'; } // $conn->rollBack(); $stmt = null; $conn = null; return false; } }
// simple call public static function call(&$conn, &$stmt, &$error, &$bind, &$res, &$sql, $mode = '', $prepare = true) { try { if ($prepare) { $stmt = $conn->prepare($sql); } if ($bind['mode'] == 'yes') { if (is_array($bind['vari']) and count($bind['vari']) > 0) { foreach ($bind['vari'] as $key => &$val) { $stmt->bindValue($key, $val); } } } $stmt->execute(); if ($res === '1') { $res = array(); $res = $stmt->fetchAll(PDO::FETCH_ASSOC); if (!$res) { $error['code'] = 'nodata'; $error['msg'] = '查无数据'; $error['cnt'] = 0; } } elseif ($res === 'n') { $res = array(); $i = 0; $j = 0; do { if (!$res[$i] = $stmt->fetchAll(PDO::FETCH_ASSOC)) { $res[$i]['cnt'] = 0; $j++; } $i++; } while ($stmt->nextRowset()); if ($i == $j) { $error['code'] = 'nodata'; $error['msg'] = '查无数据'; $error['cnt'] = 0; } } return true; } catch (PDOException $e) { if (self::$debug == 1) { $error['msg'] = $e->getMessage(); $error['sqltext'] = $sql; } else { $error['code'] = 'error_mysql:query'; $error['msg'] = '数据库查询错误。'; } // $conn->rollBack(); $stmt = null; $conn = null; return false; } }
public static function commit(&$conn, &$stmt) { try { if ($conn) $conn->commit(); return true; } catch (PDOException $e) { if (self::$debug == 1) { $error['code'] = 'error_fatal:commit'; $error['msg'] = $e->getMessage(); } else { $error['code'] = 'error_fatal:commit'; $error['msg'] = '数据库提交异常错误。'; } $stmt = null; $conn = null; return false; } }
public static function rollback(&$conn, &$stmt) { try { if ($conn) $conn->rollBack(); return true; } catch (PDOException $e) { if (self::$debug == 1) { $error['code'] = 'error_fatal:commit'; $error['msg'] = $e->getMessage(); } else { $error['code'] = 'error_fatal:rollback'; $error['msg'] = '数据库回滚异常错误。'; } $stmt = null; $conn = null; return false; } }
public static function close(&$conn, &$stmt, $term = false) { try { // if ($term) { // if ($conn) self::commit($conn); // } // else { // if ($conn) self::rollback($conn); // } $stmt = null; // $conn = null; } catch (PDOException $e) { if (self::$debug == 1) { $error['code'] = 'error_fatal:close_commit'; $error['msg'] = $e->getMessage(); } else { $error['code'] = 'error_fatal:close_rollback'; $error['msg'] = '数据库关闭异常错误。'; } $stmt = null; $conn = null; return false; } }
} ?> PS1:如果您使用INNO引擎,需要事务处理的话,把那些注释搞掉就可以了!
PS2:支持存储过程返回多条结果集,暂不支持存储过程OUT,INOUT参数!
[ 本帖最后由 books 于 2007-5-30 04:13 PM 编辑 ]
|
|