Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
730 views
in Technique[技术] by (71.8m points)

mysql - include Jfactory class in an external php file, Joomla

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 ... 
" . mysql_error ());
 mysql_select_db($db) or die ("Could not connect to database ... 
" . 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 ... 
" . mysqli_error ());
 mysqli_select_db($db2) or die ("Could not connect to database ... 
" . 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 :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...