Forum Moderators: coopster

Message Too Old, No Replies

no mysql insert id() after $this->db->setQuery( "INSERT

         

carsten888

6:53 pm on Mar 14, 2008 (gmt 0)

10+ Year Member



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 insert/update/select, but no insert id any more.

make var:
var $db;

constructor:
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:
$this->db->setQuery( "INSERT INTO #__pi......

insert works.

so far so good.
but when this happens:
$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?

turbosaab

11:46 pm on Mar 14, 2008 (gmt 0)

10+ Year Member



Funny, I just ran into this same problem. I swear this code used to work until my host switched something recently? Anyway, change to:
$id = mysql_insert_id($database);

carsten888

8:55 am on Mar 15, 2008 (gmt 0)

10+ Year Member



I tried that but got a error.
for:
$id = mysql_insert_id($database);
and:
$id = mysql_insert_id($this->db);
and
$temp_db = $this->db;
$id = mysql_insert_id($temp_db);

I get this error when I put anything in between those bracets:

Warning: mysql_insert_id(): supplied argument is not a valid MySQL-Link resource in

turbosaab

2:07 pm on Mar 15, 2008 (gmt 0)

10+ Year Member



Are you testing using 1.5 or 1.0? (or same errors for both?) I think to get the id from JFactory you are going to have to do something like:
$id = $this->db->insertid();

carsten888

4:49 pm on Mar 15, 2008 (gmt 0)

10+ Year Member



Yes! thats it!

that works on Joomla 1.5 as well as on Joomla 1.0.x!
That's realy surprising it works on 1.0.x as well, cos that has no JFactory, so maybe thats some function that already existed. I tried to search for it in the 'development' section of the website, but (as usual) can't find it for Joomla 1.0.x there.

Thank you ever so much, that was the only thing that kept my components code running on both 1.0.x as well as 1.5!

thank you!

turbosaab

5:31 pm on Mar 15, 2008 (gmt 0)

10+ Year Member



You're welcome :-)