RE: [PEAR] error returning with PEAR::DB
| From: | Torsten Roehr | Date: | Tue, 30 Mar 2004 20:03:17 +0000 |
| Subject: | RE: [PEAR] error returning with PEAR::DB | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-11538@lists.php.net to get a copy of this message | ||
Hi Pupeno,
if the PEAR error handling is set to PEAR_ERROR_RETURN (default mode) you can do:
// open connection bla bla
// process query
$result =& $db->query('INSERT bla bla...');
// check if an error occurred
if (is_a($result, 'DB_error')) {
// set a warning for the user that the user name exists
}
else {
// continue your process
}
The only problem I see is that you cannot check WHICH error occurred. So if your INSERT query has an
incorrect syntax you won't recognize it as the result would be the same as with an existing key
in your table. A better way would be to write a small function named userNameExists() that makes a
select query and looks for the user name. You could even make it more general and call it
recordSetExists() and use it for various tables and columns by passing an array with the required
values like table name, column name and so on. This works quite well for me.
Then you could do:
if (!userNameExists($params(...))) {
// insert new user
}
else {
// set warning
}
Hope this helps,
Regards,
Torsten Roehr
> -----Original Message-----
> From: Pupeno [mailto:pupeno@pupeno.com]
> Sent: Tuesday, March 30, 2004 9:24 PM
> To: pear-general@lists.php.net
> Subject: [PEAR] error returning with PEAR::DB
>
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> I have a database which has a unique key that is chosed by the user (the
> username), so, I should complain to the user if the username is
> already used
> and prompt for choosing another one... ok.
> I could check if that username was used by running a query...
> But I'm trying
> an alternate way to do it, I want to directly insert the new
> username and if
> it fails check why it failed and if it was the username, ask for
> a diferent
> username.
> Now, using PEAR::DB, how do I check what eactly was wrong in the insert ?
> Thank you.
> - --
> Pupeno: pupeno@pupeno.com
> http://www.pupeno.com
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.4 (GNU/Linux)
>
> iD8DBQFAaclFtCepaMf3unIRAkTUAJ0aY2+srv/3t6qDFhlg1wTgurKwFwCbBJsG
> HGiLZtmljoF69HzgdV+pZyI=
> =+6KM
> -----END PGP SIGNATURE-----
>
> --
> PEAR General Mailing List (http://pear.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>