iPixel Posted March 17, 2010 Share Posted March 17, 2010 I'm trying to start and shift my programming towards OOP style as opposed to procedural. Mainly for cleanliness. But whatever the reason i want to learn it =). So i'm making this easy breeze MySQL connection class but the page wont even open properly. Any help/guidence is much appreciated. <?php # OOP LEARNING PAGE # class DBcon() { function DBcon($username,$password) { $db = mysql_connect("localhost", $username, $password) or die(mysql_error()); if(mysql_select_db("tablename",$db) or die(mysql_error())) { echo "Success"; } else { echo "Error"; } } } # Let's test this connection class. $connect = new DBcon(); $connect->DBcon('abcd','efghi'); ?> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/195567-oop-noob-looking-for-guidence/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 17, 2010 Share Posted March 17, 2010 You have a fatal syntax error on line 4. You should be learning php (or learning something new in php), developing php code, and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that php will help you by displaying all the errors it detects. You will save a TON of time. Quote Link to comment https://forums.phpfreaks.com/topic/195567-oop-noob-looking-for-guidence/#findComment-1027619 Share on other sites More sharing options...
Wolphie Posted March 17, 2010 Share Posted March 17, 2010 To begin with, the problem you're having is on line 4, which is when you're defining a class. A class isn't a function, when you define a class you don't need parentheses (the brackets). When you initiate a class, you should include them though. You should also have some kind of error handling, using or die() is bad practice, especially so in OOP. Quote Link to comment https://forums.phpfreaks.com/topic/195567-oop-noob-looking-for-guidence/#findComment-1027622 Share on other sites More sharing options...
iPixel Posted March 17, 2010 Author Share Posted March 17, 2010 LOL the () are a force of habbit from using too many functions in my lifetime. While the page now works, it seems that $connect->DBcon('abcd','efghi'); doesnt actually give the values to the variables in the function. My syntax is correct no? Quote Link to comment https://forums.phpfreaks.com/topic/195567-oop-noob-looking-for-guidence/#findComment-1027630 Share on other sites More sharing options...
iPixel Posted March 17, 2010 Author Share Posted March 17, 2010 Woot i got it. Thanks Guys! <?php # OOP LEARNING PAGE # error_reporting(E_ALL); ini_set("display_errors", 1); class DBcon { function DBcon($username,$password) { $db = mysql_connect("localhost", $username, $password); if(mysql_select_db("tablename",$db)) { echo "Success"; } else { echo "Error"; } } } # Let's test this connection class. $connect = new DBcon('abcd,efghi'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/195567-oop-noob-looking-for-guidence/#findComment-1027632 Share on other sites More sharing options...
Wolphie Posted March 17, 2010 Share Posted March 17, 2010 You're using the same namespace for the function. The class is already named DBCon, and inside that class you also have a function named DBCon. <?php class MySQLConn { function __construct($username, $password, $database, $host = 'localhost') { $conn = mysql_connect($host, $username, $password); if ($conn) { if (mysql_select_db($database, $conn)) { echo "Success"; } else { echo "Failed"; } } } } // This will automatically connect to MySQL once the object has been initiated because of using a constructor $db = new MySQLConn('username', 'password', 'database_name'); ?> http://www.php.net/manual/en/language.oop5.decon.php Quote Link to comment https://forums.phpfreaks.com/topic/195567-oop-noob-looking-for-guidence/#findComment-1027633 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.