Skip to content

Instantly share code, notes, and snippets.

@mnvx
Created September 18, 2019 15:35
Show Gist options
  • Save mnvx/616d84801aaa4db5fbeb0bad5df85876 to your computer and use it in GitHub Desktop.
Save mnvx/616d84801aaa4db5fbeb0bad5df85876 to your computer and use it in GitHub Desktop.
pdo_pgsql test
<?php
//////////////////////////////////// Returns 0 records
$pdo = new \PDO('pgsql:host=localhost;dbname=postgres', 'postgres', 'postgres');
$sql = <<<SQL
SELECT *
FROM (
SELECT 'CHAC TECHNOLOG*' as alias
UNION
SELECT 'KINDERY LIGHTING SALES DE?T*'
) m
JOIN (
SELECT 'CHACTECHNOLOGICO\\' as ie_clean
UNION
SELECT 'KINDERYLIGHTINGSALESDEPT' as ie_clean
) t ON t.ie_clean ILIKE REPLACE(REPLACE(REPLACE(m.alias, '*', '%'), ?, '_'), ' ', '')
ORDER BY ie_clean;
SQL;
echo $sql . PHP_EOL . PHP_EOL;
$stmt = $pdo->prepare($sql);
$stmt->execute(['?']);
print_r($stmt->fetchAll(\PDO::FETCH_ASSOC));
//////////////////////////////////// Returns 2 records
$pdo = new \PDO('pgsql:host=localhost;dbname=postgres', 'postgres', 'postgres');
$sql = <<<SQL
SELECT *
FROM (
SELECT 'CHAC TECHNOLOG*' as alias
UNION
SELECT 'KINDERY LIGHTING SALES DE?T*'
) m
JOIN (
SELECT 'CHACTECHNOLOGICO' as ie_clean
UNION
SELECT 'KINDERYLIGHTINGSALESDEPT' as ie_clean
) t ON t.ie_clean ILIKE REPLACE(REPLACE(REPLACE(m.alias, '*', '%'), ?, '_'), ' ', '')
ORDER BY ie_clean;
SQL;
echo $sql . PHP_EOL . PHP_EOL;
$stmt = $pdo->prepare($sql);
$stmt->execute(['?']);
print_r($stmt->fetchAll(\PDO::FETCH_ASSOC));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment