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.. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
matthew.javelet Posted March 5, 2013 Share Posted March 5, 2013 (edited) 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 March 5, 2013 by matthew.javelet Quote Link to comment 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(); ?> Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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(); Quote Link to comment 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 Quote Link to comment 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(); 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.