Inam
Inam

Reputation: 241

How to pass Params to PDO prepare statement

I m new to PHP PDO and I want to pass param to a query in prepare statement but i have been failed again and again although it is working fine in mysqli statements but here it is not showing me records not showing any error..problem is with passing the params please some one guide me ...

Here is my code..

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);
    }
  }

Upvotes: 0

Views: 50

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57121

I think you have to make the bind variables contain any extra markup rather than try and include it in the SQL, so your bind becomes ',('.$recordCities.'),' and the SQL is just 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);

Upvotes: 1

Related Questions