DuDeXsNaVe Posted May 10, 2011 Share Posted May 10, 2011 Hi Folks! I have been designing basic sites / web applications for the last couple of years now but have recently decided that I really want to be able to code amazing sites, so have started a journey to teach myself to become a better programmer / coder. (so any pointers on books to read would be good) my skills so far... (Jquery, PHP (procedural), HTML / XHTML) so want to develop these further and also learn AJAX, PHP OOP. On a daily basis i get on just fine with PHP writing almost all sites / apps in PHP ( Procedural ) but have decided that i should stop being lazy and actually learn to code OOP PHP, the only problem is on my first attemp i am completly stumped, i have wrote what i believe to be an extramly simple piece of code but for some reason it just does not work . Can one of you nice people please point out where the hell i am going wrong, i have written (see code below) a "class_lib.php" for my db connection etc and a simple html ("index.php") to display the results. "CLASS_LIB.PHP" <?php class dbcon { var $dbhost = NULL; var $dbuser = NULL; var $dbpass = NULL; var $db = NULL; var $con = NULL; function setdb($host, $user, $password, $database) { $this->dbhost = $host; $this->dbuser = $user; $this->dbpass = $password; $this->db = $database; $this->con = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass) or die (mysql_error()); mysql_select_db($this->db) or die (mysql_error()); } function querydb($query) { return mysql_query($query, $this->con) or die (mysql_error()); } } ?> "INDEX.PHP" <?php include("class_lib.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <p><?php $mysql = new dbcon(); $mysql->setdb('localhost','user','password','database'); $result = $mysql->querydb("SELECT * FROM datatable"); $num=mysql_numrows($result); $i=0; while ($i < $num) { $id = mysql_result($result,$i,"id"); $fname = mysql_result($result,$i,"fname"); $sname = mysql_result($result,$i,"sname"); echo nl2br("<div><b>$fname $sname</b><br /><br /><hr></div>"); $i++; } ?></p> </body> </html> Quote Link to comment Share on other sites More sharing options...
DuDeXsNaVe Posted May 10, 2011 Author Share Posted May 10, 2011 anyone??? :'( Quote Link to comment Share on other sites More sharing options...
dreamwest Posted May 10, 2011 Share Posted May 10, 2011 http://www.wizecho.com/nav=php&s=class Quote Link to comment Share on other sites More sharing options...
DuDeXsNaVe Posted May 10, 2011 Author Share Posted May 10, 2011 http://www.wizecho.com/nav=php&s=class This doesn't really help, it is not telling anything i dont already know. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted May 10, 2011 Share Posted May 10, 2011 what's the output? Quote Link to comment Share on other sites More sharing options...
trq Posted May 10, 2011 Share Posted May 10, 2011 Database wrappers are just that, a wrapper around already existing functionality. You class adds no benefit to the current implementation. I understand your likely just trying to learn the syntax but you need to go a little deeper than simply wrapping already existing functionality for no reason. You class relies upon the mysql extension, have you though of actually adding flexibility to your design and allowing it to use any kind of database extension? This will give you a better idea of OOP design. Thought still, I would only use a db abstraction as a learning tool, in real life there are very good database abstraction layers already written. PS: You might want to find a more up to date learning resource, your using PHP4 syntax. Quote Link to comment Share on other sites More sharing options...
DuDeXsNaVe Posted May 10, 2011 Author Share Posted May 10, 2011 what's the output? nothing blank page, also tested with putting "print_r($result);" into index.php to see what that output was and it just displayed the character "1" nothing else!! really odd, if i purposly enter an incorrect user or password it displays the error as expected so is using the class. Quote Link to comment Share on other sites More sharing options...
DuDeXsNaVe Posted May 10, 2011 Author Share Posted May 10, 2011 Database wrappers are just that, a wrapper around already existing functionality. You class adds no benefit to the current implementation. I understand your likely just trying to learn the syntax but you need to go a little deeper than simply wrapping already existing functionality for no reason. You class relies upon the mysql extension, have you though of actually adding flexibility to your design and allowing it to use any kind of database extension? This will give you a better idea of OOP design. Thought still, I would only use a db abstraction as a learning tool, in real life there are very good database abstraction layers already written. PS: You might want to find a more up to date learning resource, your using PHP4 syntax. Thanks for the insightful words , you are correct there is no point in what i am trying to do other than learn the syntax for OOP but other than REALLY basic stuff (like get / set) im stuck. Interesting you say im using PHP4 syntax, i had no idea, i am completely self taught and as yet have never read a book on PHP (obviously i will do now, any suggestions for a good one?), just out of curiosity what part of my syntax is PHP4 and what is the PHP5 or even PHP6 equivalent? oh p.s. has anyone actually got an answer to my actual problem, as i would love to know why it is not working!! Quote Link to comment Share on other sites More sharing options...
aruns Posted May 10, 2011 Share Posted May 10, 2011 use this $num=mysql_num_rows($result); insteed of $num=mysql_numrows($result); bcoz you dont have declaration in your lib for mysql_numrows Quote Link to comment Share on other sites More sharing options...
DuDeXsNaVe Posted May 10, 2011 Author Share Posted May 10, 2011 use this $num=mysql_num_rows($result); insteed of $num=mysql_numrows($result); bcoz you dont have declaration in your lib for mysql_numrows thanks for pointing that out,still no luck though no change Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted May 10, 2011 Share Posted May 10, 2011 Database wrappers are just that, a wrapper around already existing functionality. You class adds no benefit to the current implementation. I understand your likely just trying to learn the syntax but you need to go a little deeper than simply wrapping already existing functionality for no reason. You class relies upon the mysql extension, have you though of actually adding flexibility to your design and allowing it to use any kind of database extension? This will give you a better idea of OOP design. Thought still, I would only use a db abstraction as a learning tool, in real life there are very good database abstraction layers already written. PS: You might want to find a more up to date learning resource, your using PHP4 syntax. Thanks for the insightful words , you are correct there is no point in what i am trying to do other than learn the syntax for OOP but other than REALLY basic stuff (like get / set) im stuck. Interesting you say im using PHP4 syntax, i had no idea, i am completely self taught and as yet have never read a book on PHP (obviously i will do now, any suggestions for a good one?), just out of curiosity what part of my syntax is PHP4 and what is the PHP5 or even PHP6 equivalent? oh p.s. has anyone actually got an answer to my actual problem, as i would love to know why it is not working!! The 'var' keyword is deprecated, and a clear sign of PHP 4. You should be using one of the access modifiers - 'public', 'protected', or 'private' - instead. PHP 5 has been around since 2004. There's no reason not to get up to speed with it. There is no PHP 6. The key features that were slated for PHP 6 - namespaces and late static binding - have instead been incorporated in the latest version (or so) of PHP 5. Quote Link to comment Share on other sites More sharing options...
DuDeXsNaVe Posted May 10, 2011 Author Share Posted May 10, 2011 I have SOLVED the problem!!!!!!! :D :D but would it be possible for someone to tell me why this worked function querydb($query) { $results = mysql_query($query, $this->con) or die (mysql_error()); return $results; } and this didn't, i dont get the difference function querydb($query) { return mysql_query($query, $this->con) or die (mysql_error()); } Quote Link to comment Share on other sites More sharing options...
DuDeXsNaVe Posted May 10, 2011 Author Share Posted May 10, 2011 Database wrappers are just that, a wrapper around already existing functionality. You class adds no benefit to the current implementation. I understand your likely just trying to learn the syntax but you need to go a little deeper than simply wrapping already existing functionality for no reason. You class relies upon the mysql extension, have you though of actually adding flexibility to your design and allowing it to use any kind of database extension? This will give you a better idea of OOP design. Thought still, I would only use a db abstraction as a learning tool, in real life there are very good database abstraction layers already written. PS: You might want to find a more up to date learning resource, your using PHP4 syntax. Thanks for the insightful words , you are correct there is no point in what i am trying to do other than learn the syntax for OOP but other than REALLY basic stuff (like get / set) im stuck. Interesting you say im using PHP4 syntax, i had no idea, i am completely self taught and as yet have never read a book on PHP (obviously i will do now, any suggestions for a good one?), just out of curiosity what part of my syntax is PHP4 and what is the PHP5 or even PHP6 equivalent? oh p.s. has anyone actually got an answer to my actual problem, as i would love to know why it is not working!! The 'var' keyword is deprecated, and a clear sign of PHP 4. You should be using one of the access modifiers - 'public', 'protected', or 'private' - instead. PHP 5 has been around since 2004. There's no reason not to get up to speed with it. There is no PHP 6. The key features that were slated for PHP 6 - namespaces and late static binding - have instead been incorporated in the latest version (or so) of PHP 5. Thanks for that, i shall investigate the access modifiers a little more. as i say i am self taught and got the "var" thing from when i used to do VB programming, just found it worked dont think i actually picked it up from anywhere. anyone know of any good books to get upto speed on PHP / PHP OOP as most i have looked at start from a REALLY basic / beginner point of view Quote Link to comment Share on other sites More sharing options...
trq Posted May 11, 2011 Share Posted May 11, 2011 anyone know of any good books to get upto speed on PHP / PHP OOP as most i have looked at start from a REALLY basic / beginner point of view Sorry to say, but it looks to me like that is exactly where you should be starting from. However, Google the book PHP Objects, Patterns & Practice - it's a pretty good starting point. Quote Link to comment Share on other sites More sharing options...
fugix Posted May 11, 2011 Share Posted May 11, 2011 also, php.net offers run-through on the basics of the OOP syntax..you might already know most of the inforamtion, however they provide examples that may help you to understand the syntax/theory better http://www.php.net/manual/en/language.oop5.php 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.