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,061
  • 2
  • 28
  • 40
J. Doe
  • 133
  • 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
  • 41,216
  • 9
  • 68
  • 101
1

try using a proper string eg: using concat

    ("SELECT id FROM titles WHERE skuid LIKE concat('%', :cusa, '%')");
ScaisEdge
  • 129,293
  • 10
  • 87
  • 97