010 |
public function __construct() {
|
011 |
$this ->time = $this ->microtime_float();
|
012 |
require_once ( "config.db.php" );
|
013 |
$this ->connect( $db_config [ "hostname" ], $db_config [ "username" ], $db_config [ "password" ], $db_config [ "database" ], $db_config [ "pconnect" ]);
|
014 |
$this ->is_log = $db_config [ "log" ];
|
016 |
$handle = fopen ( $db_config [ "logfilepath" ]. "dblog.txt" , "a+" );
|
017 |
$this ->handle= $handle ;
|
022 |
public function connect( $dbhost , $dbuser , $dbpw , $dbname , $pconnect = 0, $charset = 'utf8' ) {
|
024 |
$this ->link_id = @mysql_connect( $dbhost , $dbuser , $dbpw , true);
|
026 |
$this ->halt( "数据库连接失败" );
|
029 |
$this ->link_id = @mysql_pconnect( $dbhost , $dbuser , $dbpw );
|
031 |
$this ->halt( "数据库持久连接失败" );
|
034 |
if (!@mysql_select_db( $dbname , $this ->link_id)) {
|
035 |
$this ->halt( '数据库选择失败' );
|
037 |
@mysql_query( "set names " . $charset );
|
041 |
public function query( $sql ) {
|
042 |
$this ->write_log( "查询 " . $sql );
|
043 |
$query = mysql_query( $sql , $this ->link_id);
|
044 |
if (! $query ) $this ->halt( 'Query Error: ' . $sql );
|
049 |
public function get_one( $sql , $result_type = MYSQL_ASSOC) {
|
050 |
$query = $this ->query( $sql );
|
051 |
$rt =& mysql_fetch_array( $query , $result_type );
|
052 |
$this ->write_log( "获取一条记录 " . $sql );
|
057 |
public function get_all( $sql , $result_type = MYSQL_ASSOC) {
|
058 |
$query = $this ->query( $sql );
|
061 |
while ( $row =& mysql_fetch_array( $query , $result_type )) {
|
065 |
$this ->write_log( "获取全部记录 " . $sql );
|
070 |
public function insert( $table , $dataArray ) {
|
073 |
if ( ! is_array ( $dataArray ) || count ( $dataArray )<=0) {
|
074 |
$this ->halt( '没有要插入的数据' );
|
077 |
while (list( $key , $val )=each( $dataArray )) {
|
081 |
$field = substr ( $field ,0,-1);
|
082 |
$value = substr ( $value ,0,-1);
|
083 |
$sql = "insert into $table($field) values($value)" ;
|
084 |
$this ->write_log( "插入 " . $sql );
|
085 |
if (! $this ->query( $sql )) return false;
|
090 |
public function update( $table , $dataArray , $condition = "" ) {
|
091 |
if ( ! is_array ( $dataArray ) || count ( $dataArray )<=0) {
|
092 |
$this ->halt( '没有要更新的数据' );
|
096 |
while ( list( $key , $val ) = each( $dataArray ))
|
097 |
$value .= "$key = '$val'," ;
|
098 |
$value .= substr ( $value ,0,-1);
|
099 |
$sql = "update $table set $value where 1=1 and $condition" ;
|
100 |
$this ->write_log( "更新 " . $sql );
|
101 |
if (! $this ->query( $sql )) return false;
|
106 |
public function delete ( $table , $condition = "" ) {
|
107 |
if ( empty ( $condition ) ) {
|
108 |
$this ->halt( '没有设置删除的条件' );
|
111 |
$sql = "delete from $table where 1=1 and $condition" ;
|
112 |
$this ->write_log( "删除 " . $sql );
|
113 |
if (! $this ->query( $sql )) return false;
|
118 |
public function fetch_array( $query , $result_type = MYSQL_ASSOC){
|
119 |
$this ->write_log( "返回结果集" );
|
120 |
return mysql_fetch_array( $query , $result_type );
|
124 |
public function num_rows( $results ) {
|
125 |
if (! is_bool ( $results )) {
|
126 |
$num = mysql_num_rows( $results );
|
127 |
$this ->write_log( "获取的记录条数为" . $num );
|
135 |
public function free_result() {
|
136 |
$void = func_get_args();
|
137 |
foreach ( $void as $query ) {
|
138 |
if ( is_resource ( $query ) && get_resource_type( $query ) === 'mysql result' ) {
|
139 |
return mysql_free_result( $query );
|
142 |
$this ->write_log( "释放结果集" );
|
146 |
public function insert_id() {
|
147 |
$id = mysql_insert_id( $this ->link_id);
|
148 |
$this ->write_log( "最后插入的id为" . $id );
|
153 |
protected function close() {
|
154 |
$this ->write_log( "已关闭数据库连接" );
|
155 |
return @mysql_close( $this ->link_id);
|
159 |
private function halt( $msg = '' ) {
|
160 |
$msg .= "\r\n" .mysql_error();
|
161 |
$this ->write_log( $msg );
|
166 |
public function __destruct() {
|
167 |
$this ->free_result();
|
168 |
$use_time = ( $this -> microtime_float())-( $this ->time);
|
169 |
$this ->write_log( "完成整个查询任务,所用时间为" . $use_time );
|
171 |
fclose( $this ->handle);
|
176 |
public function write_log( $msg = '' ){
|
178 |
$text = date ( "Y-m-d H:i:s" ). " " . $msg . "\r\n" ;
|
179 |
fwrite( $this ->handle, $text );
|
184 |
public function microtime_float() {
|
185 |
list( $usec , $sec ) = explode ( " " , microtime());
|
186 |
return ((float) $usec + (float) $sec );
|
[代码] config.db.php
02 |
$db_config [ "hostname" ] = "localhost" ;
|
03 |
$db_config [ "username" ] = "root" ;
|
04 |
$db_config [ "password" ] = "123" ;
|
05 |
$db_config [ "database" ] = "test" ;
|
06 |
$db_config [ "charset" ] = "utf8" ;
|
07 |
$db_config [ "pconnect" ] = 1; |
-
librespeed/speedtest: Self-hosted Speedtest for HTML5 and more. Easy setup, exam
阅读:1206|2022-08-30
-
女人怀孕后,为了有一个健康聪明的宝宝,经历各种体检、筛查。其实这些体检和筛查中的
阅读:926|2022-11-06
-
duzyn/material-axure-library: An Axure widget library based on Google Material D
阅读:588|2022-08-15
-
OriImage=imread(amp;#39;D:\图片\Pinned\2_110624211810_1.jpgamp;#39;); %读入图
阅读:592|2022-07-18
-
jie123108/lua-resty-s3: a aws s3 client for ngx_lua
阅读:863|2022-08-16
-
web-ridge/react-native-paper-dates: Smooth and fast cross platform Material Desi
阅读:276|2022-08-17
-
yrccondor/mdx: MDx - Material Design WordPress Theme
阅读:359|2022-08-17
-
工程造价 培养目标: 培养学生会看图、懂算工程量、精套价、善沟通及协调,适应第一线
阅读:657|2022-07-30
-
离中国最远的国家是阿根廷。从太平洋直线计算,即往东线走,北京到阿根廷的布宜诺斯艾
阅读:623|2022-11-06
-
inertiajs/inertia-laravel: The Laravel adapter for Inertia.js.
阅读:686|2022-08-12
|
请发表评论