Andy-H Posted December 30, 2008 Share Posted December 30, 2008 Ok, I dont actually have any code right now, I have been looking into OO PHP to build a MySQL class to handle connections, querys, fetching data etc. I have read a few tutorials with no success into understanding how to so this due to the tutorials all being written in shroom season and comparing OO PHP to magic boes and stuff :S DOes anyone know of a simple OOP database handeling script that I can pick apart abnd try to learn from? Thanks for replies/help. Quote Link to comment https://forums.phpfreaks.com/topic/138929-solved-help-with-oop/ Share on other sites More sharing options...
Mchl Posted December 30, 2008 Share Posted December 30, 2008 Zend Framework perhaps? Quote Link to comment https://forums.phpfreaks.com/topic/138929-solved-help-with-oop/#findComment-726552 Share on other sites More sharing options...
Andy-H Posted December 30, 2008 Author Share Posted December 30, 2008 What does it do? lol Quote Link to comment https://forums.phpfreaks.com/topic/138929-solved-help-with-oop/#findComment-726559 Share on other sites More sharing options...
Andy-H Posted December 30, 2008 Author Share Posted December 30, 2008 From the tutorials I have read so far I got this far: <?php Class db { function db() { $this->host = 'localhost'; $this->user = '*****'; $this->pass = '*****'; $this->data = '*****'; $conn = dbConnection(); } function dbConnection { $link = mysql_connect($this->host, $this->user, $this->pass)or die('Unable to establish link to db.'); $conn = mysql_select_db($this->data, $link); } lol I assumes it was completely wrong and decided tutorials arent my style. I just perfer to edit the code, read the errors and fix them. Thats how I learned procedural anyway lol Quote Link to comment https://forums.phpfreaks.com/topic/138929-solved-help-with-oop/#findComment-726561 Share on other sites More sharing options...
Mchl Posted December 30, 2008 Share Posted December 30, 2008 Zend Framework is a collection of classes that do... well lots of things. Among them is one that deals with database connections. Quote Link to comment https://forums.phpfreaks.com/topic/138929-solved-help-with-oop/#findComment-726563 Share on other sites More sharing options...
Maq Posted December 30, 2008 Share Posted December 30, 2008 Here's the class I use. $host="***"; $user_name="***"; $password="***"; $db="***"; define("DB",$db); define("HOST",$host); define("USERNAME",$user_name); define("PASSWORD",$password); class db_works { var $Query_ID=0; var $connection=0; function connect() { if($this->connection==0) { $this->connection=mysql_connect(HOST,USERNAME,PASSWORD) or die("Database Error ".mysql_error()); $SelectResult = mysql_select_db(DB, $this->connection) or die("Could not Select Database".mysql_error()); } else { echo "Database Connection Could not be Established"; die(); } } function query($sql) { $this->Query_ID=mysql_query($sql,$this->connection); if(!$this->Query_ID) { $errorstr = mysql_error(); if (stripos($errorstr, "Duplicate") === false) { echo "Query Failed " . $errorstr . "\n"; } } else return $this->Query_ID; } function connection_close() { mysql_close($this->connection); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138929-solved-help-with-oop/#findComment-726565 Share on other sites More sharing options...
Andy-H Posted December 30, 2008 Author Share Posted December 30, 2008 Zend Framework is a collection of classes that do... well lots of things. Among them is one that deals with database connections. ahhh, thanks, Ill look into it Quote Link to comment https://forums.phpfreaks.com/topic/138929-solved-help-with-oop/#findComment-726567 Share on other sites More sharing options...
Andy-H Posted December 30, 2008 Author Share Posted December 30, 2008 Here's the class I use. <?php $host="***"; $user_name="***"; $password="***"; $db="***"; define("DB",$db); define("HOST",$host); define("USERNAME",$user_name); define("PASSWORD",$password); class db_works { var $Query_ID=0; var $connection=0; function connect() { if($this->connection==0) { $this->connection=mysql_connect(HOST,USERNAME,PASSWORD) or die("<b>Database Error</b><br>".mysql_error()); $SelectResult = mysql_select_db(DB, $this->connection) or die("Could not Select Database".mysql_error()); } else { echo "Database Connection Could not be Established"; die(); } } function query($sql) { $this->Query_ID=mysql_query($sql,$this->connection); if(!$this->Query_ID) { $errorstr = mysql_error(); if (stripos($errorstr, "Duplicate") === false) { echo "Query Failed " . $errorstr . " "; } } else return $this->Query_ID; } function connection_close() { mysql_close($this->connection); } } ?> Thanks, hopefully editing that will get me the basic understandings. If I get this right I'll post the code back later for anyone to critisize constructively or otherwise lol Quote Link to comment https://forums.phpfreaks.com/topic/138929-solved-help-with-oop/#findComment-726572 Share on other sites More sharing options...
Maq Posted December 30, 2008 Share Posted December 30, 2008 Yeah that's just a basic SQL class that has some error checking. But it's great if you have multiple servers and you forget the password, host etc... This way you really can't make a mistake, just plop this file in an include section and connect. Quote Link to comment https://forums.phpfreaks.com/topic/138929-solved-help-with-oop/#findComment-726574 Share on other sites More sharing options...
Andy-H Posted December 30, 2008 Author Share Posted December 30, 2008 $db = new db_works; $db->connect(); $query = "SELECT blah1, blah2 FROM tblBlah ORDER BY timesUsed DESC LIMIT 10"; $result = $db->query($query); Is that how you would use it? Quote Link to comment https://forums.phpfreaks.com/topic/138929-solved-help-with-oop/#findComment-726584 Share on other sites More sharing options...
Maq Posted December 30, 2008 Share Posted December 30, 2008 Minor syntax errors, and you need to include the file. require_once('db_works.php'); $db = new db_works(); $db->connect(); $query = "SELECT blah1, blah2 FROM tblBlah ORDER BY timesUsed DESC LIMIT 10"; $result = $db->query($sql) or die(mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/138929-solved-help-with-oop/#findComment-726597 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.