• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

find highest id, and add one for the database query

iBrightDev

Jay Street
NLC
NLC
ok, i ended up having to do a little off coding to get my cms menu, content and other stuff to insert in the DB properly. i use 'id' and 'display' in the menu table because display is what i sort by, and i give the ability to re-organize the nav for the CMS, so, i felt it was better to have a regular ID and a display id.

so, the problem i was running into was that i was just counting the rows and trying to add 1 to the number of rows returned. not good if you start removing entries from the database because then you would end up with duplicate display id's, and things would start getting messed up.

so, i thought i would share my solution in case anyone else was looking to do something similar.

PHP:
// Get the curent display ID number
$SQL = "SELECT * FROM menu"; // Select the table to query
$result = mysql_query($SQL); // Run a standard query
$numRows = mysql_num_rows($result); // Count the number of rows returned
$posNums = array(); // build an array for the results we are going to return
				
for ($i = 0; $i < $numRows; $i++) { // loop through the results found starting at the first column
				
	$row = mysql_fetch_row($result); // determine where column in the table that we want to loop through
	$pID = $row[1]; // we want the display id, not the regular id, so, we go with row 1
					
	$posNums[$i] = $pID; // store all the array values in the array found in $pID
				
}
				
sort($posNums); // Sort the array results with the smallest number first in case results are out of order
$lastID = end($posNums); // find the last number after the array was sorted
$lastID++; // add one more number to the highest number found
$display .= $lastID; // once the new number was created, store it as a variable to be used later

so, there you have it, now,if i call $display, it will find whatever the highest number is found in the DB, and add 1 to it.

example...

display[]
1
2
5
6
9
15
25
13
14
12
88
55
23
69
32

after sorted, it would look like...

display[]
1
2
5
6
9
12
13
14
15
23
25
32
55
69
88

therefore determining that 88 is the highest number found, and since we add one to it, the $display variable would become 89. now we have a good display id to work with and there should not be any more duplicate display id's messing up the CMS navigation. :D

hope this is useful to someone else besides me.
 
There are much simpler ways to accomplish what you needed. It's much quicker and reliable to give `id` the AUTO_INCREMENT attribute within MySQL and set id to NULL when inserting. If for some reason that's not an option, using this SQL:
Code:
SELECT MAX(id)+1 AS 'new_id' FROM table
will return what you need.

While your way works, it jumps through some unnecessary hoops. You'd start running into serious performance problems at high loads. SELECTing * from a table multiple times, especially if the table is large, is expensive.
 
There are much simpler ways to accomplish what you needed. It's much quicker and reliable to give `id` the AUTO_INCREMENT attribute within MySQL and set id to NULL when inserting. If for some reason that's not an option, using this SQL:
Code:
SELECT MAX(id)+1 AS 'new_id' FROM table
will return what you need.

While your way works, it jumps through some unnecessary hoops. You'd start running into serious performance problems at high loads. SELECTing * from a table multiple times, especially if the table is large, is expensive.

did not realize you could do it that way since this was all i found. http://us2.php.net/max :( i will give that a try Trevor. thanks. :D
 
ok, i did this...
PHP:
$SQL = "SELECT MAX(display)+1 FROM menu";
$result = mysql_query($SQL); // Run a standard query
$row = mysql_fetch_array($result);
$display = max($row);
seems to be working. thanks Trevor.
 
Back
Top