ElkArte Community

Extending Elk => Addons => Addons ideas and questions => Topic started by: wintstar on September 05, 2015, 06:04:41 am

Title: $db_connection
Post by: wintstar on September 05, 2015, 06:04:41 am
I want to convert my mod SMF Ajax Chat (http://experiri.scaricare.de/index.php?topic=3.0) to ElkArte. I do not get a database connection:
Error-Report: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) Error-Code: 2002 (http://localhost/elk104/index.php?action=ajac)
ErroNotice of xampp localhost
Error-Report: No database selected Error-Code: 1046

require_once(dirname(AJAX_CHAT_PATH) . '/SSI.php');
Code: [Select]
$db_connection
In Elkarte SSI.php "global $db_connection" was removed. Through what it has been replaced?

Title: Re: $db_connection
Post by: Spuds on September 05, 2015, 06:49:38 am
I think you should be able to do a
Code: [Select]
$db = database();
then use
Code: [Select]
 $db->connection();
Title: Re: $db_connection
Post by: wintstar on September 05, 2015, 07:08:47 am
 :-*  not work

Notice: Undefined variable: db in C:\xampp\htdocs\elk104\Ajac\lib\class\CustomAJAXChat.php on line 19

Fatal error: Call to a member function database() on null in C:\xampp\htdocs\elk104\Ajac\lib\class\CustomAJAXChat.php on line 19

Code: [Select]
class CustomAJAXChat extends AJAXChat {

    // Initialize custom configuration settings
    function initCustomConfig() {
        global $db_name,$db_connection;
       
         $db->database();
       
        // Use the existing ElkArte database connection:
        $this->setConfig('dbConnection', 'link', $db_connection);
    }

    // Override the database connection method to make sure the ElkArte database is selected:
    function initDataBaseConnection() {
        global $db_name;

        // Call the parent method to initialize the database connection:
        parent::initDataBaseConnection();

        // Select the ElkArte database:
        $this->db->select($db_name);
        if($this->db->error()) {
            echo $this->db->getError();
            die();
        }
    }

CustomAJAXChat.php (https://github.com/Frug/AJAX-Chat/blob/smf/chat/lib/class/CustomAJAXChat.php#L13)
class CustomAJAXChat extends AJAXChat {

    // Initialize custom configuration settings
    function initCustomConfig() {
        global $db_name,$db_connection;
       
        // Use the existing ElkArte database connection:
        $this->setConfig('dbConnection', 'link', $db_connection);
    }

    // Override the database connection method to make sure the ElkArte database is selected:
    function initDataBaseConnection() {
        global $db_name;

        // Call the parent method to initialize the database connection:
        parent::initDataBaseConnection();

        // Select the SMF database:
        $this->db->select($db_name);
        if($this->db->error()) {
            echo $this->db->getError();
            die();
        }
    }



Title: Re: $db_connection
Post by: Spuds on September 05, 2015, 07:33:42 am
Make sure you have it as
Code: (yes) [Select]
db = database();
not
Code: (no) [Select]
db->database();
Its a static db object, not a method, so you have to assign it via = to some variable (here we use $db) before you try to use its methods via  $db->blabla
Title: Re: $db_connection
Post by: wintstar on September 05, 2015, 08:13:56 am
Thanks @Spuds, This exceeds my knowledge.
Title: Re: $db_connection
Post by: emanuele on September 05, 2015, 08:21:39 am
It's not really that much different:
Code: [Select]
    // Initialize custom configuration settings
    function initCustomConfig() {
         $db = database();
         $db->database();
Title: Re: $db_connection
Post by: wintstar on September 05, 2015, 08:38:38 am
Not work  :-[
QuoteNotice: Use of undefined constant db_connection - assumed 'db_connection' in C:\xampp\htdocs\elk104\Ajac\lib\custom.php on line 15
 
Fatal error: Call to undefined method Database_MySQL::database() in C:\xampp\htdocs\elk104\Ajac\lib\class\CustomAJAXChat.php on line 19

I think with removing " $db_connection" will bridge systems difficult to connect to the database. Why it was removed?

Title: Re: $db_connection
Post by: emanuele on September 05, 2015, 08:42:53 am
I'm not sure why you want to use the connection directly...
I'll have a look at the code this evening, and if you can point me to the file that uses $db_connection in your code, that would speed up the answer. ;)
Title: Re: $db_connection
Post by: wintstar on September 05, 2015, 08:48:49 am
Thanks @emanuele  :)

The starting point is the Blueimp Ajax Chat (https://github.com/Frug/AJAX-Chat/tree/smf) for Smf.

AJAX-Chat (https://github.com/Frug/AJAX-Chat/tree/smf)/chat (https://github.com/Frug/AJAX-Chat/tree/smf/chat)/lib (https://github.com/Frug/AJAX-Chat/tree/smf/chat/lib)/class (https://github.com/Frug/AJAX-Chat/tree/smf/chat/lib/class)/CustomAJAXChat.php
https://github.com/Frug/AJAX-Chat/blob/smf/chat/lib/class/CustomAJAXChat.php#L13


Title: Re: $db_connection
Post by: Spuds on September 05, 2015, 08:51:10 am
What I was trying to say is I think you have a typo and used $db->database() and it should be $db=database() At least I think.  Probably best to let emanuele take a look !

class CustomAJAXChat extends AJAXChat {

    // Initialize custom configuration settings
    function initCustomConfig() {
        global $db_name,$db_connection;
       
         $db = database();
$db_connection = $db->connection();

       
        // Use the existing ElkArte database connection:
        $this->setConfig('dbConnection', 'link', $db_connection);
    }
Title: Re: $db_connection
Post by: wintstar on September 05, 2015, 09:03:44 am
 :) :) :) :) :)
That was @Spuds. I'm so happy. Now I can solve the other problems. db_prefix and so..

Many Thanks you both. But I think it will not be my last question :-[ .



Title: Re: $db_connection
Post by: wintstar on September 05, 2015, 12:55:33 pm
Database connection works. Boards as channels are read. Only the user will not be displayed. For Today's first final, it goes on tomorrow.


Edit. Database connection to the tables from Ajax Chat is not working :-[


Title: Re: $db_connection
Post by: wintstar on September 06, 2015, 05:27:09 am
I dont get any further :'(


I have worked my way through and between SMF and ElkArte found no differences between the user and the login. Maybe someone has the time and could watch it times. Responsible for logging and the user are the following files:

The install.php is not finished yet. This includes only the installation, not the uninstall. Because beta status is the button for the chat, only available for the Admin.

Already in advance, thank you very much :)

Do not use in a liveaboard!
Title: Re: $db_connection
Post by: wintstar on September 08, 2015, 08:08:14 am
I believe I have found the error. Homemade, probably. It is up to my own Addon "Start Page". Only where the fault lies, I do not know. Will report as soon as I've found the cause.


Title: Re: $db_connection
Post by: wintstar on September 08, 2015, 08:34:34 am
It is the .htaccess

Code: [Select]
DirectoryIndex index.php?action=startpage index.php index.html index.htm


So I wanted to set the home page for my website.