1

I'm trying to find a value from my database using SQL LIKE:

SELECT id FROM titles WHERE skuid LIKE '%:cusa%'

Where :cusa is defined CUSA06536 as a dynamic value in my PHP code.

It should return:

enter image description here

But instead it returns nothing. No errors are outputted. Am I doing something wrong? Are dynamic values not supported?


PHP script:

public function findCusa($cusa) {
    $find = $this->query("SELECT id FROM titles WHERE skuid LIKE '%:cusa%'");
    $find->execute(array(":cusa" => $cusa));

    if($find->rowCount() > 0) {
        echo 'found!';
    }

    echo 'not found';
}

Then called as $ps->findCusa('CUSA06536'); which returns not found.

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
J. Doe
  • 143
  • 1
  • 10

2 Answers2

3

Don't add the % in the LIKE statement, instead do it in the PHP code. Most likely your DB framework is getting confused when you use a quoted bind parameter.

$find = $this->query("SELECT id FROM titles WHERE skuid LIKE :cusa");
$find->execute(array(":cusa" => '%' . $cusa . '%'));
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
1

try using a proper string eg: using concat

    ("SELECT id FROM titles WHERE skuid LIKE concat('%', :cusa, '%')");
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107