Warning: curl_exec() has been disabled for security reasons in /pub/host/sunboyu/sunboyu/www/wp-includes/http.php on line 1022
Mysql数据库抽象层操作类 » Blog Archive 一个程序猿 孙小一,孙小二,PHP,MYSQL,LINUX,APACHE,原创技术,扯淡

Mysql数据库抽象层操作类

作者 : admin 于 2008-09-08 14:26:23 标签: , ,
2008
09-8

DB.InterFace.php

  1. <?php
  2. /*  DB.InterFace.php
  3.  *  DB
  4.  *  @link        http://www.sunboyu.cn
  5.  *  @package     OA
  6.  *  @version     V1.0
  7.  *
  8.  *  2008 08 28  sunboyu@gmail.com
  9.  *  Demo
  10.  */
  11. interface DateBaseConnect
  12. {
  13.     #数据库连接
  14.     public function Connect( $host , $user , $pass , $datebase );
  15. #使用数据库
  16. public function selectDateBase( $datebase );
  17. #执行一个查询
  18. public function Query( $sql );
  19. #取得一行
  20. public function fetchRow( $sql );
  21. #取得所有
  22. public function fetchAll( $sql );
  23. #取得影响行数
  24. public function affectedRow();
  25. #取得结果行数
  26. public function recordCount();
  27. #取得上次插入ID
  28. public function insertID();
  29. #释放资源
  30. public function close();
  31. }
  32. ?>


DB.Mysql.Class.php

  1. <?php
  2. /*  DB.Mysql.Class.php
  3.  *  Mysql
  4.  *  @link        http://www.sunboyu.cn
  5.  *  @package     OA
  6.  *  @version     V1.0
  7.  *
  8.  *  2008 08 28  sunboyu@gmail.com
  9.  *  Demo
  10.  */
  11. require_once(ROOT."/Include/Class/DB.InterFace.php");
  12. class Mysql implements DateBaseConnect
  13. {
  14. #连接标识
  15. public $handle = false;
  16. #结果标识
  17. public $query;
  18. #查询次数
  19. public $exetime;
  20. #数据库连接
  21.     public function Connect( $host , $user , $pass , $datebase )
  22. {
  23. $this->handle = @mysql_connect( $host , $user , $pass , false ) or die("Can't connetc to the DateBse.".mysql_error());
  24. $this->selectDateBase( $datebase );
  25. $this->exetime = 0;
  26. }
  27. #使用数据库
  28. public function selectDateBase( $datebase )
  29. {
  30. @mysql_select_db( $datebase , $this->handle ) or die("Can't select the DateBase".mysql_error());
  31. }
  32. #执行一个查询
  33. public function Query( $sql )
  34. {
  35. $this->query = mysql_query( $sql , $this->handle ) or die("query error".mysql_error());
  36. $this->exetime++;
  37. return true;
  38. }
  39. #取得一行
  40. public function fetchRow( $sql )
  41. {
  42. $this->Query( $sql );
  43. while( $row = mysql_fetch_array( $this->query, MYSQL_ASSOC ) )
  44. {
  45. return $row;
  46.     }
  47. return false;
  48. }
  49. #取得所有
  50. public function fetchAll( $sql )
  51. {
  52. $this->Query( $sql );
  53. $rows = false;
  54. while( $rs = mysql_fetch_array( $this->query, MYSQL_ASSOC ) )
  55. {
  56. $rows[] = $rs;
  57. }
  58. return $rows;
  59. }
  60. #取得影响行数
  61. public function affectedRow()
  62. {
  63. return mysql_affected_rows( $this->query );
  64. }
  65. #取得结果行数
  66. public function recordCount()
  67. {
  68. return mysql_num_rows( $this->query );
  69. }
  70. #取得上次插入ID
  71. public function insertID()
  72. {
  73. return (mysql_insert_id()>0) ? mysql_insert_id() : false;
  74. }
  75. #释放资源
  76. public function close()
  77. {
  78. @mysql_close( $this->handle );
  79. }
  80. #析构函数
  81. /*
  82. function __destruct()
  83. {
  84. $this->close();
  85. }
  86. */
  87. }
  88. ?>

DB.Mysql.Fact.Class.php

  1. <?php
  2. /*  DB.Mysql.Fact.Class.php
  3.  *  Mysql
  4.  *  @link        http://www.sunboyu.cn
  5.  *  @package     OA
  6.  *  @version     V1.0
  7.  *
  8.  *  2008 08 28  sunboyu@gmail.com
  9.  *  Demo
  10.  */
  11. require_once(ROOT."/Include/Class/DB.Mysql.Class.php");
  12. class DB extends Mysql
  13. {
  14.     #插入操作
  15. /*
  16. $array = array(
  17.     'name' = "sunboyu",
  18. 'pass' = "123456"
  19. );
  20. */
  21. function __insert( $array , $table )
  22. {
  23.     $sql = "INSERT INTO {$table} (";
  24.     if(!is_array($array))
  25. {
  26.     die('the array is not a array!');
  27. }
  28. $keys = array_keys( $array );
  29. $sql .= implode( ',' , $keys );
  30. $sql .= ") VALUES (";
  31. foreach( $array as $key=>$value )
  32. {
  33.     if(is_int( $array[$key] ))
  34. {
  35.     $sql .= $array[$key];
  36. }
  37. else if(is_string( $array[$key] ))
  38. {
  39.     $sql .= "'{$array[$key]}'";
  40. }
  41. $sql .= ",";
  42. }
  43. $sql = substr_replace( $sql , "" , -1 );
  44. $sql .= ")";
  45. return $this->Query( $sql );
  46. }
  47. }
  48. ?>

发表评论




XHTML:你可以使用的标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

(若看不到验证码,请重新加载页面。)