0

I am a php noob. I am trying to make a database connection file into a class (does it need to be private, static or something?). Netbeans is now throwing up errors and I get 500 internal server error when I try and run it from a browser. I think I have missed something simple ! Thanks

<?php
class config{

//define server, username, password and database
var $db_host="domain.co.uk.mysql"; 
var $db_user="user";                 
var $db_password="password";    
var $db_name="dbname"; 
var $db_tableprefix="tableprefix"; 
//connect to MySQL server
mysql_connect($db_host,$db_user,$db_password);

//select the database
@mysql_select_db($db_name) or die ("Unable to select database: " . mysql_error());

}
?>
2
  • 2
    take a look on how to create php classes...you have no methods..you need them so you can instantiate your class and use your connection. Since you are a noob, as you said, try using CodeIgniter, will help you a lot... Commented Feb 16, 2012 at 17:25
  • 1
    What you have there is a synax error + written for PHP4. Try reading this (php.net/manual/en/language.oop5.basic.php) a bit and come back if you are still unable to get it to work. Commented Feb 16, 2012 at 17:26

4 Answers 4

3

None of your statements are in methods. And you have not instantiated the class and called on a method that would perform your statements.

Classes are for object oriented programming. Your class has just old regular procedural code in a class which will not work.

<?php
    class config{

        //define server, username, password and database
        var $db_host="domain.co.uk.mysql"; 
        var $db_user="user";                 
        var $db_password="password";    
        var $db_name="dbname"; 
        var $db_tableprefix="tableprefix"; 

        public function connect() {
            //connect to MySQL server        
            mysql_connect($db_host,$db_user,$db_password);

            //select the database
            @mysql_select_db($db_name) or die ("Unable to select database: " . mysql_error());
        }

    }

    $var = new config();
    $var->connect();

?>
Sign up to request clarification or add additional context in comments.

Comments

2

For sake of argument here, I'm assuming your class called "config" is meant to hold far more than just config information for the database. In which case I would make a class called config that has your config settings in it.

class config {
    //define server, username, password and database
    var $db_host="domain.co.uk.mysql"; 
    var $db_user="user";                 
    var $db_password="password";    
    var $db_name="dbname"; 
    var $db_tableprefix="tableprefix"; 
}

And a class for your DB object, which should extend the mysqli object since it's more efficient than the procedural mysql functions and built for OOP:

   class db extends mysqli {
        protected static var $dbo = NULL;
        public static function &getInstance() {
            //load config
            $config = new config();
            if( self::$dbo === NULL ) self::$dbo = new db($config->db_host,$config->db_user,$config->db_password, $config->db_name);
        }
        return self::$dbo;
   }

This will allow you to build other functionality into your DBO class and it will prevent the script from making duplicate connections to the database. When you want to use a database connection, call:

$db = db::getInstance();

And you'll receive the instance of the db class (mysqli) which you can use from there.

Comments

1

The old mysql libraries aren't best suited for the object oriented classes. Use mysqli like this...

 class config{
     //define server, username, password and database
     private $db_host="domain.co.uk.mysql"; 
     private $db_user="user";                 
     private $db_password="password";    
     private $db_name="dbname"; 
     private $db_tableprefix="tableprefix"; 

    //connect to MySQL server
    public static function getConnection(){
         return new mysqli($db_host,$db_user,$db_password, $db_name);
    }
}

Then call your class like this:

$conn = config::getConnection();

You can then use the connection like:

$result = $conn->query("select * from sometable;");
var_dump($result);

Also, you may want to name your class something other than config.... maybe DbConnection and put it in a file by itself called DbConnection.php

Good luck!

Comments

0

When composing a class, you have to know how things will work ... to have properties, methods .. etc. Just writing the class will not do the job. Define what you want to be done - general mysql class connector, some specific task?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.