That I'm trying to do here is get the VALUE of hit_id from the last row of
hits. It is an auto-increment number, but it is much higher than the count
of the rows, because people go through and delete their hits from time to
time. Am I correct in assuming that auto-numbers are never re-used?
Walter
On 7/20/01 7:06 PM, "Joshua J. Kugler" <[EMAIL PROTECTED]> wrote:
> Read the manual again on last_insert_id(). That function only gives you the
> unique id of the last insert *for that thread* If you connect, don't do any
> inserts, and select last_insert_id, there is no guarentee what you will get.
> If you are trying to find the number of rows, it would be much easier to do
> this:
>
> SELECT COUNT(*) AS pixels FROM hits
>
> Try that. From the looks of what you are doing, that will be accurate every
> time. COUNT(*) knows how many rows there are in a table. last_insert_id()
> has nothing to do with how many rows are in a table.
>
> If you are going to delete old rows one day, then you might want to do this:
>
> SELECT MAX(id) as pixels FROM hits
>
> Hope that helps.
>
> On Friday 20 July 2001 13:31, Walter Lee Davis wrote:
>> Currently, I have been counting the rows in PHP in order to get the value
>> of the last hit_id in the database like this:
>>
>> $sql4 = "SELECT hits.hit_id from hits";
>>
>> $result = mysql_query ($sql4, $connection)
>> or die("error #" . mysql_errno() . ": " . mysql_error());
>> $last = mysql_num_rows($result) - 1;
>> $go = mysql_data_seek($result, $last);
>> $row = mysql_fetch_object($result);
>> $pixels = number_format($row->hit_id);
>>
>> Which is really expensive on my very tall hits table. I hit upon doing it
>> in MySQL, which is much, much faster:
>>
>> $sql4 = "SELECT hits.hit_id
>> as pixels
>> from hits
>> WHERE hit_id=LAST_INSERT_ID();";
>>
>> $result = mysql_query ($sql4, $connection)
>> or die("error #" . mysql_errno() . ": " . mysql_error());
>> $row = mysql_fetch_object($result);
>> $pixels = number_format($row->pixels);
>>
>> It's staggering the difference in speed. But if I sit there and hit refresh
>> on the browser, I get wildly different values for $pixels. It jumps around
>> the actual number by +-10 or 12. The first method is precisely the same
>> every time (unless a hit is recorded in the interim). Am I doing something
>> wrong here? Can I not count on MySQL to know how many rows it has recorded
>> in a table? Any idea why that select statement would select a different row
>> each time?
---------------------------------------------------------------------
Before posting, please check:
http://www.mysql.com/manual.php (the manual)
http://lists.mysql.com/ (the list archive)
To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php