<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 &lt; $all_companies; $delta++)<br>  {<br>    drupal_set_message(t(&#39;delta = &#39;).$delta.t(&#39;last id = &#39;).db_last_insert_id(rs_companies, cid));<br>      db_query(<br>    &#39;INSERT INTO {rs_companies} (uid, prevcompany, joindate, releasedate) &#39;<br>
      .&quot;VALUES (&#39;%d&#39;, &#39;%s&#39;, &#39;%d&#39;, &#39;%d&#39;)&quot;, <br>    $user-&gt;uid, <br>    $form_state[&#39;values&#39;][&#39;allcompanies&#39;][$delta][&#39;companies&#39;],<br> $form_state[&#39;values&#39;][&#39;allcompanies&#39;][$delta][&#39;startdate&#39;],<br>
 $form_state[&#39;values&#39;][&#39;allcompanies&#39;][$delta][&#39;enddate&#39;]<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">&lt;<a href="mailto:larry@garfieldtech.com">larry@garfieldtech.com</a>&gt;</span> wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">Yep, you&#39;re doing it backwards, actually. :-)<br><br>In D6:<br><br>db_query(&quot;INSERT INTO {foo} (a, b) VALUES (%d, &#39;%s&#39;)&quot;, 1, &#39;hi&#39;);<br>
$cid = db_last_insert_id(); // The DB auto-generated this value.<br><br>In D7:<br><br>$cid = db_insert()<br> -&gt;fields(array(<br>   &#39;a&#39; =&gt; 1,<br>   &#39;b&#39; =&gt; &#39;hi&#39;<br> ))<br> -&gt;execute();<br>
// db_insert()-&gt;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(&quot;SELECT COUNT(*) FROM {foo}&quot;)-&gt;fetchField();<br><br>$max_cid = db_query(&quot;SELECT MAX(cid) FROM {foo}&quot;)-&gt;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>&gt; Thanks Nancy.<br>&gt; Do you mean that in my code I should not put the value for &quot;cid&quot;. That<br>&gt; means $lastid (in red) should be removed.<br>
&gt;<br>&gt;<br>&gt;  *$lastid = 0;<br>&gt;   for($delta = 0; $delta &lt; $all_companies; $delta++)<br>&gt;   {<br>&gt;  $lastid = db_last_insert_id(rs_companies, cid) + 1;<br>&gt;       db_query(<br>&gt;     &#39;INSERT INTO {rs_companies} ( cid, uid, prevcompany, joindate,<br>
&gt; releasedate) &#39;<br>&gt;       .&quot;VALUES (%d, &#39;%d&#39;, &#39;%s&#39;, &#39;%d&#39;, &#39;%d&#39;)&quot;,<br>&gt;  $lastid,  *<br>&gt; *$user-&gt;uid*<br>&gt;<br>&gt;<br>&gt; Regards<br>&gt; Austin<br>&gt;<br>
&gt; On Mon, Mar 28, 2011 at 4:20 PM, nan wich &lt;<a href="mailto:nan_wich@bellsouth.net">nan_wich@bellsouth.net</a>&gt; wrote:<br>&gt; &gt;   You don&#39;t need to provide the value for an auto increment field.<br>&gt; &gt;<br>
&gt; &gt; &quot;Db_last_insert_id()&quot; is how you get the value *after* the insert, if you<br>&gt; &gt; need it at all.<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt; *Nancy*<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt; Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L.<br>
&gt; &gt; King, Jr.<br>&gt; &gt;<br>&gt; &gt;  ------------------------------<br>&gt; &gt;  *From:* Austin Einter<br>&gt; &gt;<br>&gt; &gt;  I have a table, where &#39;cid&#39; field is of type &quot;serial&quot;.<br>&gt; &gt;<br>
&gt; &gt;  *$lastid = 0;<br>&gt; &gt;<br>&gt; &gt;   for($delta = 0; $delta &lt; $all_companies; $delta++)<br>&gt; &gt;   {<br>&gt; &gt;<br>&gt; &gt;  $lastid = db_last_insert_id(rs_companies, cid) + 1;<br>&gt; &gt;<br>&gt; &gt;       db_query(<br>
&gt; &gt;<br>&gt; &gt;     &#39;INSERT INTO {rs_companies} ( cid, uid, prevcompany, joindate,<br>&gt; &gt;<br>&gt; &gt; releasedate) &#39;<br>&gt; &gt;<br>&gt; &gt;       .&quot;VALUES (%d, &#39;%d&#39;, &#39;%s&#39;, &#39;%d&#39;, &#39;%d&#39;)&quot;,<br>
&gt; &gt;<br></div></div>&gt; &gt;  $lastid,  *<br></blockquote></div><br>