Asheeown Posted April 14, 2008 Share Posted April 14, 2008 Okay, so I am making a script for an emulated gaming server which uses a standard MySQL server for all it's data. Now this is a mailer system and this is going to create and send items. What it does: 1. Gathers a comprehensive list of everyone in the game it's going to send to (DONE) 2. Inserts a new item into the database (DONE) 3. Inserts a record into the mail table with the item ID added in step 2 and the UserID from step 1 (Partially Done) Now all the coding is done except for ONE thing on step 3 The ID field in the mail table for step 3 is not auto_increment and the structure cannot be changed, what I need it to do is pull the absolutely last (biggest) ID number in the mail table and increase it by one and if no records exist make the ID 0 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Dynasty Mailer</title> </head> <body> <?php $Host = "localhost"; $User = "xxxxxx"; $Password = "xxxxxx"; $conn = mysql_connect('localhost', $User, $Password); $CharacterDB = "fun_characters"; $ItemID = "100101"; $Subject = "A Gift From Dynasty!"; $Body = "This is the message sent for the user."; mysql_select_db($CharacterDB); ?> <?php $CharacterQuery = mysql_query("SELECT * FROM characters"); while($Characters = mysql_fetch_assoc($CharacterQuery)) { extract($Characters); // Create Item $Insert = mysql_query("INSERT INTO playeritems (ownerguid, entry, randomsuffix, enchantments) VALUES('0', '$ItemID', '', '')"); $Item = mysql_insert_id(); // Mail Item if(!$message_id) { $MailCount = mysql_query("SELECT message_id FROM mailbox ORDER BY message_id desc"); if(mysql_num_rows($MailCount) == 0) { $message_id=0; } else { $MailCount = mysql_fetch_assoc($MailCount); extract($MailCount); echo("<center><strong>Item: $ItemID mailed to $guid.</strong></center>"); $message_id = $message_id+1; } } else { $message_id++; echo("<center><strong>Item: $ItemID mailed to $guid.</strong></center>"); } $Mail = mysql_query("INSERT INTO mailbox (message_id, player_guid, sender_guid, subject, body, attached_item_guids) VALUES ('$message_id', '$guid', '1', '$Subject', '$Body', '$Item')") or die(mysql_error()) ; } // End of Characters ?> </body> </html> Okay right now under the Mail Item comment I have no idea where I was going with this, everything else is fine just getting $message_id from what is currently in the database "message_id" is the column in which the ID is put $guid = User ID $Item = Item ID Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted April 15, 2008 Share Posted April 15, 2008 I have no idea what all the rambling was about, but I think you want mysql_insert_id... http://www.php.net/mysql_insert_id Quote Link to comment Share on other sites More sharing options...
Asheeown Posted April 16, 2008 Author Share Posted April 16, 2008 No, that's completely different. Alright, how do you get the last (highest) record of an ID column if they're all intergers. Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted April 18, 2008 Share Posted April 18, 2008 http://www.mysql.com/max Quote Link to comment Share on other sites More sharing options...
haku Posted April 19, 2008 Share Posted April 19, 2008 $top_id = mysql_query("SELECT id FROM mail_table ORDER BY id DESC LIMIT 1"); $next_id = (mysql_num_rows($top_id) > 0) ? mysql_result($top_id, 0, 0) + 1 : 0; Quote Link to comment Share on other sites More sharing options...
stuffradio Posted April 19, 2008 Share Posted April 19, 2008 Try $top_id = mysql_fetch_array(mysql_query("SELECT id FROM mail_table ORDER BY id DESC LIMIT 1")); echo "$top_id[id] is the last fetched id selected!"; Quote Link to comment Share on other sites More sharing options...
haku Posted April 19, 2008 Share Posted April 19, 2008 A) the mysql query you gave was exactly the same as mine and didn't add anything. B) your code didn't provide the full solution to his question. Quote Link to comment Share on other sites More sharing options...
stuffradio Posted April 19, 2008 Share Posted April 19, 2008 A) the mysql query you gave was exactly the same as mine and didn't add anything. B) your code didn't provide the full solution to his question.[/Quote] A) I added a mysql_fetch_array... this allows you to fetch the value in one variable. B) It does provide the full solution because I showed how to display the id. Quote Link to comment Share on other sites More sharing options...
haku Posted April 19, 2008 Share Posted April 19, 2008 I added a mysql_fetch_array... this allows you to fetch the value in one variable. I think you need to look at mysql_result It does provide the full solution because I showed how to display the id. Look at his original question: what I need it to do is pull the absolutely last (biggest) ID number in the mail table and increase it by one and if no records exist make the ID 0 I think you will find that the code I gave does all of the above. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.