Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7850263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:47:39+00:00 2026-06-02T18:47:39+00:00

I am writing a module for Joomla, at this point I really need to

  • 0

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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:47:39+00:00Added an answer on June 2, 2026 at 6:47 pm

    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');
    

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Joomla 2.5.3 module. I'm trying to get an article from a
I'm writing a module that has several methods. Let's consider this : package MyPackage;
I am writing a Perl module, and I am using carp to throw a
I am writing an Apache module for my internship. I am using C for
I am not really familiar with Joomla but I have been tasked with writing
I am starting out with Joomla and am writing a simple Joomla module. I
I am writing a module that provides one function and needs an initialization step,
Quick background: writing a module. One of my objects has methods that may or
I am writing an integration module for our product to go into any Joomla
I'm writing a module in Drupal-7 that dynamically sends a one-time login link to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.