Reputation: 73
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());
}
?>
Upvotes: 0
Views: 198
Reputation: 3023
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.
Upvotes: 2
Reputation: 41508
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!
Upvotes: 1
Reputation: 590
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?
Upvotes: 0
Reputation: 5122
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();
?>
Upvotes: 3