Page 1 of 1

max(id) counting - can someone explain

Posted: Thu Mar 15, 2007 7:56 pm
by crazytopu
Can anybody tell me why it is printing 29 where as the max id is 28 ?

Also, when there is no entry in the table, it prints 2, I run the sql query and in the phpmyadmin it shows null. Any idea? Apparently, if there is no entry I expected it to return 0.

I am just trying to find out the highest id no in the table.

Code: Select all

$query= "SELECT max(id) AS total FROM request";




$result = $connector->query($query);
$num = mysql_num_rows($result);




if($num>0) {

while($row=$connector->fetchArray($result)) {

		$total = $row['total'];

}

}


echo  $total;

Posted: Thu Mar 15, 2007 7:59 pm
by volka
try

Code: Select all

$query= "SELECT max(id) AS total FROM request";
$result = $connector->query($query);
$row=$connector->fetchArray($result);
echo 'max: ', $row['total'], "<br />\n";

$query= "SELECT count(*) AS total FROM request";
$result = $connector->query($query);
$row=$connector->fetchArray($result);
echo 'count: ', $row['total'], "<br />\n";


$query= "SELECT * FROM request ORDER BY id DESC LIMIT 10";
$result = $connector->query($query);
while( $row=$connector->fetchArray($result) ) {
	echo htmlentities(join(',', $row)), "<br />\n";
}
and post the output.

Posted: Thu Mar 15, 2007 8:05 pm
by crazytopu
thanks, here is the output






Code: Select all

max: 29
count: 3
29,29,Q20000,Q20000,y,y,fdasd,fdasd,fasdf,fasdf,2007-03-15,2007-03-15,2007-12-28,2007-12-28,1,1,0,0
28,28,Q20000,Q20000,ok,ok,we are looking for genuine windows office supplier,we are looking for genuine windows office supplier,We are a small company and need windows office for 100 systems immedately. We are only looking for quote from the authorised dealer. Thanks.,We are a small company and need windows office for 100 systems immedately. We are only looking for quote from the authorised dealer. Thanks.,2007-03-15,2007-03-15,2007-04-28,2007-04-28,1,1,0,0
12,12,Q20000,Q20000,chi,chi,ki go bole meyeta,ki go bole meyeta,ahare,ahare,2007-03-14,2007-03-14,2007-03-27,2007-03-27,1,1,0,0

Posted: Thu Mar 15, 2007 8:08 pm
by volka
crazytopu wrote:max: 29
count: 3
29,29,Q20000,Q20000,y,y,fdasd,fdasd,fasdf,fasdf,2007-03-15,2007-03-15,2007-12-28,2007-12-28,1,1,0,0
28,28,Q20000,Q20000,ok,ok,we are looking for genuine windows office supplier,we are looking for genuine windows office supplier,We are a small company and need windows office for 100 systems immedately. We are only looking for quote from the authorised dealer. Thanks.,We are a small company and need windows office for 100 systems immedately. We are only looking for quote from the authorised dealer. Thanks.,2007-03-15,2007-03-15,2007-04-28,2007-04-28,1,1,0,0
12,12,Q20000,Q20000,chi,chi,ki go bole meyeta,ki go bole meyeta,ahare,ahare,2007-03-14,2007-03-14,2007-03-27,2007-03-27,1,1,0,0
I guess the first field is `id`and max(id)=29 is correct.