ch1326 Posted April 21, 2011 Share Posted April 21, 2011 Hi, everyone. I'm having a problem with getting the result from my php code. <?php class Mysql{ private $host; private $user; private $pass; private $db; public function __construct(){ $this->host = "localhost"; $this->user = "root"; $this->pass = "password"; $this->db = "person"; $this->conn(); } private function conn(){ $conn = mysql_connect($this->host, $this->user, $this->pass); if(!$conn){ mysql_error(); }else{ mysql_select_db($this->db, $conn); } } public function query($sql){ $result = mysql_query($sql); return $result; } public function fetch_array($result){ return mysql_fetch_array($result); } public function num_rows($result){ return mysql_num_rows($result); } } $db = new Mysql(); /******************************************************************************************/ class Person{ public $id; public $name; public function show_all(){ return $this->by_sql("SELECT * FROM USERS"); } public function show_by_id($id){ return $this->by_sql("SELECT * FROM USERS WHERE id = {$id} LIMIT 1"); } public function show_by_name($n){ return $this->by_sql("SELECT * FROM USERS WHERE name = {$n}"); } private function by_sql($sql){ global $db; $result = $db->query($sql); $arr = array(); while ($r = $db->fetch_array($result)){ $arr[] = $this->initiate($r); } return $arr; } private function initiate($r){ $obj = new self; foreach ($r as $att=>$val){ $obj->$att = $val; } return $obj; } } ?> <?php //phpinfo(); require_once("obj.php"); $user = new Person(); echo $user->show_by_id(1); ?> Quote Link to comment https://forums.phpfreaks.com/topic/234327-need-some-help-with-php-object-code/ Share on other sites More sharing options...
cs.punk Posted April 21, 2011 Share Posted April 21, 2011 Yes and the problem is? Do you recieve an error? Do you get a blank screen? Would make it much easier for someone to help you out... Quote Link to comment https://forums.phpfreaks.com/topic/234327-need-some-help-with-php-object-code/#findComment-1204415 Share on other sites More sharing options...
ch1326 Posted April 21, 2011 Author Share Posted April 21, 2011 Blank screen. Quote Link to comment https://forums.phpfreaks.com/topic/234327-need-some-help-with-php-object-code/#findComment-1204420 Share on other sites More sharing options...
KevinM1 Posted April 21, 2011 Share Posted April 21, 2011 Wow... okay, this is going to be long, so bear with me. First, never, ever, ever use 'global'. EVER. This goes for both procedural programming and OOP, but especially with OOP. 'global' is the exact opposite of what OOP is all about, as it ties your objects to the environment in which they are created, nullifying their modularity and breaking their encapsulation. Regardless of how you program, you should always have clean, clear, and explicit function/method signatures. 'global' is not explicit. In fact, it creates an unknown (to the rest of the system) implicit requirement. Simply put, if a function/method requires a parameter in order to work, pass that parameter through the argument list. That's why it's there. Second, part of the 'magic' with OOP is that objects can contain permanent references to other objects. This is known as composition, and is what you should be using here. Here's how you do it: class Mysql { // class definition } class Person { private $db; public function __construct($db) { $this->db = $db; } // rest of the definition, which uses $this->db to do work } $db = new Mysql(/* connection args */); $person = new Person($db); echo $person->show_by_id(1); Third, your Mysql errors are never output to the screen. You still need to echo or print them. Finally, there's already an OO variant of MySQL in PHP - the MySQLi extension. There's also an even more abstract, database agnostic option called PDO. Chances are, you already have at least one of these, if not both, available to you. Unless you're doing this for your own learning, I suggest using one of them. mysqli pdo Quote Link to comment https://forums.phpfreaks.com/topic/234327-need-some-help-with-php-object-code/#findComment-1204429 Share on other sites More sharing options...
ch1326 Posted April 22, 2011 Author Share Posted April 22, 2011 You are AWESOME, Nightslyr!!!!! Thank you so much Quote Link to comment https://forums.phpfreaks.com/topic/234327-need-some-help-with-php-object-code/#findComment-1204755 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.