Jump to content

Cant connect to database


mjahkoh

Recommended Posts

just trying to start off. Got this class 2 connect to a db but thinks arent working.The classes php file

<?php

 

function connect() {

    $this->log .= "connect() called<br />";

    switch($this->cnn_id) {

      /*

      You can define all the database connections you need in this

      switch statement.

      */

      case 0:

        $this->db_type = "mysql";

        $this->server = "Localhost";

        $user            = "havesters";

        $pwd              = "Ndege19qwangu76";

        $this->db    = "havesters";

        break;

      case 1:

        $this->db_type = "mysql";

        $this->server = "";

        $user            = "";

        $pwd              = "";

        $this->db    = "";

        break;

      case 2:

        $this->db_type = "postgres";

        $this->server = "";

        $user            = "";

        $pwd              = "";

        $this->db    = "";

        break;

      case 3:

        $this->db_type = "mssql";

        $this->server = "";

        $user            = "";

        $pwd              = "";

        $this->db    = "";

        break;

    }

}

?>

 

when i call it as thus

<?php

require_once('classes/common.php');

$d = new db(0);

?>

 

is  I get the error

 

 

Fatal error: Class 'db' not found in C:\xampp\htdocs\phptrysite\Databases.php on line 3

 

Link to comment
https://forums.phpfreaks.com/topic/74705-cant-connect-to-database/
Share on other sites

Please use [ code][ /code] tags when you post code.

 

You have only showed the code for the function connect() and the bit where you attempt to create a new instance of the class... what about the class it self?

 

<?php

class db {

    public $conn_id;

    function connect() {
        function code.....
    }

}
?>

 

Something like the above code is missing or at least you did not post it.

 

Another think, you can't do this:

 

$d = new db(0);

 

In relation to the code i posted in my previous answer you should do something like this:

 

$d = new db();
$d->conn_id = 0;
d->connect();

 

First you create an instance of the class, then you define conn_id to 0 for that instance and then you call connect() for that instance.

 

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.