Skip to main content
Topic: $db_connection (Read 3848 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

$db_connection

I want to convert my mod SMF Ajax Chat 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
ErroNotice of xampp localhost
Error-Report: No database selected Error-Code: 1046

Code: [Select]
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?

Regards Stephan

Re: $db_connection

Reply #1

I think you should be able to do a
Code: [Select]
$db = database();
then use
Code: [Select]
 $db->connection();

Re: $db_connection

Reply #2

 :-*  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
Code: [Select]
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();
        }
    }



Last Edit: September 05, 2015, 07:20:32 am by wintstar
Regards Stephan

Re: $db_connection

Reply #3

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

Re: $db_connection

Reply #4

Thanks @Spuds, This exceeds my knowledge.
Regards Stephan

Re: $db_connection

Reply #5

It's not really that much different:
Code: [Select]
    // Initialize custom configuration settings
    function initCustomConfig() {
         $db = database();
         $db->database();
Bugs creator.
Features destroyer.
Template killer.

Re: $db_connection

Reply #6

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?

Regards Stephan

Re: $db_connection

Reply #7

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. ;)
Bugs creator.
Features destroyer.
Template killer.


Re: $db_connection

Reply #9

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 !

Code: [Select]
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);
    }

Re: $db_connection

Reply #10

 :) :) :) :) :)
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 :-[ .



Regards Stephan

Re: $db_connection

Reply #11

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 :-[


Last Edit: September 05, 2015, 01:18:33 pm by wintstar
Regards Stephan

Re: $db_connection

Reply #12

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:
  • ajac\lib\custom.php
  • ajac\lib\class\CustomAJAXChat.php

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!

Re: $db_connection

Reply #13

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.


Regards Stephan

Re: $db_connection

Reply #14

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.
Regards Stephan