Page 1 of 1

no mysql_insert_id() after $this->db->setQuery( "INSERT

Posted: Thu Mar 13, 2008 2:59 pm
by carsten888
To make my components work in both Joomla 1.5 and older versions I made in the constructor of my class the $db into a var. All works except for when I make an insert, because I can't get the id any more.

make the var:

Code: Select all

var $db;
constructor:

Code: Select all

function class_pi(){
		//constructor		
		global $database;		
		
		//get database
		if( defined('_JEXEC') ){
			//joomla 1.5
			$this->db = JFactory::getDBO();
		}else{
			//joomla 1.0.x
			$this->db = $database;
		}	
}
use in class:

Code: Select all

$this->db->setQuery( "INSERT INTO #__pi......
so far so good.
but when this happens:

Code: Select all

$id = mysql_insert_id();
echo $id;exit;	
it outputs '0'.

This used to work, but not since I call the database in this new way.

anyone?

Re: no mysql_insert_id() after $this->db->setQuery( "INSERT

Posted: Thu Mar 13, 2008 9:10 pm
by GMassi
Judging by the code you have posted you are not executing the query. Try

Code: Select all

$this->db->setQuery( "INSERT INTO #__pi......
$this->db->query(); 
Then mysql_insert_id() should work.

Re: no mysql_insert_id() after $this->db->setQuery( "INSERT

Posted: Fri Mar 14, 2008 6:57 am
by carsten888
Judging by the code you have posted you are not executing the query.
sorry i didn't include that line, but as i said it all works.
insert/update/select all work fine like this excvept for mysql_insert_id()

Re: no mysql_insert_id() after $this->db->setQuery( "INSERT

Posted: Sat Mar 15, 2008 6:32 pm
by carsten888
solved! thanks to turbosaab.

Code: Select all

$id = $this->db->insertid(); 
works on Joomla 1.5 as well as on Joomla 1.0.x!


this was the only thing that kept my components from code running on both 1.0.x as well as 1.5!