首页 > php开发 > php join 构造sql查询语句

php join 构造sql查询语句

join() 函数把数组元素组合为一个字符串,join() 函数是 implode() 函数的别名,例如下代码:

<?php
$arr = array(
    'hello',
    'world!',
    'beautiful',
    'day!'
);
echo join(" ", $arr);
?>

下面是一段dedecms搜索页面的条件利用了join函数:

<?php
if ($this->starttime > 0) {
    $ksqls[] = " arc.senddate>'" . $this->starttime . "' ";
}
if ($this->typeid > 0) {
    $ksqls[] = " typeid in (" . getsonids($this->typeid) . ") ";
}
if ($this->channeltype > 0) {
    $ksqls[] = " arc.channel='" . $this->channeltype . "'";
}
if ($this->mid > 0) {
    $ksqls[] = " arc.mid = '" . $this->mid . "'";
}
$ksqls[] = " arc.arcrank > -1 ";
$this->addsql = ($ksql == '' ? join(' and ', $ksqls) : join(' and ', $ksqls) . " and ($ksql)");
?>

它这里就构成了一条select * from 表 where 条件,根据$ksql多少来构造成一条完整的sql查询语句.

注释:join() 可以接收两种参数顺序,但是由于历史原因,explode() 是不行的,你必须保证 separator 参数在 string 参数之前才行。

数据库教程中的表可通过键将彼此联系起来,主键(primary key)是一个列,在这个列中的每一行的值都是唯一的,在表中,每个主键的值都是唯一的,这样做的目的是在不重复每个表中的所有数据的情况下,把表间的数据交叉捆绑在一起。

本文链接:http://www.phprm.com/develop/fs1322.html

收藏随意^^请保留教程地址.

标签:join 构造 sql查询

相关文章

发表留言