Re: Auto Increment Problems....
| From: | Joel Boonstra | Date: | Tue, 30 Jul 2002 20:05:32 +0000 |
| Subject: | Re: Auto Increment Problems.... | ||
| References: | 1 2 | Groups: | php.db php.general |
| Request: | Send a blank email to php-db+get-20909@lists.php.net to get a copy of this message | ||
[straying OT, but...]
> Instead of incrementing to find the next row to count them, you dont have to
> set the ID if it is auto increment. MySQL will do it for you (and i think it
> might fill the holes too). Also, to get the num. of rows just do this -
>
> $get_rows = mysql_query("SELECT * FROM
table");
> $num_rows = mysql_num_rows($get_rows);
Oh my, don't do that! If you only want to get the number of rows in a
table, don't bog down the MySQL server by doing a 'SELECT *' on it.
This could be a *ton* of data...
Why not use MySQL's built-in counting functionality:
$result = mysql_query('SELECT count(*) AS count FROM table');
$count_arr = mysql_fetch_assoc($result);
$count = $count_arr['count'];
MySQL will be able to simply count rows a lot faster than actually
SELECTing all of the data.
Of course, if you're counting rows just to find out what the next value
for an ID is, then, as you already said, you're doing the wrong thing.
Joel
--
[ joel boonstra | jboonstra@gospelcom.net ]