如何通过PARAMS到PDO准备语句

问题描述 投票:0回答:1

我新来的PHP PDO,我想通过参数去查询在准备发言,但我已经失败了一次又一次,虽然这是工作中的mysqli报表不错,但在这里它没有显示我的记录没有显示任何error..problem是与经过PARAMS请有人指导我...

这里是我的代码..

if($_GET['title']!="" && isset($_GET['cities']) && isset($_GET['categories'])){

    $title = trim($_GET['title']);
    $cities = $_GET['cities'];
    $categories = $_GET['categories'];
    foreach($cities as $city) {
      $recordcities .= $city."|";
    }
    foreach($categories as $category){
      $recordCat .= $category."|";
    }

    $select = $pdo->prepare("select * from selectcities WHERE CONCAT(',', `cities`, ',') REGEXP ',(:cities),' and CONCAT(',', `category`, ',') REGEXP ',(:categories),' ");
    $select->bindParam(':cities',$recordCities,PDO::PARAM_STR);
    $select->bindParam(':categories',$recordCat,PDO::PARAM_STR);
    $select->execute();
    echo $select->rowCount();
    while($result = $select->fetch(PDO::FETCH_ASSOC)){
       //echo $result['Title'].'<br>';
       print_r($result);
    }
  }
php pdo
1个回答
1
投票

我认为你必须使绑定变量包含任何额外的标记,而不是试图将其包含在SQL,所以你的绑定成为',('.$recordCities.'),'和SQL只是REGEXP :cities ...

$select = $pdo->prepare("select * from selectcities 
            WHERE CONCAT(',', `cities`, ',') REGEXP :cities 
              and CONCAT(',', `category`, ',') REGEXP :categories ");
$recordCities = ',('.$recordCities.'),';
$recordCat = ',('.$recordCat.'),';
$select->bindParam(':cities',$recordCities,PDO::PARAM_STR);
$select->bindParam(':categories',$recordCat,PDO::PARAM_STR);
© www.soinside.com 2019 - 2024. All rights reserved.