Page 1 of 1

connecting db using class... what am i doing wrong?

Posted: Fri Mar 28, 2008 12:47 am
by eccen
I am trying to connect to a database using class but it is returning error.. and I cannot figure out why :(

db.class.php

Code: Select all

 
<?php
class mysql {
    private $linkid;
    private $host;
    private $user;
    private $password;
    private $db;
    
    // class constructor
    function _construct($host, $user, $password, $db) {
        $this->host = $host;
        $this->user = $user;
        $this->password = $password;
        $this->db = $db;
    }
    
    // connect to the mysql server
    function connect() {
        try {
            $this->linkid = @mysql_connect($this->host,$this->user,$this->password);
            if (! $this->linkid)
                throw new Exception("Could not connect to the MySQL server");
        }
        catch (Exception $e) {
            die($e->getMessage());
        }
    }
    
    function select() {
        try {
            if (! @mysql_select_db($this->db, $this->linkid))
                throw new Exception("Could not connect to the database");
        }
        catch (Exception $e) {
            die($e->getMessage());
        }
    }
}
?>
 
test.php

Code: Select all

 
<?
    include "db.class.php";
    $mysqldb = new mysql("localhost", "dbuser", "password","dbtable");
    $mysqldb->connect();
    $mysqldb->select();
?>
 
What am I doing wrong? I need help.. :?

Re: connecting db using class... what am i doing wrong?

Posted: Fri Mar 28, 2008 1:48 am
by Christopher
What is the error message?

Re: connecting db using class... what am i doing wrong?

Posted: Fri Mar 28, 2008 1:51 am
by it2051229
do NOT MAKE USE "mysql" AS A CLASS NAME.. it's RESERVED WORD.. change it to something else..
and uhmm am not sure about your constructor but it worked fine for me... i modified your code

Code: Select all

 
<?php
class mysqlClass {
    private $linkid;
    private $host;
    private $user;
    private $password;
    private $db;
   
    // class constructor
    function mysqlClass($host, $user, $password, $db) {
        $this->host = $host;
        $this->user = $user;
        $this->password = $password;
        $this->db = $db;
    }
   
    // connect to the mysql server
    function connect() {
        try {
            $this->linkid = @mysql_connect($this->host,$this->user,$this->password);
            if (! $this->linkid)
                throw new Exception("Could not connect to the MySQL server");
        }
        catch (Exception $e) {
            die($e->getMessage());
        }
    }
   
    function select() {
        try {
            if (! @mysql_select_db($this->db, $this->linkid))
                throw new Exception("Could not connect to the database");
        }
        catch (Exception $e) {
            die($e->getMessage());
        }
    }
}
?>
 
and the connecting page..

Code: Select all

 
<?php
    include "db.class.php";
    $mysqldb = new mysqlClass("localhost", "root", "","mycms");
    $mysqldb->connect();
    $mysqldb->select();
?>
 

Re: connecting db using class... what am i doing wrong?

Posted: Fri Mar 28, 2008 1:56 pm
by eccen
I figured it out :)
I was missing an underscore for constructor

should be __construct(####), not _construct(####)

Re: connecting db using class... what am i doing wrong?

Posted: Sat Mar 29, 2008 5:58 pm
by it2051229
good job :!: now go back to work :twisted: