本文摘自PHP中文网,作者慕斯,侵删。
上篇文章给大家介绍了《如何运用PHP进行数据库操作类?》,本文继续给大家介绍在PHP中数据库的查询方法应该如何实现??有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。查询方法应该如何实现:我们根据以前所讲的原理,我们需要用到以下方法:
//filed方法
//table方法
/ /where方法
//group方法
/ /having方法
//order方法
//limit方法
,如果说我们调用了其中的一个,那么我就会将其中的一个保存到一个数组中,最终我们通过select方法查询,最后将结果返回给我们,接下来我们就可以,以代码的形式呈现,代码如下:
我们先定义一个(function)函数;我们通过field方法,当传递过来以后,我们要判断,field是否传递了,如果不为空,我们会继续往下传递,如果为空,我们则直接返回$this,意思就是如果不为空,再进行处理,
//field 方法:
1 2 3 4 5 6 7 8 9 10 11 12 | function field( $field )
{
if (! empty ( $field )) {
if (is_ string( $field )) {
$this ->options[ 'field' ] = $field ;
} else if (is_ array ( $field )) {
$this ->options[ 'field' ] = join( ',' , $field );
}
}
return $this ;
}
|
//table方法:
一样的首先我们也是要判断是否为空;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function table( $table )
{
if (! empty ( $table )) {
$this ->options[ 'table' ] = $table ;
}
return $this ;
}
function where ( $where )
{
if (! empty ( $where )) {
$this ->options[ 'where' ] = 'where ' . $where ;
}
return $this ;
}
|
//group方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function group( $group )
if (! empty ( $group )) {
$this - >options[ ' group' ] = 'group by ' . $group ;
}
return $this ;
}
function having( $having )
{
if (! empty ( $having )) {
$this ->options[ 'having' ] = 'having' . $having ;
}
return $this ;
}
|
//order方法
1 2 3 4 5 6 7 | function order( $order )
{
if (! empty ( $order )) {
$this ->options[ 'order' ] = 'order by' . $order ;
}
return $thiys ;
}
|
//limit方法
1 2 3 4 5 6 7 8 9 10 | function limit( $limit )
{
if (! empty ( $limit )) {
if ( is_string ( $limit )) {
$this ->options[ 'limit' ] = 'limit' . $limit ;
} else if ( is_array ( $limit )) {
$this ->options[ 'limit' ] = 'limit' . join( ',' , $limit );
}
}
}
|
以上就是我们对外公开的一些方法;
推荐教程:《MySQL教程》
以上就是在PHP中数据库的查询方法应该如何实现?的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
mssql中having的用处详解
sql如何删除多条数据
php 屏蔽关键字的方法
细数mysql中sql语句的分类
sqlserver sql性能优化技巧
c#操作linq to sql组件进行数据库建模的基本教程
10种java开发者编写sql语句时常见错误
使用excel自动生成sql语句
触发器的类型有哪三种?
asp.net封装的sql数据库访问类
更多相关阅读请进入《查询方法》频道 >>
机械工业出版社
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。
转载请注明出处:木庄网络博客 » 在PHP中数据库的查询方法应该如何实现?