比来在新浪的云平台(SAE,http://sae.sina.com.cn)中做应用,底本应用SAE供给的mysql操纵类SaeMysql(http://apidoc.sinaapp.com/sae/SaeMysql.html),然则有些不便利:
1、SaeMysql没有供给完全的增删查改办法,插入数据、删除数据、更新数据都只能本身写完全的sql然后在应用runSql 办法履行;
2、要先初始化¥mysql = new SaeMysql();,今后要在其它函数内进行mysql操纵的话,都不克不及忘了把¥mysql 列入全局变量;
3、我不赞成在具体的营业代码中直接应用SAE供给的类,今后如果把应用迁徙出SAE,会斗劲麻烦。
所以我本身把SaeMysql从头封装了一下,本身写了个mysql操纵类。
代码如下:
//by kuiGG www.kuigg.com
class kuigg_Db {
function tbname(¥tb) {
return “kuigg_{¥tb}”;
}
function getdata (¥arr , ¥separator = ‘,’) {
¥str = ¥s = ”;
foreach (¥arr as ¥k => ¥v) {
¥str .= ¥s.”`{¥k}`=’{¥v}’”;
¥s = ¥separator;
}
return ¥str;
}
function count (¥tb , ¥fields = ‘*’ , ¥terms = ”){
¥o = & self::in();
¥tb= self::tbname(¥tb);
¥where = empty(¥terms) ? ’1′ : ¥terms;
¥query = “ count({¥fields}) `{¥tb}` where {¥where}”;
return ¥o->getVar(¥query);
}
function fetchdata (¥tb , ¥fields = ‘*’ , ¥terms = ”){
¥o = & self::in();
¥tb= self::tbname(¥tb);
¥data = array();
¥query = “ {¥fields} `{¥tb}` {¥terms}”;
return ¥o->getData(¥query);
}
function fetchrow (¥tb , ¥fields = ‘*’ , ¥terms = ”){
¥o = & self::in();
¥tb= self::tbname(¥tb);
¥data = array();
¥query = “ {¥fields} `{¥tb}` {¥terms}”;
return ¥o->getLine(¥query);
}
function fetchitem (¥tb , ¥field , ¥terms = ”){
¥o = & self::in();
¥tb= self::tbname(¥tb);
¥data = array();
¥query = “ {¥field} `{¥tb}` {¥terms}”;
return ¥o->getVar(¥query);
}
function (¥tb, ¥arr, ¥getid = false, ¥replace = false) {
¥o = & self::in();
¥tb= self::tbname(¥tb);
¥data = self::getdata(¥arr);
¥cmd = ¥replace ? ‘REPLACE INTO’ : ‘INSERT INTO’;
¥silence = ¥silence ? ‘SILENT’ : ”;
¥query = “{¥cmd} `{¥tb}` SET {¥data}”;
¥return = ¥o->runSql(¥query);
return ¥getid ? ¥o->lastId() : ¥return;
}
function _id() {
¥o = & self::in();
return ¥o->lastId();
}
function (¥tb, ¥arr, ¥terms = NULL , ¥getarows = false , ¥low_priority = false) {
¥o = & self::in();
¥tb= self::tbname(¥tb);
¥data = self::getdata(¥arr);
¥cmd = “UPDATE “.(¥low_priority ? ‘LOW_PRIORITY’ : ”);
¥where = empty(¥terms) ? ’1′ : ¥terms;
¥query = “{¥cmd} `{¥tb}` SET {¥data} WHERE {¥where}”;
¥return = ¥o->runSql(¥query);
return ¥getarows ? ¥o->affectedRows() : ¥return;
}
function (¥tb, ¥terms = NULL,¥getarows = false, ¥limit = 0) {
¥o = & self::in();
¥tb = self::tbname(¥tb);
¥where = empty(¥terms) ? ’1′ : ¥terms;
¥query = “DELETE FROM `{¥tb}` WHERE {¥where} “.(¥limit ? “LIMIT {¥limit}” : ”);
¥return = ¥o->runSql(¥query);
return ¥getarows ? ¥o->affectedRows() : ¥return;
}
function affected_rows() {
¥o = & self::in();
return ¥o->affectedRows();
}
function query(¥query) {
¥o = & self::in();
return ¥o->runSql(¥query);
}
function &in() {
static ¥object;
if(empty(¥object)) {
¥object = new SaeMysql();
}
return ¥object;
}
}
下载地址:http://www.kuigg.com/attachments/2011/10/kuigg_Db.rar
这个类不须要实例化,调用里面的办法的时辰只须要以kuigg_Db::function的情势就可以。
下面申明一下这里面的几个办法:
tbname:这是处理惩罚表名的办法,给注解增长前缀用。
重点是供给了完全 增删查改办法:
增:(tb,¥arr,¥getid = false,¥replace = false)
须要供给的几个参数分别是¥tb:须要插入的表名;¥arr:把插入的数据遵守key=字段名,value=值的情势机关的数组;¥getid:是否获取新插入的id,默认为false不获取,若是设为true就会返回id值;¥replace:是否以调换体式格式插入,默认为false,一般也用不到。
示例:
¥arr = array(‘user’ => ‘kuigg’ , ‘email’ => ‘i@kuigg.com’ , ‘website’ => ‘http://www.kuigg.com’);
¥uid = kuigg_Db::(‘user’, ¥arr, true);
删:(tb,¥terms = NULL,getarows = false,¥limit = 0)
须要供给的几个参数分别是¥tb:须要删除数据的表名;¥terms:删除的前提;¥getarows:是否获取被删除的行数,默认为false不获取,若是设为true就会此次操纵删除的行数;¥limit:删除的行数,默认0即删除所有合适前提的行。
示例:
¥rownum = kuigg_Db::(‘user’, “`uid` = ’1′”, true);
查:供给了4个办法:
fetchdata (¥tb , ¥fields = ‘*’ , ¥terms = ”)
fetchrow (¥tb , ¥fields = ‘*’ , ¥terms = ”)
fetchitem (¥tb , ¥field , ¥terms = ”)
count (¥tb , ¥fields = ‘*’ , ¥terms = ”)
分别可以取得多维数组、单维数组、单项值、统计数量。
改:(tb,¥arr,¥terms = NULL ,¥getarows = false ,¥low_priority = false)
须要供给的几个参数分别是¥tb:须要更新数据的表名;¥terms:须要更新的前提;¥getarows:是否获取更新的行数,默认为false不获取,若是设为true就会此次操纵更新的行数。
¥arr = array(‘user’ => ‘kuigg’ , ‘email’ => ‘i@kuigg.com’ , ‘website’ => ‘http://www.kuigg.com’);
¥rownum = kuigg_Db::(‘user’, ¥arr, “where uid = ’1′ “, true);
用了这个类,将来若是把应用迁出,只须要把这个类批改一下就可以了。
Copyright@ 2011-2016 版权所有:大连千亿科技有限公司 辽ICP备11013762-3号 google网站地图 百度网站地图 网站地图
公司地址:大连市沙河口区中山路692号辰熙星海国际2317 客服电话:0411-39943997 QQ:2088827823 37482752
法律声明:未经许可,任何模仿本站模板、转载本站内容等行为者,本站保留追究其法律责任的权利! 隐私权政策声明