mysqli 封装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | <?php //header header( 'content-type:text/html;charset=UTF-8' ); class DB { //定义属性 private $host; //主机名 private $port; //端口号 private $name; //用户名 private $pass; //密码 private $dbname; //数据库名 private $charset; //设置字符集 private $link; //连接数据库 private static $instance; //初始化 构造函数 private function __construct($arr = array()){ $ this ->host = isset($arr[ 'host' ]) ? $arr[ 'host' ] : 'localhost' ; $ this ->port = isset($arr[ 'port' ]) ? $arr[ 'port' ] : '3306' ; $ this ->name = isset($arr[ 'name' ]) ? $arr[ 'name' ] : 'root' ; $ this ->pass = isset($arr[ 'pass' ]) ? $arr[ 'pass' ] : 'root' ; $ this ->dbname = isset($arr[ 'dbname' ]) ? $arr[ 'dbname' ] : 'yii2_project' ; $ this ->charset = isset($arr[ 'charset' ]) ? $arr[ 'charset' ] : 'utf8' ; //连接数据库 $ this ->db_connect(); //选择数据库 $ this ->db_usedb(); //设置字符集 $ this ->db_charset(); } //连接数据库 private function db_connect(){ //主机名:端口号 用户名 密码 $ this ->link = mysqli_connect($ this ->host. ':' .$ this ->port, $ this ->name, $ this ->pass); //连接失败 if (!$ this ->link){ echo '数据库连接失败<br>' ; echo '错误编码是:' .mysqli_errno($ this ->link). '<br>' ; echo '错误信息是:' .mysqli_error($ this ->link). '<br>' ; exit; } } //选择数据库 private function db_usedb(){ mysqli_query($ this ->link, "use {$this->dbname}" ); } //设置字符集 private function db_charset(){ mysqli_query($ this ->link, "set names {$this->charset}" ); } //私有化克隆函数,防止外界克隆对象 private function __clone() { } //单例访问统一入口 public static function getInstance($arr) { if (!(self::$instance instanceof self)) { self::$instance = new self($arr); } return self::$instance; } /** * 执行语句 * @param $sql * @return source */ private function query($sql){ $res = mysqli_query($ this ->link, $sql); if (!$res){ echo 'sql语句有错误<br>' ; echo '错误编码是:' .mysqli_errno($ this ->link). '<br>' ; echo '错误信息是:' .mysqli_error($ this ->link). '<br>' ; exit; } return $res; //成功返回数据 } /** * 获取刚插入数据的id * @return int */ public function getInsertId(){ return mysqli_insert_id($ this ->link); } /** * 查询某个字段, 例如 select count(*), select username * @param $sql * @return string or int */ public function getOne($sql){ $query = $ this ->query($sql); return mysqli_free_result($query); } /** * 获取一行记录 * @param $sql * @return array 一维 */ public function getRow($sql, $type= 'assoc' ){ $query = $ this ->query($sql); if (!in_array($type, array( "assoc" , "array" , "row" ))){ die( "mysql_query error" ); } $functionname = "mysqli_fetch_" .$type; return $functionname($query); } /** * 前置条件:通过资源获取一条记录 * @param $query source * @return array 一维 */ public function getRowFromSource($query, $type= "assoc" ){ if (!in_array($type, array( "assoc" , "array" , "row" ))){ die( "mysql_query error" ); } $functionname = "mysqli_fetch_" .$type; return $functionname($query); } /** * 获取所有记录 * @param $sql * @return array 二维 */ public function getAll($sql){ $query = $ this ->query($sql); $list = array(); while ($row = $ this ->getRowFromSource($query)) { $list[] = $row; } return $list; } /* * 新增数据方法 * @param1 $table, $data 表名 数据 * @return 上一次增加操做产生ID值 */ public function insert($table, $data){ //遍历数组,得到每一个字段和字段的值 $kstr = '' ; $vstr = '' ; foreach ($data as $key => $val) { //$key是字段名, $val是对应的值 $kstr .= $key. "," ; $vstr .= "'$val'," ; } $kstr = rtrim($kstr, ',' ); $vstr = rtrim($vstr, ',' ); //添加的sql语句 $sql = "insert into $table ($kstr) values ($vstr)" ; //执行 $ this ->query($sql); //返回上一次增加操做产生ID值 return $ this ->getInsertId(); } /* * 删除一条数据方法 * @param1 $table, $where=array('id'=>'1') 表名 条件 * @return 受影响的行数 */ public function deleteOne($table, $ where ){ if (is_array($ where )){ foreach ($ where as $key => $val) { $condition = $key. '=' .$val; } } else { $condition = $ where ; } $sql = "delete from $table where $condition" ; $ this ->query($sql); //返回受影响的行数 return mysqli_affected_rows($ this ->link); } /* * 删除多条数据方法 * @param1 $table, $where 表名 条件 * @return 受影响的行数 */ public function deleteAll($table, $ where ){ if (is_array($ where )){ foreach ($ where as $key => $val) { if (is_array($val)){ $condition = $key. ' in (' .implode( ',' , $val) . ')' ; } else { $condition = $key. '=' .$val; } } } else { $condition = $ where ; } $sql = "delete from $table where $condition" ; $ this ->query($sql); //返回受影响的行数 return mysqli_affected_rows($ this ->link); } /* * 修改数据方法 * @param1 $table,$data,$where 表名 数据 条件 * @return 受影响的行数 */ public function update($table,$data,$ where ){ //遍历数组,得到每一个字段和字段的值 $str= '' ; if (is_array($data)){ foreach ($data as $key=>$v){ $str.= "$key='$v'," ; } $str=rtrim($str, ',' ); //修改的SQL语句 $sql= "update $table set $str where $where" ; } else { //修改的SQL语句 $sql= "update $table set $data where $where" ; } $ this ->query($sql); //返回受影响的行数 return mysqli_affected_rows($ this ->link); } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步