在PHPLIB中的MYSQL类中加入这些函数,看看是不是有意义,大家评评。 没有多大意义,select( $table, $fields = "*", $where = "", $order = "", $group = "", $limit = 10 )如果写成这个样子我要调试,测试一下sql都很不方便.这个主意还是蛮有创意的. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 调试并不难,因为MYSQL类有错误提示的。再说了,可以加个SQL语句输出的函数,这并不是什么大问题。而且我贴出来不是让大家用我的这个东东,因为我也没有具体测试过。。只是想把这个想法共享出来。然后大家一起来完善一下。。 要过滤什么字符??有高见请具体说说好吗??下面是我重新修改了一下的。。 /* public: insert record */ function insert( $table, $data) { if ( is_array( $data ) ) { $this->sql = "INSERT INTO $table ( `".implode( "` , `" , array_keys($data) )."` ) VALUES ( '".implode( "' , '", array_values($data) )."' )"; } else { die("<strong>Error</strong> : Data Empty! "); } return $this->query( $this->sql ); } /* public: delete record */ function delete( $table, $where = "", $limit = 0 ) { $this->sql = "DELETE FROM $table "; $this->sql .= $this->where($where); $this->sql .= intval($limit) ? " LIMIT ". intval($limit) : ""; return $this->query( $this->sql ); } /* public: update record */ function update( $table, $update = "", $where = "", $limit = 0 ) { $setword = ""; if( is_array( $update ) ) { while ( list( $field, $value ) = each($update) ) { $setword .= $setword ? " , " : ""; $setword .= "`$field` = '".$value."'"; } }else { $setword = $update; } $this->sql = "UPDATE $table SET $setword "; $this->sql .= $this->where($where); $this->sql .= intval($limit) ? " LIMIT ". intval($limit) : ""; return $this->query( $this->sql ); } /* public: select record */ function select( $table, $fields = "*", $where = "", $order = "", $group = "", $limit = 10 ) { } /* public: set where */ function where( $where ) { $condition = ""; if( is_array( $where ) ) { foreach( $where as $key => $val ){ if( preg_match( "/^\d*$/", $key ) ) {//or foreach( $val as $k => $v ) { $condition .= $condition ? " OR ( `$k` = '".implode( "' AND `$k` = '", $v )."' )" : " WHERE ( `$k` = '".implode( "' AND `$k` = '", $v )."')"; } }else {//and $condition .= $condition ? " AND ( `$key` = '".implode( "' OR `$key` = '", $val )."')" : " WHERE ( `$key` = '".implode( "' OR `$key` = '", $val )."')"; } } }else { $condition .= $where; } return $condition; } /* public: show sql */ function showsql() { return $this->sql; } 这些封装做一个类。不用return 直接query了 之所以不另外封装成独立的类,是因为MYSQL类里有方便的东西在里面嘛。比如数据库连接,选择数据库,错误提示等等再说了都是MYSQL语句的处理,放在一起我觉得是挺好了。用RETURN的意思就是检测执行是否成功或者说返回SQL执行后的返回值。请大家帮我完善一下好吗??尤其是那个SELECT不晓得怎么做。。 查询是一种很复杂的情,我很久以前就向楼主一样,做了insert , update ,delete,当然也做了查询,但查询只是针对一个表的。但往往现实中的查询往往是关联很多表的。所以我做了一个类,有上面所有的功能,别外加了一个execute、和query方法。第一个用于返回执行insert , update ,delete,后的引响行数另一个用于返回处理后的查询结果。我喜欢的是带关键字的数组(^_^)如果为了省事,直接用Adodb类库。现在我正在用(^_^) Adodb类库哪里有?我查了半天都没有下载的呢? 你可以去官方网站上下载最新版本http://php.weblogs.com/adodb相关的说明请看http://www.phpe.net/articles/373.shtml php 正则替换的问题 动态多逻辑关系多条件查询的php语句 php里一个关于循环的问题 php怎么写web serviec? .net代码转PHP,请高手帮忙~~~ PHP的时间问题 谁有Apache 2 和 PHP 4.3 的安装文档?让我参考一下 PHP支持access数据库么? PHP中如何捕捉鼠标? 请问 nginx下如何忽略大小写目录和文件 gettimeofday()的问题,郁闷,不懂! 有没有人用PHP开发过语音视频聊天室?
再说了,可以加个SQL语句输出的函数,这并不是什么大问题。而且我贴出来不是让大家用我的这个东东,因为我也没有具体测试过。。
只是想把这个想法共享出来。然后大家一起来完善一下。。
下面是我重新修改了一下的。。
/* public: insert record */
function insert( $table, $data) {
if ( is_array( $data ) ) {
$this->sql = "INSERT INTO $table ( `".implode( "` , `" , array_keys($data) )."` )
VALUES ( '".implode( "' , '", array_values($data) )."' )";
} else {
die("<strong>Error</strong> : Data Empty! ");
}
return $this->query( $this->sql );
}
/* public: delete record */
function delete( $table, $where = "", $limit = 0 ) {
$this->sql = "DELETE FROM $table ";
$this->sql .= $this->where($where);
$this->sql .= intval($limit) ? " LIMIT ". intval($limit) : "";
return $this->query( $this->sql );
}
/* public: update record */
function update( $table, $update = "", $where = "", $limit = 0 ) {
$setword = "";
if( is_array( $update ) ) {
while ( list( $field, $value ) = each($update) ) {
$setword .= $setword ? " , " : "";
$setword .= "`$field` = '".$value."'";
}
}else {
$setword = $update;
}
$this->sql = "UPDATE $table SET $setword ";
$this->sql .= $this->where($where);
$this->sql .= intval($limit) ? " LIMIT ". intval($limit) : "";
return $this->query( $this->sql );
}
/* public: select record */
function select( $table, $fields = "*", $where = "", $order = "", $group = "", $limit = 10 ) {
}
/* public: set where */
function where( $where ) {
$condition = "";
if( is_array( $where ) ) {
foreach( $where as $key => $val ){
if( preg_match( "/^\d*$/", $key ) ) {//or
foreach( $val as $k => $v ) {
$condition .= $condition
? " OR ( `$k` = '".implode( "' AND `$k` = '", $v )."' )"
: " WHERE ( `$k` = '".implode( "' AND `$k` = '", $v )."')";
}
}else {//and
$condition .= $condition
? " AND ( `$key` = '".implode( "' OR `$key` = '", $val )."')"
: " WHERE ( `$key` = '".implode( "' OR `$key` = '", $val )."')";
}
}
}else {
$condition .= $where;
}
return $condition;
}
/* public: show sql */
function showsql() {
return $this->sql;
}
用RETURN的意思就是检测执行是否成功或者说返回SQL执行后的返回值。请大家帮我完善一下好吗??
尤其是那个SELECT不晓得怎么做。。
http://php.weblogs.com/adodb相关的说明请看
http://www.phpe.net/articles/373.shtml