多字搜索不会在PHP中显示数组

时间:2017-02-03 14:38:54

标签: php

为什么代码

$varTerm = explode(" ", $nomer);
    $termsStringArray = array();
    $termsArray = array();
    foreach($varTerm as $term){
        $term = trim($term);
        if(!empty($term)) {
            array_push($termsStringArray, "adrs LIKE ? ");
            array_push($termsArray, $term);
            array_push($termsArray, $term);  // note, you can do this part differently, if you'd like
        }
    }
    $implodedTermsString = implode('AND ', $termsStringArray);
    //echo $implodedTermsString;
    print_r($termsArray);
 $sth = $pdo->prepare("SELECT * 
 FROM sms_addrs WHERE ". $implodedTermsString);
var_dump($sth);
$sth->execute($termsArray);
var_dump($sth);

显示此输出:

Array ( [0] => shell [1] => shell [2] => Compans [3] => Compans ) 

object(PDOStatement)#2 (1) { ["queryString"]=> string(61) "SELECT * FROM sms_addrs WHERE adrs LIKE ? AND adrs LIKE ? " }

我的意思是为什么我看不到的话?标记

如果我使用像这里的直接词:

$sth->execute(array("%shell%","%Compans%"));

我在行中获得所需的输出。

1 个答案:

答案 0 :(得分:0)

我找到了一个有效的代码:

$search = strtoupper($nomer);
$search = strip_tags($search); 
$search = trim($search);
$words = explode(' ', $search);
$words_condition = array();
$arguments = array();
foreach ($words as $word) {
      $words_condition[] = 'adrs LIKE ?';
      $arguments[] = '%'.$word.'%';
}

$sth = $pdo->prepare('SELECT * FROM sms_addrs WHERE '.implode(' AND ',$words_condition));
$sth->execute($arguments);

我的问题是沉闷的!谢谢大家!