Jump to content

Php/MySql reading last IDs


Asheeown

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/101126-phpmysql-reading-last-ids/
Share on other sites

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.