<div>Hi All<br>As you suggested, I changed the code where I do not put auto-increment field in db_query.</div>
<div>The code looks as below (Please note that $cid is not used in db_query).</div>
<div> </div>
<div> </div>
<div> <em> for($delta = 0; $delta < $all_companies; $delta++)<br> {<br> drupal_set_message(t('delta = ').$delta.t('last id = ').db_last_insert_id(rs_companies, cid));<br> db_query(<br> 'INSERT INTO {rs_companies} (uid, prevcompany, joindate, releasedate) '<br>
."VALUES ('%d', '%s', '%d', '%d')", <br> $user->uid, <br> $form_state['values']['allcompanies'][$delta]['companies'],<br> $form_state['values']['allcompanies'][$delta]['startdate'],<br>
$form_state['values']['allcompanies'][$delta]['enddate']<br> );</em></div>
<div> </div>
<div> </div>
<div>So I have not put $cid, when I checked the table, I see $cid values are inserted properly.</div>
<div>$cid in row 1 value is 155694</div>
<div>$cid in row 2 value is 155695</div>
<div>$cid in row 3 value is 155696<br></div>
<div>etc...</div>
<div> </div>
<div>My question is , why the value is starting from 155694, why not from 0 or 1.</div>
<div> </div>
<div> </div>
<div>Is there anyway I force it to start from 0 or 1.</div>
<div> </div>
<div> </div>
<div>Regards</div>
<div>Austin.</div>
<div> </div>
<div> </div>
<div> </div>
<div class="gmail_quote">On Mon, Mar 28, 2011 at 7:50 PM, Larry Garfield <span dir="ltr"><<a href="mailto:larry@garfieldtech.com">larry@garfieldtech.com</a>></span> wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">Yep, you're doing it backwards, actually. :-)<br><br>In D6:<br><br>db_query("INSERT INTO {foo} (a, b) VALUES (%d, '%s')", 1, 'hi');<br>
$cid = db_last_insert_id(); // The DB auto-generated this value.<br><br>In D7:<br><br>$cid = db_insert()<br> ->fields(array(<br> 'a' => 1,<br> 'b' => 'hi'<br> ))<br> ->execute();<br>
// db_insert()->execute() returns the new auto-inc value automatically.<br><br>Note: In D7, you can do a multi-insert statement where several inserts run as<br>a set. That is faster than issuing separate queries, but the return value is<br>
then undefined and you do not have access to the last-insert-id. Decide if<br>you *actually* need to auto-generated ID right then and then pick multi-insert<br>or not as appropriate.<br><br>As to your other question, the max number of rows, bear in mind that the<br>
number of rows may not correspond go the largest auto-inc value. If you ever<br>delete a row then the two will no longer match up. Depending on which you<br>care about, either of the following are valid (this is D7 syntax):<br>
<br>$num_records = db_query("SELECT COUNT(*) FROM {foo}")->fetchField();<br><br>$max_cid = db_query("SELECT MAX(cid) FROM {foo}")->fetchField();<br><br>Cheers.<br><br>--Larry Garfield<br>
<div>
<div></div>
<div class="h5"><br>On Monday, March 28, 2011 5:59:14 am Austin Einter wrote:<br>> Thanks Nancy.<br>> Do you mean that in my code I should not put the value for "cid". That<br>> means $lastid (in red) should be removed.<br>
><br>><br>> *$lastid = 0;<br>> for($delta = 0; $delta < $all_companies; $delta++)<br>> {<br>> $lastid = db_last_insert_id(rs_companies, cid) + 1;<br>> db_query(<br>> 'INSERT INTO {rs_companies} ( cid, uid, prevcompany, joindate,<br>
> releasedate) '<br>> ."VALUES (%d, '%d', '%s', '%d', '%d')",<br>> $lastid, *<br>> *$user->uid*<br>><br>><br>> Regards<br>> Austin<br>><br>
> On Mon, Mar 28, 2011 at 4:20 PM, nan wich <<a href="mailto:nan_wich@bellsouth.net">nan_wich@bellsouth.net</a>> wrote:<br>> > You don't need to provide the value for an auto increment field.<br>> ><br>
> > "Db_last_insert_id()" is how you get the value *after* the insert, if you<br>> > need it at all.<br>> ><br>> ><br>> > *Nancy*<br>> ><br>> ><br>> ><br>> > Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L.<br>
> > King, Jr.<br>> ><br>> > ------------------------------<br>> > *From:* Austin Einter<br>> ><br>> > I have a table, where 'cid' field is of type "serial".<br>> ><br>
> > *$lastid = 0;<br>> ><br>> > for($delta = 0; $delta < $all_companies; $delta++)<br>> > {<br>> ><br>> > $lastid = db_last_insert_id(rs_companies, cid) + 1;<br>> ><br>> > db_query(<br>
> ><br>> > 'INSERT INTO {rs_companies} ( cid, uid, prevcompany, joindate,<br>> ><br>> > releasedate) '<br>> ><br>> > ."VALUES (%d, '%d', '%s', '%d', '%d')",<br>
> ><br></div></div>> > $lastid, *<br></blockquote></div><br>