I'm trying to execute a query with LIKE '%txt%'
in a Firebird database, Im using PHP with PDO and the connection is OK, I'm just not getting results (the echo returns []) with this code:
header('Content-type: text/plain; charset=ISO-8859-1');
$host = 'firebird:dbname=192.x.x.x:/usr/interbase/test.gdb';
$username = 'test';
$password = 'test';
try{
$db = new PDO($host, $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
echo "Error: " . $e->getMessage();
}
$txt = "%Oll%";
$sql = "SELECT PRV_CODIGO id, PRV_RAZSOC text ";
$sql .= "FROM T_MYTABLE ";
$sql .= "WHERE PRV_RUT LIKE ':txt' OR PRV_RAZSOC LIKE ':txt'";
try{
$select = $db->prepare($sql);
$select->bindValue(":txt",$txt,PDO::PARAM_STR);
$select->execute();
$result = $select->fetchAll(PDO::FETCH_ASSOC);
//var_dump($select);
echo json_encode($result);
}catch (PDOException $e) {
echo $e->getMessage();
}
I have tested this things:
If I uncomment that var_dump, it shows the following in the web browser:
object(PDOStatement)#2 (1) {
["queryString"]=>
string(106) "SELECT PRV_CODIGO id, PRV_RAZSOC text FROM T_MYTABLE WHERE PRV_RUT LIKE ':txt' OR PRV_RAZSOC LIKE ':txt'"
}
And you can see that the string is keeping the ':txt' intact.
If I put the variable directly in the query, with no prepared statement, like this:
$sql = "SELECT PRV_CODIGO id, PRV_RAZSOC text ";
$sql .= "FROM T_MYTABLE ";
$sql .= "WHERE PRV_RUT LIKE '$txt' OR PRV_RAZSOC LIKE '$txt'";
It works well, it brings the results and that var_dump shows the following:
object(PDOStatement)#2 (1) {
["queryString"]=>
string(108) "SELECT PRV_CODIGO id, PRV_RAZSOC text FROM T_MYTABLE WHERE PRV_RUT LIKE '%Oll%' OR PRV_RAZSOC LIKE '%Oll%'"
}
It works because the string is now getting the $txt value and not preserving the ':txt'. Of course I can't put that variable there because I'm trying to do this with prepared statement.
So I need to know what is happening, I have never had this kind of problems with MySQL.