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?

-- 
Joshua Kugler, Information Services Director
Associated Students of the University of Alaska Fairbanks
[EMAIL PROTECTED], 907-474-7601

---------------------------------------------------------------------
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

Reply via email to