[Solved]Fetching 'total' returns 123 instead of 3

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

[Solved]Fetching 'total' returns 123 instead of 3

Post by Steveo31 »

I'm back again ;)

I have a DB set up with tables with 'name' and 'total'. 'total' is an auto_increment primary key. $location is a var of the name of the table to retrieve from. Here's my code to retrieve it:

Code: Select all

<?php

$getID = 'SELECT total FROM '.$location;
$getID_q = mysql_query($getID, $db);
while($row = mysql_fetch_assoc($getID_q)){
    echo $row['total'];
}
And this echoes all the values. Say I have entered 5 values in name... this would make the auto_increment value (total) be 5. No prob, but the above returns 12345 instead of 5. I think I need to use a WHERE in the original statement, but I'm not 100% sure on how.

I checked the mySQL manual but to no avail.
Last edited by Steveo31 on Fri Mar 26, 2004 6:54 pm, edited 1 time in total.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

if you want to count the rows try this, (the auto_increment number is not the best way to keep track of how many rows you have)

Code: Select all

$total = mysql_num_rows($getID);
... or is it something different that you want?
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Perfect. Thank you!
Post Reply