• mysqli的简单工具包


    mysqli的简单工具包

      1 <?php
      2 /**
      3  * 连接
      4  * @param string $host
      5  * @param string $user
      6  * @param string $password
      7  * @param string $charset
      8  * @param string $database
      9  * @return object 连接标识符
     10  */
     11 function connect1($host,$user,$password,$charset,$database) {
     12     $link = mysqli_connect ( $host, $user, $password ) or die ( '数据库连接失败<br/>ERROR ' . mysqli_connect_errno () . ':' . mysqli_connect_error () );
     13     mysqli_set_charset ( $link, $charset );
     14     mysqli_select_db ( $link, $database ) or die ( '指定数据库打开失败<br/>ERROR ' . mysqli_errno ( $link ) . ':' . mysqli_error ( $link ) );
     15     return $link;
     16 }
     17 /**
     18  * 连接 需要传递数组
     19  * @param array $config
     20  * @return object
     21  */
     22 function connect2($config) {
     23     $link = mysqli_connect ( $config ['host'], $config ['user'], $config ['password'] ) or die ( '数据库连接失败<br/>ERROR ' . mysqli_connect_errno () . ':' . mysqli_connect_error () );
     24     mysqli_set_charset ( $link, $config ['charset'] );
     25     mysqli_select_db ( $link, $config ['dbName'] ) or die ( '指定数据库打开失败<br/>ERROR ' . mysqli_errno ( $link ) . ':' . mysqli_error ( $link ) );
     26     return $link;
     27 }
     28 /**
     29  * 用常量的形式建立连接
     30  * @return unknown
     31  */
     32 function connect3(){
     33     $link = mysqli_connect ( DB_HOST, DB_USER, DB_PWD ) or die ( '数据库连接失败<br/>ERROR ' . mysqli_connect_errno () . ':' . mysqli_connect_error () );
     34     mysqli_set_charset ( $link, DB_CHARSET );
     35     mysqli_select_db ( $link, DB_DBNAME ) or die ( '指定数据库打开失败<br/>ERROR ' . mysqli_errno ( $link ) . ':' . mysqli_error ( $link ) );
     36     return $link;
     37 }
     38 
     39 /*
     40  array(
     41  'username'=>'king',
     42  'password'=>'king',
     43  'age'=>'12',
     44  'regTime'=>'123123123'
     45  );
     46  INSERT user(username,password,age,regTime) VALUES('king','king','12','123123123');
     47  */
     48 /**
     49  * 插入操作
     50  * @param object $link
     51  * @param array $data
     52  * @param string $table
     53  * @return boolean
     54  */
     55 function insert($link,$data,$table){
     56     $keys = join ( ',', array_keys ( $data ) );
     57     $vals = "'" . join ( "','", array_values ( $data ) ) . "'";
     58     $query = "INSERT {$table}({$keys}) VALUES({$vals})";
     59     $res = mysqli_query ( $link, $query );
     60     if ($res) {
     61         return mysqli_insert_id ( $link );
     62     } else {
     63         return false;
     64     }
     65 }
     66 
     67 /*
     68  array(
     69          'username'=>'king123',
     70          'password'=>'king123',
     71          'age'=>'32',
     72          'regTime'=>'123123123'
     73  );
     74  UPDATE user SET username='king123',password='king123',age='32',regTime='123123123' WHERE id=1
     75 */
     76 /**
     77  * 更新操作
     78  * @param object $link
     79  * @param array $data
     80  * @param string $table
     81  * @param string $where
     82  * @return boolean
     83  */
     84 function update($link, $data, $table, $where = null) {
     85     foreach ( $data as $key => $val ) {
     86         $set .= "{$key}='{$val}',";
     87     }
     88     $set = trim ( $set, ',' );
     89     $where = $where == null ? '' : ' WHERE ' . $where;
     90     $query = "UPDATE {$table} SET {$set} {$where}";
     91     $res = mysqli_query ( $link, $query );
     92     if ($res) {
     93         return mysqli_affected_rows ( $link );
     94     } else {
     95         return false;
     96     }
     97 }
     98 
     99 //DELETE FROM user WHERE id=
    100 /**
    101  * 删除操作
    102  * @param object $link
    103  * @param string $table
    104  * @param string $where
    105  * @return boolean
    106  */
    107 function delete($link, $table, $where = null) {
    108     $where = $where ? ' WHERE ' . $where : '';
    109     $query = "DELETE FROM {$table} {$where}";
    110     $res = mysqli_query ( $link, $query );
    111     if ($res) {
    112         return mysqli_affected_rows ( $link );
    113     } else {
    114         return false;
    115     }
    116 }
    117 
    118 /**
    119  * 查询指定记录
    120  * @param object $link
    121  * @param string $query
    122  * @param string $result_type
    123  * @return array|boolean
    124  */
    125 function fetchOne($link, $query, $result_type = MYSQLI_ASSOC) {
    126     $result = mysqli_query ( $link, $query );
    127     if ($result && mysqli_num_rows ( $result ) > 0) {
    128         $row = mysqli_fetch_array ( $result, $result_type );
    129         return $row;
    130     } else {
    131         return false;
    132     }
    133 }
    134 
    135 /**
    136  * 查询所有记录
    137  * @param object $link
    138  * @param string $query
    139  * @param string $result_type
    140  * @return array|boolean
    141  */
    142 function fetchAll($link, $query, $result_type = MYSQLI_ASSOC) {
    143     $result = mysqli_query ( $link, $query );
    144     if ($result && mysqli_num_rows ( $result ) > 0) {
    145         while ( $row = mysqli_fetch_array ( $result, $result_type ) ) {
    146             $rows [] = $row;
    147         }
    148         return $rows;
    149     } else {
    150         return false;
    151     }
    152 }
    153 
    154 /**
    155  * 得到表中的记录数
    156  * @param object $link
    157  * @param string $table
    158  * @return number|boolean
    159  */
    160 function getTotalRows($link, $table) {
    161     $query = "SELECT COUNT(*) AS totalRows FROM {$table}";
    162     $result = mysqli_query ( $link, $query );
    163     if ($result && mysqli_num_rows ( $result ) == 1) {
    164         $row = mysqli_fetch_assoc ( $result );
    165         return $row ['totalRows'];
    166     } else {
    167         return false;
    168     }
    169 }
    170 
    171 /**
    172  * 得到结果集的记录条数
    173  * @param object $link
    174  * @param string $query
    175  * @return boolean
    176  */
    177 function getResultRows($link, $query) {
    178     $result = mysqli_query ( $link, $query );
    179     if ($result) {
    180         return mysqli_num_rows ( $result );
    181     } else {
    182         return false;
    183     }
    184 }
    185 
    186 
    187 
    188 /**
    189  * @param object $link
    190  */
    191 function getServerInfo($link) {
    192     return mysqli_get_server_info ( $link );
    193 }
    194 /**
    195  * @param object $link
    196  */
    197 function getClientInfo($link) {
    198     return mysqli_get_client_info ( $link );
    199 }
    200 
    201 /**
    202  * @param object $link
    203  */
    204 function getHostInfo($link){
    205     return mysqli_get_host_info($link);
    206 }
    207 
    208 /**
    209  * @param object $link
    210  */
    211 function getProtoInfo($link) {
    212     return mysqli_get_proto_info ( $link );
    213 }
     1 <?php
     2 $config = array(
     3     'host'=>'localhost',
     4     'user'=>'root',
     5     'password'=>'root',
     6     'charset'=>'utf8',
     7     'dbName'=>'51zxw'
     8 );
     9 
    10 
    11 define('DB_HOST','localhost');
    12 define('DB_USER','root');
    13 define('DB_PWD','root');
    14 define('DB_CHARSET','utf8');
    15 define('DB_DBNAME','51zxw');
  • 相关阅读:
    声明以及字符表述类--字母大小写的敏感性
    条款52:写了placement new 也要写placement delete(write placement delete if you write placement new)
    verdi知识点
    关于$test$plusargs和$value$plusargs的小结
    条款40:明智而审慎地使用多重继承(use multiple inheritance judiciously)
    条款39:明智而审慎地使用private继承(use private inheritance judiciously)
    条款38:通过复合塑模has-a或“根据某物实现出”
    条款37:绝不重新定义继承而来的缺省参数值(Never redefine a function's inherited default parameter value)
    自学nodejs系列
    五个典型的JavaScript面试题
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/8548942.html
Copyright © 2020-2023  润新知