Yohanne Posted March 5, 2013 Share Posted March 5, 2013 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 More sharing options...
Jessica Posted March 5, 2013 Share Posted March 5, 2013 How would we know? Besides the fact that single quotes strings are not parsed. You need a username. Link to comment https://forums.phpfreaks.com/topic/275260-oop-mysql-connection/#findComment-1416673 Share on other sites More sharing options...
Yohanne Posted March 5, 2013 Author Share Posted March 5, 2013 okay, i put root as a username here.. and i have an tag echo to initialize if code is running. but its get empty. means there is something wrong. with it. Link to comment https://forums.phpfreaks.com/topic/275260-oop-mysql-connection/#findComment-1416677 Share on other sites More sharing options...
matthew.javelet Posted March 5, 2013 Share Posted March 5, 2013 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? Link to comment https://forums.phpfreaks.com/topic/275260-oop-mysql-connection/#findComment-1416678 Share on other sites More sharing options...
Yohanne Posted March 5, 2013 Author Share Posted March 5, 2013 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(); ?> Link to comment https://forums.phpfreaks.com/topic/275260-oop-mysql-connection/#findComment-1416702 Share on other sites More sharing options...
KevinM1 Posted March 5, 2013 Share Posted March 5, 2013 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. Link to comment https://forums.phpfreaks.com/topic/275260-oop-mysql-connection/#findComment-1416733 Share on other sites More sharing options...
P5system Posted March 6, 2013 Share Posted March 6, 2013 use mysqli function. It is used for connection in object oriented programming Link to comment https://forums.phpfreaks.com/topic/275260-oop-mysql-connection/#findComment-1416869 Share on other sites More sharing options...
Hall of Famer Posted March 8, 2013 Share Posted March 8, 2013 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(); Link to comment https://forums.phpfreaks.com/topic/275260-oop-mysql-connection/#findComment-1417385 Share on other sites More sharing options...
mweldan Posted March 8, 2013 Share Posted March 8, 2013 I have created one for the sake of learning. it is not completed but enough to show you how to start. https://github.com/weldan/karipap/blob/master/karipap.class.php Link to comment https://forums.phpfreaks.com/topic/275260-oop-mysql-connection/#findComment-1417577 Share on other sites More sharing options...
Hall of Famer Posted March 8, 2013 Share Posted March 8, 2013 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. Link to comment https://forums.phpfreaks.com/topic/275260-oop-mysql-connection/#findComment-1417589 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.