ch1326 Posted April 7, 2010 Share Posted April 7, 2010 Got some errors from this code. need help please <?php class MYSQL { var $host = "localhost"; var $user = "root"; var $pass = "password"; var $db = "userlist"; function __construct($host, $user, $pass, $db){ $this->host = $host; $this->user = $user; $this->pass = $pass; $this->db = $db; } public function query($sql){ $this->connDB(); $result = mysql_query($sql); return fetchArray($result); } public function fetchArray($result){ while ($row = mysql_fetch_array($result) ){ echo "Name is ". $row["name"]; } } public function connDB () { $conn = mysql_connect($this->host, $this->user, $this->pass); mysql_select_db($this->db, $conn); } } $example = new MYSQL(); echo $example->query("select * from user"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/197856-need-help-with-oop-please/ Share on other sites More sharing options...
KevinM1 Posted April 7, 2010 Share Posted April 7, 2010 Well, first, if you define your constructor to take arguments, you must actually pass arguments to it when you invoke it, unless you define those arguments as default arguments. In other words, the following will throw an error: class MYSQL { var $host = "localhost"; var $user = "root"; var $pass = "password"; var $db = "userlist"; function __construct($host, $user, $pass, $db) { $this->host = $host; $this->user = $user; $this->pass = $pass; $this->db = $db; } } $db = new MYSQL(); Whereas the following will not: class MYSQL { var $host = "localhost"; var $user = "root"; var $pass = "password"; var $db = "userlist"; function __construct($host = '', $user = '', $pass = '', $db = '') { $this->host = $host; $this->user = $user; $this->pass = $pass; $this->db = $db; } } $db = new MYSQL(); Note, however, that the constructor in this second example is pretty useless as written, as it will merely set all your data members to empty strings. You should also use access modifiers for your data members. The 'var' keyword works, but is essentially deprecated (it's a remnant of PHP 4), and makes those data members publicly accessible, which is pretty useless. Data members should be protected or private, with appropriate getter and setter methods. Quote Link to comment https://forums.phpfreaks.com/topic/197856-need-help-with-oop-please/#findComment-1038297 Share on other sites More sharing options...
Mchl Posted April 7, 2010 Share Posted April 7, 2010 Also: 1: 'var' should not be used in PHP5. Use proper visibility modifiers instead (public/protected/private) 2: If you're toying with mysql and OOP, why not give mysqli a try? Quote Link to comment https://forums.phpfreaks.com/topic/197856-need-help-with-oop-please/#findComment-1038300 Share on other sites More sharing options...
hackalive Posted April 7, 2010 Share Posted April 7, 2010 go mysqli Quote Link to comment https://forums.phpfreaks.com/topic/197856-need-help-with-oop-please/#findComment-1038303 Share on other sites More sharing options...
KevinM1 Posted April 7, 2010 Share Posted April 7, 2010 Also: 1: 'var' should not be used in PHP5. Use proper visibility modifiers instead (public/protected/private) You should also use access modifiers for your data members. The 'var' keyword works, but is essentially deprecated (it's a remnant of PHP 4), and makes those data members publicly accessible, which is pretty useless. Data members should be protected or private, with appropriate getter and setter methods. Quote Link to comment https://forums.phpfreaks.com/topic/197856-need-help-with-oop-please/#findComment-1038384 Share on other sites More sharing options...
Mchl Posted April 7, 2010 Share Posted April 7, 2010 Like you read everything other people write Quote Link to comment https://forums.phpfreaks.com/topic/197856-need-help-with-oop-please/#findComment-1038386 Share on other sites More sharing options...
KevinM1 Posted April 7, 2010 Share Posted April 7, 2010 Like you read everything other people write I'm offended, sir, offended! Pistols, at dawn. Quote Link to comment https://forums.phpfreaks.com/topic/197856-need-help-with-oop-please/#findComment-1038388 Share on other sites More sharing options...
Mchl Posted April 7, 2010 Share Posted April 7, 2010 I'm sleeping at dawn. Does noon suit you? Sir? Quote Link to comment https://forums.phpfreaks.com/topic/197856-need-help-with-oop-please/#findComment-1038397 Share on other sites More sharing options...
ignace Posted April 7, 2010 Share Posted April 7, 2010 Like you read everything other people write Has this something to do with you thinking everyone else besides you is incapable of helping? Such arrogance Quote Link to comment https://forums.phpfreaks.com/topic/197856-need-help-with-oop-please/#findComment-1038417 Share on other sites More sharing options...
KevinM1 Posted April 7, 2010 Share Posted April 7, 2010 I'm sleeping at dawn. Does noon suit you? Sir? I'm eating at noon. Is early evening acceptable, sir? Quote Link to comment https://forums.phpfreaks.com/topic/197856-need-help-with-oop-please/#findComment-1038424 Share on other sites More sharing options...
Mchl Posted April 7, 2010 Share Posted April 7, 2010 I think your early evening is close to my midinight, sir. Quote Link to comment https://forums.phpfreaks.com/topic/197856-need-help-with-oop-please/#findComment-1038446 Share on other sites More sharing options...
ignace Posted April 7, 2010 Share Posted April 7, 2010 I think your early evening is close to my midinight, sir. Maybe you could both just hire a mercenary and let them duke it out Quote Link to comment https://forums.phpfreaks.com/topic/197856-need-help-with-oop-please/#findComment-1038462 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.