Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 02-17-2008, 07:24 PM Counting rows
Sharon_leic's Avatar
Super Talker

Posts: 115
Name: Sharon
Location: Leicester, uk
Trades: 0
$query = "SELECT COUNT(*) as row_count FROM profiles";
$result = mysql_query($query) or die(mysql_error());
$total = mysql_fetch_object($result);
$counter = $total->row_count;

The code above will tell me how many rows there are that ain't been deleted

but how do i find out how many rows there are altogether?

in admin php it says next auto increment number is 18..
but the above script it returning 6 because 12 rows have been deleted

so how do i get the script to say 18 ?

thanks

sharon
__________________
mysql_connect("localhost", "brain", "sharon") or die(mysql_error());
mysql error: brain doesn't exist!
Sharon_leic is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-18-2008, 02:21 AM Re: Counting rows
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
What do you want to get?

Currently you are selecting the number of rows in the table. If you need the next autoincrement value (the value which will be assigned to the autoincrement field of record during next insert statement) you should select max(autoincrement_field)+1 instead.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 02-18-2008, 05:08 AM Re: Counting rows
Sharon_leic's Avatar
Super Talker

Posts: 115
Name: Sharon
Location: Leicester, uk
Trades: 0
Quote:
Originally Posted by mtishetsky View Post
What do you want to get?

Currently you are selecting the number of rows in the table. If you need the next autoincrement value (the value which will be assigned to the autoincrement field of record during next insert statement) you should select max(autoincrement_field)+1 instead.
Thanks

that worked perfect

$query = "SELECT MAX(ID) as row_count FROM albums";
$result = mysql_query($query) or die(mysql_error());
$total = mysql_fetch_object($result);
$counter = $total->row_count;

Shaz x
__________________
mysql_connect("localhost", "brain", "sharon") or die(mysql_error());
mysql error: brain doesn't exist!
Sharon_leic is offline
Reply With Quote
View Public Profile
 
Old 02-18-2008, 11:35 PM Re: Counting rows
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
But, as you say, if there are deleted records, the number you expect would not be right.

Code:
SHOW TABLE STATUS LIKE 'table_name'
Would return:
Code:
+----------------+
| Auto_increment |
+----------------+
|       99       |
+----------------+
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 02-19-2008, 12:42 AM Re: Counting rows
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Dammit, I missed this case.

Sharon, why do you need the next value for auto_increment field? Isn't it easier to get the id of last inserted record with mysql_insert_id() ?
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 02-19-2008, 04:49 AM Re: Counting rows
Sharon_leic's Avatar
Super Talker

Posts: 115
Name: Sharon
Location: Leicester, uk
Trades: 0
Hiya

i needed it because i am writing a site and needed to show the last 6 entries for something going backwards from the last entry

but now i just found out there is

$result = mysql_query("SELECT * FROM this WHERE that = 1 ORDER BY the other DESC") or die(mysql_error());
while($riw = mysql_fetch_array( $result )) {do thisandthat}

lol much quicker and less messy lol

before i wrote a load of lines of code when i could just as easy done the above..

i am a php newbie so learning still
__________________
mysql_connect("localhost", "brain", "sharon") or die(mysql_error());
mysql error: brain doesn't exist!
Sharon_leic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Counting rows
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17672 seconds with 12 queries