Home > Backend Development > PHP Tutorial > yii2.0 两表联查where条件用in 的时候,怎么使用bindParam绑定参数

yii2.0 两表联查where条件用in 的时候,怎么使用bindParam绑定参数

WBOY
Release: 2016-06-06 20:06:08
Original
1495 people have browsed it

代码如下:

<code>public function test()
{
   $a = array(1,2,3,4,5);
    $uid = implode(',', $a);
    $sql = "SELECT A.uid,B.id,B.state FROM A
        LEFT JOIN B ON A.id=B.id
        WHERE A.uid in ($uid)";
    $cmd =  Self::getDb()->createCommand($sql);
    $res =  $cmd->queryAll();
    /*var_dump($res);
    array (size=3)
        0 => 
            array (size=6)
              'uid' => string '1' (length=2)
              'id' => string '1100' (length=5)
              'state' => string '2' (length=1)
        1 => 
            array (size=6)
              'uid' => string '2' (length=2)
              'id' => string '1200' (length=5)
              'state' => string '2' (length=1)
        2 => 
            array (size=6)
              'uid' => string '3' (length=2)
              'id' => string '1300' (length=5)
              'state' => string '2' (length=1)*/
</code>
Copy after login
Copy after login
<code>    //-----------------------------绑定参数法-----------------------------------------
    $sql2 = "SELECT A.uid,B.id,B.state FROM A
        LEFT JOIN B ON A.id=B.id
        WHERE A.uid in (:uid)";
    $cmd2 =  Self::getDb()->createCommand($sql2);
    $cmd2 -> bindParam(":uid",$uid);
    $res2 =  $cmd2->queryAll();
    /*var_dump($res2);
    array (size=3)
        0 => 
            array (size=6)
              'uid' => string '1' (length=2)
              'id' => string '1100' (length=5)
              'state' => string '2' (length=1)*/
}


假如$sql得到的结果有三条数据,用$sql2得到的结果只有一条数据(经测试,调换$a里元
素的位置发现,$res2的数据一直是$a里第一个有数据的元素所对应的那条数据)。
经过测试和了解,发现bindParam会在替换占位符时会在参数外边加一个单引号,这样我
的in条件里边就变成了一个整体,而不是五个值。

问题:
1.就算作为一个整体,为什么还会得到一条数据呢。
2.本题怎么改正
3.YII2里,如果要使用占位符,where条件要用到in时,应该怎么使用</code>
Copy after login
Copy after login

回复内容:

代码如下:

<code>public function test()
{
   $a = array(1,2,3,4,5);
    $uid = implode(',', $a);
    $sql = "SELECT A.uid,B.id,B.state FROM A
        LEFT JOIN B ON A.id=B.id
        WHERE A.uid in ($uid)";
    $cmd =  Self::getDb()->createCommand($sql);
    $res =  $cmd->queryAll();
    /*var_dump($res);
    array (size=3)
        0 => 
            array (size=6)
              'uid' => string '1' (length=2)
              'id' => string '1100' (length=5)
              'state' => string '2' (length=1)
        1 => 
            array (size=6)
              'uid' => string '2' (length=2)
              'id' => string '1200' (length=5)
              'state' => string '2' (length=1)
        2 => 
            array (size=6)
              'uid' => string '3' (length=2)
              'id' => string '1300' (length=5)
              'state' => string '2' (length=1)*/
</code>
Copy after login
Copy after login
<code>    //-----------------------------绑定参数法-----------------------------------------
    $sql2 = "SELECT A.uid,B.id,B.state FROM A
        LEFT JOIN B ON A.id=B.id
        WHERE A.uid in (:uid)";
    $cmd2 =  Self::getDb()->createCommand($sql2);
    $cmd2 -> bindParam(":uid",$uid);
    $res2 =  $cmd2->queryAll();
    /*var_dump($res2);
    array (size=3)
        0 => 
            array (size=6)
              'uid' => string '1' (length=2)
              'id' => string '1100' (length=5)
              'state' => string '2' (length=1)*/
}


假如$sql得到的结果有三条数据,用$sql2得到的结果只有一条数据(经测试,调换$a里元
素的位置发现,$res2的数据一直是$a里第一个有数据的元素所对应的那条数据)。
经过测试和了解,发现bindParam会在替换占位符时会在参数外边加一个单引号,这样我
的in条件里边就变成了一个整体,而不是五个值。

问题:
1.就算作为一个整体,为什么还会得到一条数据呢。
2.本题怎么改正
3.YII2里,如果要使用占位符,where条件要用到in时,应该怎么使用</code>
Copy after login
Copy after login

请使用 Yii2 提供的AR直接操作,请参考 http://www.yiiframework.com/doc-2.0/guide-db-active-record.html

http://stackoverflow.com/questions/14767530/php-using-pdo-with-in-clause-array

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template