Re: problems with nextId
| From: | Matt McDonald | Date: | Fri, 16 Aug 2002 17:10:13 +0000 |
| Subject: | Re: problems with nextId | ||
| References: | 1 2 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-2139@lists.php.net to get a copy of this message | ||
Thanks for the help, I think I'm starting to get somewhere, but I'm still
not fully understanding where the problem is. FYI, I'm using MySQL 3.23.39
I gave the account full database permissions, then tried your script listed
below, and came back with this:
An error has occured: DB Error: DB backend not capable
So is it my version of MySQL the problem (it's fairly close to the latest
stable version available), or is it the way I have my tables configured?
I'm still kind of confused how sequences work. Right now, I've got 22
tables set up in by database. So when I call nextId to set up a new
sequence, does it scan all tables and come up with a value that would work
no matter what table I insert into? If that is the case, perhaps there is a
table I have configured that stumps the scanning process?
Matt
> 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>