Re: DB wildcards
| From: | Justin Patrin | Date: | Sat, 14 May 2005 14:31:21 +0000 |
| Subject: | Re: DB wildcards | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-19215@lists.php.net to get a copy of this message | ||
On 5/13/05, Danny Brow <dan@fullmotions.com> wrote:
> I can't seem to get wild cards (?) to work right, the below code returns
> nothing from the database. But if I change the first ? to last_name
> like:
>
> $q = $db->getAll('SELECT * FROM patients WHERE last_name LIKE ?',
> array($last_name));
>
> it works fine, but when I change the field type to ? and try to pull the
> value in from a variable or $_POST I get nothing. I've just looked
> through three books and searched google but can't find an answer.
>
> Help please.
>
> Dan.
>
> <?php
>
> include 'DB.php';
>
> $db = DB::connect('pgsql://foo:bar@localhost/foodb');
> if (DB::isError($db)) { die("Can't connect to the database:" .
> $db->getMessage()); }
>
> $db->setErrorHandling(PEAR_ERROR_DIE);
>
> $db->setFetchMode(DB_FETCHMODE_OBJECT);
>
> $last_name = 'Brow';
> $field = 'last_name';
>
> $q = $db->getAll('SELECT * FROM patients WHERE ? LIKE ?',
> array($field, $last_name));
This isn't working because ? is a placeholder for *data*, not for
identifiers. When you use this the field name get quoted as data and
this makes the query fail. You need to do something like this:
$1 = $db->getAll('SELECT * FROM patients WHERE
'.$db->quoteIndentifier($field).' LIKE ?',
array($last_name));
>
> $error = $db->getMessage($q);
>
> print_r($q)
>
> ?>
>
--
Justin Patrin