MyBB Community Forums

Full Version: $db->insert_id()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Any ideas why $db->insert_id() always returns 0 on my forums? I have to edit the code to make it do something like:
//$tid = $db->insert_id();
$query = $db->query("SELECT tid FROM ".TABLE_PREFIX."threads WHERE fid='$fid' AND subject='$subject' AND dateline='$now'");
$cheese = $db->fetch_array($query);
$tid = $cheese['tid'];

Could it be because of some settings on my server or something like that? It's not just with 1.04 either, I've had to modify the code since about 1.0PR1 or when I moved to this server I'm on now.
$db->insert_id(), like mysql_insert_id, should be placed after the query that you want the id from (basically after the query, and before the next query).
Yea it is, haven't modified the code, it just doesn't seem to work on my server, that's why I had to modify the code to what it is above, just wondering if anyone knew why the original code doesn't work for me?
oh pfft I'm stupid. $db->insert_id only is returned after an INSERT query, not a SELECT query (that's why it's called insert_id and not just any_id)
yea I know, but the line that it's after is an INSERT line, for example in newthread.php where I took the code from the first post from. It where the thread is inserted into the threads table.

		$plugins->run_hooks("newthread_do_newthread_process");

		$db->insert_query(TABLE_PREFIX."threads", $newthread);
		$tid = $db->insert_id();

$db->insert_id(); return 0, as after posting the new thread, I am redirected to 'http://www.mpsounds.net/forums/showthread.php?tid=0'
If you replace $db->insert_id() with mysql_insert_id(), does it work?
Also, which versions of PHP & MySQL are you using?

Chris
DennisTT Wrote:If you replace $db->insert_id() with mysql_insert_id(), does it work?
Still returns 0 and I'm redirected to 'showthread.php?tid=0'

Chris Boulton Wrote:Also, which versions of PHP & MySQL are you using?
PHP Version 4.3.11
MySQL Version 4.1.14
Can you open up db_mysql.php and find:

	function insert_id()
	{
		$id = mysql_insert_id();
		return $id;
	}

Replace it with:
	function insert_id()
	{
		$id = mysql_insert_id($this->link);
		return $id;
	}

Let me know then.

Chris
aha fixed it, thanks Chris, its cos I have my other DB on the pages too, never thought of that, thanks Big Grin