MyBB Community Forums

Full Version: A new function for getting autoincrement value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
The downside with insert_id is that some insert queries have to ben run in order to get it.

I think it would be nice in each DB Class have a method for getting the Autoincrement value of a table.

I'm writing a plugin and now i think it supports MySQL only because i think that syntax for getting table info are different in other engines supported by MyBB.

function get_autoincrement($table)
{
	$query = $this->query("SHOW TABLE STATUS LIKE '{$db->table_prefix}posts'");
	$result = $this->fetch_array($query);
	return $result['Auto_increment'];
}
I did this before - it's not a good idea because of race conditions (and I got some data corruption through this method).
Doesn't MySQL handle properly Race conditions?

What do you think about checking if a row with that ID exists if not then it is ok?

Stupid example:
	//Ok i try to prevent race conditions (= many users writing the same amount of data at the same time)
	$success = false;
	while($success == false)
	{
		$query = $db->query("SHOW TABLE STATUS LIKE '{$db->table_prefix}posts'");
		$result = $db->fetch_array($query);
		$pid = $result['Auto_increment'];
		$query = $db->simple_select("posts", "pid", "pid='{$pid}'");
		if($db->num_rows($query) == 0)
			$success = true;
		else
			$success = false;
	}
(2010-04-04, 11:29 AM)flash.tato Wrote: [ -> ]Doesn't MySQL handle properly Race conditions?
MySQL does, but your PHP script won't.
Unless you use semaphores. Though in that case, you should be manually locking before your critical region and unlocking afterwards.

All in all, it's easier to do the insert and grab the id from there - you won't get a race condition then.
Ok for me it means core edits.

As the hooks are before the insertion in Database.

I already write the code for editing the file and if it doesn't success it stimolates the user to edit the file manually.

I hate code edits Sad but this time seems to be necessary.
What are you trying to do exactly?
I just thought a better approach also if it means sacrifice a feature that i would've want to see implemented in future but for huge forums i think it is needed to rethink the inner working.

anyway the hooks are the ones in Datahandler, the problem is getting the pid (before insertion in database).
So you're trying to do something when a post is inserted? Can't just run your query then update later?
I could but i should use another hook and i would handle editpost.php newreply.php newthread.php etc.

Ehy seriously i don't need it anymore i just rethought the system Smile instead of a table the data will be hold in posts column the data is an array converted to string with serialize Wink

Only that implementing searching would be a bit of nightmare as it is a lot of processing, i think that this approach is worth sacrifice this feature.

Also for big board it would be such a nightmare i guess
I don't really understand what you would use an autoincrement number for... it's not really safe to rely on it for anything...
Pages: 1 2