1

I have not been able to find a suitable answer for why my PDO query isnt working with multiple "like" statements on the same column. When using a Joomla framework i can execute the query successfully .. i created output to show the query here:

Joomla core query function Output

SearchValue: Array (
    [0] => Test
    [1] => Name
)

Query output from joomla "$query->__toString()" command:
    SELECT id,name
    FROM portal_users
    WHERE name LIKE '%Test%' AND name LIKE '%Name%'

**data Set**

id    name
1     Test Name
2     Other name
3     Test Example

**Query Results**

Test Name

But when I create the same query using PDO it does not filter by both parameters and returns no results instead.

PDO submission and Output

SearchValue: Array (
    [0] => Test
    [1] => Name
)

Query submitted to PDO:
    SELECT id, name
    FROM portal_users
    WHERE name LIKE :var1 AND name LIKE :var2

PDO Data: Array (
    [:var1] => %Test%
    [:var2] => %Name%
)

**data Set**

id    name
1     Test Name
2     Other name
3     Test Example

**Query Results**

No results

I am at a loss to explain the cause of identical queries not working unless the PDO values are treated differently. Any input would be appreciated.

--UPDATE--

I have followed the example and request of user yourcommonsense and created the following code to validate my concern:

// first let's create a table 
// note that table is temporary so it won't pollute your database
$createQuery = "CREATE temporary TABLE test_users (id int auto_increment primary key, name varchar(255))";
$db->query($createQuery);

// then fill it with sample data
$insertQuery = "INSERT INTO test_users (name) VALUES ('Test Name'),('Another Name'),('Test Example')";
$db->query($insertQuery);

// now let's see if we have our data back all right
$dataCheck = "SELECT name FROM test_users";
echo "Test Query on temp table:\n$dataCheck\n";
$data = $db->query($dataCheck)->fetchAll(PDO::FETCH_ASSOC);
echo print_r($data,true);

// now let's test with a hardcoded value
$hardCodedQuery = "
    SELECT id, name
    FROM `test_users`
    WHERE name LIKE '%test%' AND name LIKE '%name%'
";
echo "Harcoded Query on temp table:\n$hardCodedQuery\n";
$data = $db->query($hardCodedQuery)->fetchAll(PDO::FETCH_ASSOC);
echo print_r($data,true);

// and finally, with a prepared statement
$preparedQuery = "
    SELECT id, name
    FROM test_users
    WHERE name LIKE :var1 AND name LIKE :var2
";
echo "Prepared Query on temp table:\n$preparedQuery\n";
$stmt = $db->prepare($preparedQuery);
$vars = array(":var1" => '%Test%', ":var2" => '%Name%');
$stmt->execute($vars);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo print_r($data,true);

I see that he is correct about my understanding as both the hardcoded statement, and the prepared statement in this code snippet do indeed result in the same results i was hoping to get from my original post above. I am still at a loss to explain why the original posted queries are not working in the same way. As stated earlier, the Joomla query and the PDO query I am running have the same structure, but the PDO above returns a successful query of 0 results. I may need to compare table structure? I will keep researching, but any other thoughts would be appreciated.

Oddly enough, the MCVE code running on my actual database works as it should, so i have to see what's different on my actual page vs. the MCVE.

Test Query on production table:
SELECT id, name FROM portal_users LIMIT 3
Array (
    [0] => Array ( [id] => 244 [name] => User One )
    [1] => Array ( [id] => 261 [name] => User Two )
    [2] => Array ( [id] => 262 [name] => User Three )
)

Harcoded Query on temp table:
    SELECT `users`.`id`,`users`.`name`
    FROM `portal_users` AS `users`
    WHERE `users`.`name` LIKE '%silver%' AND `users`.`name` LIKE '%tiger%'
Array (
    [0] => Array ( [id] => 7101 [name] => Silver Tiger )
)

Prepared Query on temp table:
    SELECT `users`.`id`, `users`.`name`
    FROM portal_users AS `users`
    WHERE  `users`.`name` LIKE :var1 AND `users`.`name` LIKE :var2

Supplied variables for prepared statement:
Array ( [:var1] => %silver% [:var2] => %tiger% )

Array (
    [0] => Array ( [id] => 7101 [name] => Silver Tiger )
)

Even when I copy and paste the query my PHP creates it will execute in a SQL query window through Navicat and return results...

SELECT id, name
FROM portal_users
WHERE name LIKE CONCAT('%','sil','%') AND name LIKE CONCAT('%','tig','%')

but when submitted via PHP PDO with the ? and array of variables, it has no results. still researching.

Silvertiger
  • 1,680
  • 2
  • 19
  • 32
  • 2
    It should work. [How to create a Minimal, Complete, and Verifiable example for a PDO related problem](https://phpdelusions.net/pdo/mcve) – Your Common Sense Feb 17 '18 at 16:11
  • As the SQL is nearly idnetical (sans the PDO binds) and the data set returns successful results using the Joomla core, i presume it is not a data set problem. I have included the code used to generate the queries as well, though it should not affect the result of the prepared queries submitted. the MCVE does not help to explain why these identical queries result in different results, but only helps me validate a data set, which in this case is the same for both queries, with different results. – Silvertiger Feb 17 '18 at 16:35
  • My original post (pre-wall of code) included the exact sql queries submitted, the values used when submitting, examples of the data set, and the results of the queries ... what additional information are you asking for? – Silvertiger Feb 17 '18 at 17:26
  • How exactly are you concluding that there were "No results"? Was there any error in between? – Salman A Feb 17 '18 at 17:27
  • There were no SQL errors returned, it was a successfully returned empty result set of no matches ... I determine this by performing a count of the records returned, if it is 0 I echo "no results" into the output. – Silvertiger Feb 17 '18 at 17:28
  • I have updated the post with the MCVE and see the merit to your feedback (spoiler confirmed). still having to fight through the difference between the MCVE and the actual code I have. – Silvertiger Feb 17 '18 at 18:05
  • [please read this answer](https://stackoverflow.com/a/36593020/3536236) – Martin Feb 17 '18 at 18:18
  • Martin - If you look at the PDO variables submitted in my first PDO example as output (section called PDO Data array:), it contains the %% around the variables. – Silvertiger Feb 17 '18 at 18:18
  • @Silvertiger only because you've just this minute updated your question to show them `:-p` – Martin Feb 17 '18 at 18:19
  • no, they were int he original post ... but i see the difference in the answer you posted .. the wildcard is in the statement, not the submitted variables... (i.e. the CONCAT statement) Let me adjust and try that .. – Silvertiger Feb 17 '18 at 18:19
  • @Martin WAT? What is this "power" you are talking about? And why PDO would "remove it"? Care to provide a proof to such a statement? – Your Common Sense Feb 18 '18 at 05:55
  • https://stackoverflow.com/questions/16255657/pdo-prepared-statements-with-wildcards – Martin Feb 18 '18 at 13:08

0 Answers0