Home > php教程 > php手册 > php join 构造sql查询语句

php join 构造sql查询语句

WBOY
Release: 2016-05-25 16:50:00
Original
1856 people have browsed it

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

<?php
$arr = array(
    &#39;hello&#39;,
    &#39;world!&#39;,
    &#39;beautiful&#39;,
    &#39;day!&#39;
);
echo join(" ", $arr);
?>
Copy after login

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

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

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

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

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

本文链接:

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template