Skip to content

Instantly share code, notes, and snippets.

@eMerzh

eMerzh/File.php Secret

Created January 20, 2012 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eMerzh/d83a9c5436d7cb8cebec to your computer and use it in GitHub Desktop.
Save eMerzh/d83a9c5436d7cb8cebec to your computer and use it in GitHub Desktop.
"Real" world example
CREATE OR REPLACE FUNCTION normalize(to_handle varchar) RETURNS varchar STRICT
AS $$
BEGIN
return lower(regexp_replace(handle,'[^[:alnum:]]','', 'g'));
END;
$$ LANGUAGE plpgsql IMMUTABLE;
CREATE TABLE taxonomy ( id integer, name varchar, name_normalized varchar);
/** A trigger that does taxonomy.name_normalized := normalize(name); on insert and update; ***/
<?php
/** search for Taxon **/
$sth = $dbh->prepare("SELECT id, name
FROM taxonomy
WHERE name_normalized like normalize(?) || '%' ");
$sth->bindValue(1, $_POST['name'], PDO::PARAM_STR);
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment