Jump to content

[SOLVED] Connecting to the Database


cheechm

Recommended Posts

Hi,

I can't seem to be able to connect to a database.

 

functions.php

require_once 'config.php';

class DBConnect extends SystemComponent {

    var $theQuery;
    var $link;

    //*** Function: DbConnect, Purpose: Connect to the database ***
    function DBconnect(){

        // Load settings from parent class
        $settings = SystemComponent::getSettings();

        // Get the main settings from the array we just loaded
        $host = $settings['dbhost'];
        $db = $settings['dbname'];
        $user = $settings['dbusername'];
        $pass = $settings['dbpassword'];

        // Connect to the database
        $this->con = mysql_connect("$host", "$user", "$pass") or die(mysql_error());
        mysql_select_db("$db", $this->con);
        register_shutdown_function(array(&$this, 'close'));

    }

    //*** Function: query, Purpose: Execute a database query ***
    function query($query) {

        $this->theQuery = $query;
        return mysql_query($query, $this->con);

    }
    

    //*** Function: fetchArray, Purpose: Get array of query results ***
    function fetchArray($result) {

        return mysql_fetch_array($result);


    

    //*** Function: close, Purpose: Close the connection ***
    function close() {

        mysql_close($this->con);
    }
  

}


}

 

config.php

class SystemComponent {

    var $settings;

    function getSettings() {

        // System variables
        $settings['siteDir'] = '****';

        // Database variables
        $settings['dbhost'] = '****;
        $settings['dbusername'] = '***';
        $settings['dbpassword'] = '***';
        $settings['dbname'] = '***';


        return $settings;

    }

}

 

I get this error when trying to connect to the database:

Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'sql12.streamline.net' (4) in functions.php on line 26

Can't connect to MySQL server on 'sql12.streamline.net' (4)

 

What is the problem?

Many thanks.

Link to comment
https://forums.phpfreaks.com/topic/71733-solved-connecting-to-the-database/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.