Jump to content

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?

Edited by matthew.javelet

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();

 

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.

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.