Re: DB and INSERT's allready exists?
| From: | Justin Patrin | Date: | Wed, 13 Jul 2005 18:18:53 +0000 |
| Subject: | Re: DB and INSERT's allready exists? | ||
| References: | 1 2 3 4 5 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-19980@lists.php.net to get a copy of this message | ||
On 7/13/05, Martin Klaffenboeck <martin.klaffenboeck@gmx.at> wrote:
> Am Mittwoch, den 13.07.2005, 10:04 -0700 schrieb Justin Patrin:
>
> > Why? Because it's always good sense to check for all possible errors
> > before processing your data. However, even if you do the select it's
> > still possible for an error to happen (say two people register with
> > the same e-mail at the same moment in time, the selects will go
> > through fine, one user will register, the other will get an INSERT
> > error).
> >
> > You should be checking for an error return from whatever DB
> > abstraction layer you're using. Assuming DB:
> > $ret = $db->query('INSERT INTO users (email) VALUES
> > ('.$db->quoteSmart($emailAddress).')');
> > if (PEAR::isError($ret)) {
> > if ($ret->getCode() == DB_ERROR_CINSTRAINT) {
> > echo 'This email address has been used before';
> > } else {
> > echo 'There has been an error';
> > mail('admin@example.com', 'Error in user insert', print_r($ret,
> > true));
> > }
> > } else {
> > //go on
> > }
>
> Thats exacly what I'm doing. There is no select before, I would say a
> select is overhead in this case.
If this is what you're doing why are you asking? This should work fine.
>
> I'm used to work with errors by using python. There we do many
> exceptions (like php5 has? im not sure). Thats the way to use the
> errors for someones case. And here its the same to.
>
PHP4 does not have exceptions and most PEAR packages are written for
PHP4. PHP5 does have exceptions but it will take a while for PEAR
packages to be rewritten for it. For now you have to use PEAR::isError
on returns or define your own error handler (which probably wouldn't
help in this case anyway).
--
Justin Patrin