PDA

View Full Version : Counting IDs from database?


jarv
07-11-2008, 12:55 PM
How do i count to the end of records in my database and add 1?

<?
$HeaderCount = mysql_query("SELECT MAX(HeaderID)+1 AS HeaderCount FROM Headertbl");


print ("$HeaderCount");
?>


result = Resource id #7

there are 22 records in my database??!

please help

abduraooft
07-11-2008, 01:21 PM
You need to fetch the result from the mysql_query result.

jarv
07-11-2008, 01:57 PM
ok so now i have:

$HeaderID = mysql_fetch_array(mysql_query("SELECT MAX(HeaderID)+1 AS HeaderCount FROM Headertbl"));

and $HeaderID outputs: Array

abduraooft
07-11-2008, 02:04 PM
because it's an array! mysql_fetch_array() (http://php.net/mysql_fetch_array) fetches an array!
echo $HeaderID['HeaderCount'];

CFMaBiSmAd
07-11-2008, 02:49 PM
jarv, when using a programming language, it is part of your job as a programmer to find out what each function does and what values are returned. The php language reference manual contains a section for each php function.

mysql_query -

Return Values
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.

mysql_fetch_array -

Return Values
Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows. The type of returned array depends on how result_type is defined. By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).

If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you must use the numeric index of the column or make an alias for the column. For aliased columns, you cannot access the contents with the original column name.

When you don't use the language reference manual you might as well be trying to drive a car when you are blind. You might get to where you want to go, but it will take you a very long time and you will have a lot of accidents along the way. It will take you a long time to produce any code that accomplishes anything useful.

Ref: http://www.php.net/docs.php

malfist
07-11-2008, 03:29 PM
Do you want the highest id or the number of rows?
For number of rows you should use SELECT count(column) FROM row

It looks like you are doing auto-incrementing, google mysql auto-increment.