Can't Query External Database with Joomla

EMB01
EMB01 used Ask the Experts™
on
I'm using the following code to delete records from a database; however, it always echos false...  I can't figure out what is going wrong.  The query works fine on it's own, so it must be my implementation.   I'm on joomla 1.5.
$db =&  JDatabase::getinstance($options1);
		$this->setDBO($db);
		if ($db->query($query)) {
		echo "false";
		} else {
		echo "true";
		}
		die;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Is it actually deleting the record?

I'm not used to this classes, but it seems to me like $db->query($query) would return 'true' upon a successful command and false on a failure. If this is the case your if statement if reversed or you should be looking for if(!$db->query($query))

Author

Commented:
Thanks, you're right; it does say true.  However, it doesn't actually delete any records when I look at the database.  Why is it saying true but not executing the query?

Author

Commented:
Latest event:  When I run the following code, the query returns true but the SETDBO method returns false...


function check_it($function, $whatisit) {
	
		if ($function) {
		echo "the $whatisit function returns TRUE<br />";
		} else {
		echo "the $whatisit function returns FALSE<br />";
		}
	
	}

	function deleteTr($table, $id)
	{
		$options1 = // creds
		$db =&  JDatabase::getinstance($options1);
		$this->check_it($this->setDBO($db), "this->setdbo(db)");
		$query = "delete from `translation` WHERE ORIGTABLE = 'catalog' and TABLEID = 1";
		$this->check_it($db->query($query), "db->query");
		die;
	}

Open in new window

Author

Commented:
Latest event:  I changed the setdbo to getdbo and now, both methods return true.  However, no data gets deleted... I'm trying to get the delete query to run and delete the corresponding data in the database.
function check_it($function, $whatisit) {
	
		if ($function) {
		echo "the $whatisit function returns TRUE<br />";
		} else {
		echo "the $whatisit function returns FALSE<br />";
		}
	
	}

	function deleteTr($table, $id)
	{
		$options1 = // creds
		$db =&  JDatabase::getinstance($options1);
		$this->check_it($this->getDBO($db), "this->setdbo(db)");
		$query = "delete from `translation` WHERE ORIGTABLE = 'catalog' and TABLEID = 1";
		$this->check_it($db->query($query), "db->query");
		die;
	}

Open in new window

Try changing your query to a select statement and echoing the data. this will verify that you are in fact able to create a valid connection to the external database.

Is the external database on the same web server? or are you trying to connect to a database on a different server (possibly through a firewall)

After that we'll have to verify that the apache user has the rights (permissions) to delete records.
Commented:
Turns out, I had to use setquery to get it to work.  I didn't understand before how joomlas functions worked...
$db =&  JDatabase::getinstance($options1);
		$query = "delete from `translation` WHERE ORIGTABLE = '".$table."' and TABLEID = ".$id;
		$db->setQuery($query);
		$db->query();

Open in new window

Author

Commented:
I found the solution myself.  Thanks for trying, though.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial