Jump to content

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.