12

I am writing a module for Joomla, at this point I really need to be able to connect to the database using Jfactory. Normally one could simply use $db = JFactory::getDBO(); , but the PHP error tells me the JFactory class is not included. So now I need to know how to include this JFactory class. I've tried a couple of suggestions found on the internet, absent success, yet. This is the code (it works perfect on standalone)

    <?php
// server info
$server = 'localhost';
$user = 'ss';
$pass = 'oo';
$db = 'ss';

$connection = mysql_connect($server, $user, $pass)  or die ("Could not connect to server ... \n" . mysql_error ());
 mysql_select_db($db) or die ("Could not connect to database ... \n" . mysql_error ());


if(isSet($_POST['username']))
{
$username = $_POST['username'];
$username = mysql_real_escape_string($username);
$sql_check = mysql_query("SELECT Username FROM users WHERE Username='$username'");

if(mysql_num_rows($sql_check))
{
echo '<font color="#cc0000"><STRONG>'.$username.'</STRONG> is already in use.</font>';
}
else
{
echo 'OK';
}

}

?>

I hope my problem is clear to you. Your help will be much appreciated.

Attempt 1

<?php

include('../../../../configuration.php');

include('../../../../libraries/joomla/factory.php');

$config =& JFactory::getConfig();

// server info
$server2 = $host;
$user2 = $user;
$pass2 = $password;
$db2 = $db;

$connection = mysqli_connect($server2, $user2, $pass2)  or die ("Could not connect to server ... \n" . mysqli_error ());
 mysqli_select_db($db2) or die ("Could not connect to database ... \n" . mysqli_error ());


if(isSet($_POST['username']))
{
$username = $_POST['username'];
$username = mysql_real_escape_string($username);
$sql_check = mysql_query("SELECT username FROM #__users WHERE username='$username'");

if(mysql_num_rows($sql_check))
{
echo '<font color="#cc0000"><STRONG>'.$username.'</STRONG> is already in use.</font>';
}
else
{
echo 'OK';
}

}
?>

but this is not working either.

attempt 2 (successful)

include('../../../../configuration.php');
$jc = new JConfig();
$table = 'users';
$users = $jc->dbprefix . $table;
// connect to the database
$mysqli = new mysqli($jc->host, $jc->user, $jc->password, $jc->db);

Now it is all working as I want. The only thing is now: safety. I'm not too sure about this being hacker proof. Can someone review this? thanks :)

Maarten Hartman
  • 1,611
  • 6
  • 26
  • 45
  • Writing a module..? By the time Joomla calls your module the JFactory gotta be included already. Are you sure it's a Joomla Module you're writing (could be a component or plugin also). See [Creating a simple module](http://docs.joomla.org/Creating_a_simple_module). – Matteus Hemström Feb 27 '12 at 12:09
  • Hello, yes I'm writing a module. I expected exactly what you are saying, JFactory gotta be included already. But somehow it's not. The code above is being called by AJAX. When using the above code, everything is working as I want to. If I remove the standalone DB connection, there is just no connection..I edited and added the above code sou you can see my attempt. thanks :) – Maarten Hartman Feb 27 '12 at 13:48
  • 1
    You say the code above is called by AJAX. Exactly what URL are you using to call it, though? This will define whether you're writing a 'real' module (with all the joomla framework already included) or a standalone script that needs to do the framework setup itself. If your AJAX is requesting a URL like 'index.php?option=com_yourcomponent&view=....', it'll be the former. If you're requesting a URL like '/modules/mod_yourmodule/...' it'll be the latter. I'm guessing it probably *is* the latter, since you mention the word 'module'. But please confirm. – Bobby Jack Feb 27 '12 at 16:34
  • hey Bobby, I finally got it to work :) thanks for your contribution. If you want, can you review what I wrote at attempt 2? thanks! – Maarten Hartman Feb 27 '12 at 18:05

3 Answers3

30

I'm sure that you figure it out, but maybe it would be useful for someone else

To use joomla database class (even if you know that is not recommended :) ) you need, first to define three constants, like:

define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );

Then you need to include three files, like:

require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$mainframe =& JFactory::getApplication('site');

EDIT

You can include only two files like:

define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] ); // define JPATH_BASE on the external file
require_once( JPATH_BASE . DS . 'libraries' . DS . 'import.php' ); // framework
require_once( JPATH_BASE . DS . 'configuration.php' ); // config file

Finally use joomla class, like:

$db = JFactory::getDBO();
stef
  • 470
  • 3
  • 16
3

Recently, the libraries/joomla/factory.php has been removed, which was causing all the scripts that utilized require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' ); to fail. As this is still used in the accepted & most upvoted answer here, it's time to an update...

You don't need the JDatabaseFactory class anymore to use Joomla database functions through JFactory::getDBO(); as the JFactory class provides them and gets already included.

These five lines are enough:

define('_JEXEC', 1);
define('JPATH_BASE', dirname(__FILE__));
define('DS', DIRECTORY_SEPARATOR);
require_once JPATH_BASE.DS.'includes'.DS.'defines.php';
require_once JPATH_BASE.DS.'includes'.DS.'framework.php';

You will be able to do e.g. this again:

$db =& JFactory::getDBO();
$query = "SELECT ...";
$db->setQuery($query);
$db->query();
Esa Jokinen
  • 279
  • 3
  • 10
1

For accessing Database information of Joomla ,You should include("configuration.php").This joomla configuaraion file contains all information of database access.

class JFactory defined in this folder "joomlaRootFolder/libraries/joomla/factory.php"

yogeshK
  • 195
  • 4
  • 19
  • Hello, thanks for your answer. I added this to my file: include('../../../../configuration.php'); include('../../../../libraries/joomla/factory.php'); $db =& JFactory::getDBO(); the files are properly included but there is still no connection. – Maarten Hartman Feb 27 '12 at 13:24