<?php
/**
* @title: Ekcms mysql类库
* @version: 1.0
* @author: perry <perry@1kyou.com>
* @published: 2013-10-2
*/ //数据库连接类
class mysql
{
var $ConnStr; function __construct(){
$this->connect();
$this->selectdb(); } //连接数据库服务器
function connect(){ $this->ConnStr=(DB_PCONNECT)?@mysql_pconnect(DB_HOST.":".DB_PORT,DB_USER,DB_PWS):@mysql_connect(DB_HOST.":".DB_PORT,DB_USER,DB_PWS); if(!$this->ConnStr){
echo '数据库连接错误'.mysql_error();
}
} //连接数据库
function selectdb(){
if(!@mysql_select_db(DB_NAME,$this->ConnStr)){
echo '数据库'.DB_NAME.'不存在';
}
@mysql_unbuffered_query("set names ".DB_CHARSET);
} //mysql执行语句
function query($sql){
$sql = str_replace("#@__", PREX, $sql);
if(!$res=@mysql_query($sql,$this->ConnStr)){
echo '操作数据库失败'.mysql_error()."<br>sql:{$sql}";
}
return $res;
} //sql报错信息
function get_error(){
$err=mysql_error($this->ConnStr);
return $err;
} //取出数字作为数组索引的数据集合
function fetch_array($sql){
$result=$this->query($sql);
$array = array();
while($rows=@mysql_fetch_array($result)){
$array[]=$rows;
}
mysql_free_result($result);
return $array;
} function fetch_rows($sql){
$query = $this->query($sql);
return @mysql_fetch_array($query);
} //读取单条数据
function get_one($sql){
$result=$this->query($sql);
return @mysql_fetch_assoc($result);
} //取出字段作为数组索引的数据集合
function fetch_asc($sql){
$result=$this->query($sql);
$arr=array();
while($rows=@mysql_fetch_assoc($result)){
$arr[]=$rows;
}
mysql_free_result($result);
return $arr;
} //最后一次插入的id
function insert_id(){
return mysql_insert_id($this->ConnStr);
} //返回数据数目
function num_rows($sql){
$result=$this->query($sql);
$num=@mysql_num_rows($result);
mysql_free_result($result);
return $num;
} //取得结果集中字段的数目
function num_fields($query){
return mysql_num_fields($query);
} //返回字段名数组
function fetch_field($sql){
$result=$this->query($sql);
$num=$this->num_fields($result);
for($i=0;$i<$num;$i++){
$arr[]=mysql_field_name($result,$i);
}
return $arr;
} //获得版本信息
function server_info(){
return mysql_get_server_info($this->ConnStr);
} //查询数据条数
function db_num($table,$where){
$num=$this->num_rows("select * from ".PREX.$table." where $where");
return $num;
} //添加信息进数据表
function db_insert($table, $field) {
$SQL = "INSERT INTO `".PREX.$table."` SET $field";
$this->query($SQL);
return $this->insert_id();
} //更新数据表信息
function db_update($table,$field,$where){
$SQL = "UPDATE `".PREX.$table."` SET $field WHERE $where";
return $this->query($SQL);
} //删除数据信息
function db_delete($table, $where) {
$SQL = "DELETE FROM `".PREX.$table."` WHERE $where";
return $this->query($SQL);
}
//执行事务函数
function query_work($arr){
$this->query("SET AUTOCOMMIT=0");
$this->query("BEGIN");
if (is_array($arr)){
foreach ($arr as $k=>$v){
if(!$this->query($v)){
$this->query("ROOLBACK");
}
}
return $this->query("COMMIT");//执行事务
}
return '';
}
//手动关闭数据库
function close(){
@mysql_close($this->ConnStr);
}
/*
*
*数据库操作
*创建、修改、删除数据表
*添加、修改、删除字段
*
*/ //创建数据表
function create_table($table,$field){
$sql="create table ".PREX.$table." (".$field.") ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci";
return $this->query($sql);
} //添加字段
function add_field($table,$field){
$sql="alter table ".PREX.$table." add ".$field." CHARACTER SET utf8 COLLATE utf8_general_ci NULL";
return $this->query($sql);
} //修改字段
function edit_field($table,$field){
$sql="alter table ".PREX.$table." CHANGE ".$field." CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL";
return $this->query($sql);
} //删除字段
function del_field($table,$field){
$sql="alter table ".PREX.$table." drop column ".$field;
return $this->query($sql);
} //显示数据表
function show_table(){
$rel=$this->fetch_array('show tables');
foreach($rel as $key=>$value){
$arr[]=$value[0];
}
return $arr;
} //修改数据表名,$table-改前表名 $table_now-改后表名
function rename_table($table,$table_now){
$sql="RENAME TABLE ".$table." TO ".$table_now;
return $this->query($sql);
} //删除数据表
function del_table($table){
$sql="drop table ".PREX.$table;
return $this->query($sql);
}
//析构函数关闭数据库
function __destruct(){
@mysql_close($this->ConnStr);
}
}
?>

PHP+mysql常用类库的更多相关文章

  1. Mysql 常用 SQL 语句集锦

    Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...

  2. MySQL常用命令和常见问题

    MySQL常用命令和常见问题 --创建数据库并设置字符集 create database wip default character set utf8 collate utf8_general_ci; ...

  3. Google的Java常用类库 Guava资料

    java的人应该都知道Apache commons的java常用类库吧,这个Guava和commons一样,封装出一套比jdk本身提供的常用类库强大.既然有了这个这么强大的类库,我们就没必要重复造轮子 ...

  4. mysql常用基本操作

    mysql常用操作 查看都有哪些库 show databases; 查看某个库的表 use 库名; show tables; 查看表的字段 desc 表名; 当前是哪个用户 select user() ...

  5. MySQL 常用的sql语句小结(待续)

    mysql 常用的sql语句 1.查看数据库各个表中的记录数 USE information_schema; SELECT table_name,table_rows FROM tables WHER ...

  6. mysql常用操作语句

    mysql常用操作语句 1.mysql -u root -p   2.mysql -h localhost -u root -p database_name 2.列出数据库: 1.show datab ...

  7. Mysql 常用 SQL 语句集锦 转载(https://gold.xitu.io/post/584e7b298d6d81005456eb53)

    Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...

  8. MySQL数据库3 - MySQL常用数据类型

    一. MySql常用数据类型 数据类型:整数(tinyint smailint int bigint) 定点数 decimal(p,s) ------ 小数点位置固定的       ---> 数 ...

  9. MYSQL常用内置函数详解说明

    函数中可以将字段名当作变量来用,变量的值就是该列对应的所有值:在整理98在线字典数据时(http://zidian.98zw.com/),有这要一个需求,想从多音字duoyinzi字段值提取第一个拼音 ...

随机推荐

  1. mysql优化技巧《转》

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  2. [转载] Google大数据引擎Dremel剖析(1)

    原文: https://mp.weixin.qq.com/s?__biz=MjM5NzAyNTE0Ng==&mid=207895956&idx=1&sn=58e8af26fd3 ...

  3. Java数组实现五子棋功能

    package ch4; import java.io.*; /** * Created by Jiqing on 2016/11/9. */ public class Gobang { // 定义棋 ...

  4. maven相关资料

    http://www.yiibai.com/maven/ Maven教程 https://www.zhihu.com/question/20104270 http://huangnx.com/tags ...

  5. smarty 快速上手

    smarty半小时快速上手入门教程 投稿:shichen2014 字体:[增加 减小] 类型:转载 时间:2014-10-27我要评论 这篇文章主要介绍了smarty半小时快速上手入门教程,以实例的形 ...

  6. PC-1500收集整理记

    目录 第1章计算器    1 1.1 存储卡    2 1.2 取出"牛皮糖"    4 1.3 打磨键盘按钮    6 1.4 通电    7 第2章底座    10 2.1 去 ...

  7. 在IIS7.5打开网页的时候,提示: HTTP 错误 500.0 - Internal Server Error 调用 LoadLibraryEx 失败,在 ISAPI 筛选器 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\\aspnet_filter.dll" 上。解决方法

  8. centos7命令

    查看ip ip addr ip link 添加服务 systemctl enable nginx 添加防火墙端口 firewall-cmd --zone=public --add-port=80/tc ...

  9. 理论与实践中的 C# 内存模型,第 2 部分

    转载自:https://msdn.microsoft.com/zh-cn/magazine/jj883956.aspx 这是介绍 C# 内存模型的系列文章的第二篇(共两篇). 正如在 MSDN 杂志十 ...

  10. CentOS中输入yum报错:sudo: unable to execute /bin/yum: No such file or directory

    今天尝试更新了下虚拟机CentOS中的python版本后. 运行“yum”命令,就报错:“sudo: unable to execute /bin/yum: No such file or direc ...