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

Have a programming question regarding your component, plug-in, extension or core hacks? Have an interesting tidbit, FAQ or programming tip you’d like to share? This is the place for you.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Post Reply
User avatar
carsten888
Joomla! Apprentice
Joomla! Apprentice
Posts: 27
Joined: Sat Feb 11, 2006 8:32 am
Location: Tilburg, Holland
Contact:

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

Post by carsten888 » Thu Mar 13, 2008 2:59 pm

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?
Last edited by carsten888 on Sun Mar 16, 2008 8:46 am, edited 1 time in total.

GMassi
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Fri Jun 23, 2006 1:45 pm

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

Post by GMassi » Thu Mar 13, 2008 9:10 pm

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.

User avatar
carsten888
Joomla! Apprentice
Joomla! Apprentice
Posts: 27
Joined: Sat Feb 11, 2006 8:32 am
Location: Tilburg, Holland
Contact:

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

Post by carsten888 » Fri Mar 14, 2008 6:57 am

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()

User avatar
carsten888
Joomla! Apprentice
Joomla! Apprentice
Posts: 27
Joined: Sat Feb 11, 2006 8:32 am
Location: Tilburg, Holland
Contact:

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

Post by carsten888 » Sat Mar 15, 2008 6:32 pm

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!


Post Reply