Re: problems with nextId
| From: | Markus Wolff | Date: | Fri, 16 Aug 2002 16:36:45 +0000 |
| Subject: | Re: problems with nextId | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-2138@lists.php.net to get a copy of this message | ||
On Fri, 16 Aug 2002 11:03:42 -0500
"Matt McDonald" <matt@coffeecup.com> wrote:
> I'm having some problem understanding exactly how the nextID function works.
> From pear.php:
>
> <?php
> ...
> $id = $db->nextId('mySequence');
> // Use the ID in your INSERT query
> $res = $db->query("INSERT INTO myTable (id,text) VALUES ($id,'foo')");
> ...
> ?>
>
> First off, can you have multiple sequences for multiple tables? How do you
> assign a sequence to a particluar table.
Yes, you can have either multiple sequences for multiple tables or one
sequence that generates IDs for an arbitrary number of tables.
The trick with assigning sequences to a table is that you don´t assign
them. You just have to fetch the IDs for a particular table from always
the same sequence to guarantee that you´ll never have duplicate IDs.
> When I run $db->nextId('mySequence'); it returns an object, not an int, ans
> I was hoping. I seem to be missing something (obvious?) here.
Have you checked the result with DB::isError()? Use this code to check:
$result = $db->nextId('mySequence');
if (DB::isError($result)) {
echo ("An error has occured: ".$result->getMessage());
} else {
$newID = $result;
// Do your INSERT query here using the new ID
}
If an error occurs with nextId(), especially if you´re trying to pull a
value from a sequence that you´ve never used before, it is quite likely
that you´re connected to the database using a user account that doesn´t
have the right to create a new table (if you´re using MySQL - otherwise
you have to check for the right to create a new sequence).
In that case, you can either GRANT more rights to the user, or create
the table/sequence manually with a superuser account.
Regards,
Markus
--
Markus Wolff <wolff@21st.de>