Jump to content

OOP mysql Connection?


Yohanne

Recommended Posts

Hi all,
 
could you help me.. what getting wrong with this code? it is not work.
 
<?php
define ('DB_SERVER_', 'localhost');
define ('DB_USER_', '');
define ('DB_PASS_', '');
define ('DB_NAME_', 'ocs_proton');

class DB_class
{
public function __construct()
{
$Conn = mysql_connect(DB_SERVER_, DB_USER_, DB_PASS_) or die('Server not Conneceted -> $Conn'. mysql_error());
mysql_select_db(DB_NAME_, $Conn) or die('database not connected -> $Conn'. mysql_error());

echo '$Conn';
}
}
?>

 

 

 
Thanks..
Link to comment
https://forums.phpfreaks.com/topic/275260-oop-mysql-connection/
Share on other sites

You need to provide us with all relative information as much as you can to help us provide you with a decent answer.

 

The code you are using obviously has it set up to return errors if there is either a server connection issue or database connection issue. 

 

Which one is it, what is the exact error you get?

Thanks for the replay and now i get it and solve.. Thanks

 

<?php
	define ('DB_SERVER_', 'localhost');
	define ('DB_USER_', 'root');
	define ('DB_PASS_', '');
	define ('DB_NAME_', 'ocs_proton');
	
	
	class DB_class
		{
			
function __construct()
	{
	$Conn = mysql_connect(DB_SERVER_, DB_USER_, DB_PASS_) or die("Server not Conneceted -> $Conn". sha1(mysql_error()));
	mysql_select_db(DB_NAME_, $Conn) or die("database not connected -> $Conn". sha1(mysql_error()));
		echo "$Conn";	
	}
		}
$ = new DB_class();
?>

There are several errors with your code:

 

1. Why are you trying to echo the connection?

2. Why aren't you saving the connection as a data member?

3. Why are you hashing (using sha1) your mysql error?

4. $ isn't a valid label for a variable.

 

You want something like:

 

 

class DB
{
   private $conn;
 
   public function __construct()
   {
      $this->conn = mysql_connect(/* stuff */);
      mysql_select_db(/* more stuff */);
   }
 
   // other methods
}
 
$myDB = new DB();

 

That said, you really should just use either MySQLi or PDO as the old mysql_* functions are soft deprecated, and both are OOP out of the box, saving you from having to write your own.

You can use PDO, it is a really powerful class and very object oriented. All you have to do is this:

 

$pdo = new PDO($dsn, $user, $password);
$stmt = $pdo->prepare('SELECT * FROM tablename WHERE column1 = ? AND column2 = ?');
$stmt->execute(array($field1, $field2));
$obj = $stmt->fetchObject();
  On 3/8/2013 at 12:49 AM, Hall of Famer said:

 

You can use PDO, it is a really powerful class and very object oriented. All you have to do is this:

 

$pdo = new PDO($dsn, $user, $password);
$stmt = $pdo->prepare('SELECT * FROM tablename WHERE column1 = ? AND column2 = ?');
$stmt->execute(array($field1, $field2));
$obj = $stmt->fetchObject();

 

Can a mod delete the code in my last post? I did not think much when I quoted it from php.net and posted it with some minor editions, but its not a very good OOP practice. Can be misleading for newbies, so its better to let it go. Thx.

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.