dimanche 26 juin 2016

Turn mysql like into wildcard

I am capturing a bunch of post data from a multi option search bar. The $_POST['brand'] is free text and I would like to turn it into a wild card.

first part - turn it into session array so I can paginate it

$_SESSION['search'] = array(
    'stock_type' => mysqli_real_escape_string($connection, $_POST['stock_type']),
    'trade_type' => mysqli_real_escape_string($connection, $_POST['trade_type']),
    'brand' => mysqli_real_escape_string($connection, $_POST['brand']),
    'country' => mysqli_real_escape_string($connection, $_POST['country']),
    'min' => mysqli_real_escape_string($connection, $_POST['min']),
    'max' => mysqli_real_escape_string($connection, $_POST['max']),
    'user' => mysqli_real_escape_string($connection, $_POST['user'])
);

Second part is building the query

How do I turn the like into a wildcard?

if (!empty($_SESSION['search']['brand'])) {
   $where .= " and t.brand LIKE '" . $_SESSION['search']['brand'] . "'";    
}

Have tried

if (!empty($_SESSION['search']['brand'])) {
    $where .= " and t.brand LIKE '%" .$_SESSION['search']['brand']. "%'";
 }

suggestions welcome

Aucun commentaire:

Enregistrer un commentaire