mds1256 Posted February 6, 2012 Share Posted February 6, 2012 Hi I have created a Database class that uses the singleton method. I have a query method within this class so i would call this to return an array of results. What i need to know is what would be the best way to use this returned array Database Class: <?php class Database { private static $dbInstance; private $hostname; private $username; private $password; private $database; private function __construct() { $this->hostname = 'localhost'; $this->username = 'user'; $this->password = 'pass'; $this->database = 'test'; mysql_connect($this->hostname, $this->username, $this->password); mysql_select_db($this->database); } public static function getDBInstance() { if (!self::$dbInstance) { self::$dbInstance = new Database(); } return self::$dbInstance; } public function query($q) { return mysql_query($q); } } ?> Person Class: <?php require_once('Database.php'); class person { public function getName($id) { $con = Database::getDBInstance(); $query = $con->query("select name from data where id = ".$id); $result = mysql_fetch_assoc($query); echo $result['name']; } } ?> Index page: <?php require_once('person.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=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <?php $person = new person(); $person->getName(1); echo "<br />"; $person->getName(2); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/256527-oop-database-class-and-query-method-help/ Share on other sites More sharing options...
trq Posted February 6, 2012 Share Posted February 6, 2012 I would seriously consider removing the singleton from this class. Singletons are renowned for making your code tightly coupled which should be avoided where possible. As for your question, I'm not sure I understand the issue. Your "person" model should represent a single row in your database, if you want "persons" you should create another model for that. This "persons" model or collection would then return an array or "person" objects. Quote Link to comment https://forums.phpfreaks.com/topic/256527-oop-database-class-and-query-method-help/#findComment-1315061 Share on other sites More sharing options...
mds1256 Posted February 6, 2012 Author Share Posted February 6, 2012 I would seriously consider removing the singleton from this class. Singletons are renowned for making your code tightly coupled which should be avoided where possible. As for your question, I'm not sure I understand the issue. Your "person" model should represent a single row in your database, if you want "persons" you should create another model for that. This "persons" model or collection would then return an array or "person" objects. Hi, thanks for the reply. I am using singleton way so it doesnt create a new database connection / object each time i need to query the database? Is there a better way of achieving this. In the end I would like a database class that has a query method that I can use for each time I need to pull back results. so for example I need the query method to be dynamic and allow a return of an array, I then need to know the best way of allowing me to dynamically handling this array (maybe with another database method). I can do the following but if you look at the index page you will see that I need to store the returned array in a variable first then echo the variable with the array element. Looks a bit messy, is there a way to do this in a one liner? Database class: <?php class Database { private static $dbInstance; private $hostname; private $username; private $password; private $database; private function __construct() { $this->hostname = 'localhost'; $this->username = 'user'; $this->password = 'pass'; $this->database = 'test'; mysql_connect($this->hostname, $this->username, $this->password); mysql_select_db($this->database); } public static function getDBInstance() { if (!self::$dbInstance) { self::$dbInstance = new Database(); } return self::$dbInstance; } public function query($q) { return mysql_query($q); } } ?> person class: <?php require_once('Database.php'); class person { public function getName($id) { $con = Database::getDBInstance(); $query = $con->query("select name from data where id = ".$id); return mysql_fetch_assoc($query); } } ?> index page: <?php require_once('person.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=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <?php $person = new person(); $name = $person->getName(1); echo $name['name']; echo "<br />"; $name = $person->getName(2); echo $name['name']; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/256527-oop-database-class-and-query-method-help/#findComment-1315062 Share on other sites More sharing options...
trq Posted February 6, 2012 Share Posted February 6, 2012 I am using singleton way so it doesnt create a new database connection / object each time i need to query the database? Is there a better way of achieving this. Yes. Make the connection first and pass it into your object upon instantiation. I can do the following but if you look at the index page you will see that I need to store the returned array in a variable first then echo the variable with the array element. Looks a bit messy, is there a way to do this in a one liner? Your querying for two different users so not really. As I said, a "Person" represents a single "person". Quote Link to comment https://forums.phpfreaks.com/topic/256527-oop-database-class-and-query-method-help/#findComment-1315071 Share on other sites More sharing options...
mds1256 Posted February 6, 2012 Author Share Posted February 6, 2012 I am using singleton way so it doesnt create a new database connection / object each time i need to query the database? Is there a better way of achieving this. Yes. Make the connection first and pass it into your object upon instantiation. I can do the following but if you look at the index page you will see that I need to store the returned array in a variable first then echo the variable with the array element. Looks a bit messy, is there a way to do this in a one liner? Your querying for two different users so not really. As I said, a "Person" represents a single "person". The latter is correct, I have forgot I am working with objects woops... Do you have example of passing a connection object into this object. Also can you use array attributes on methods eg.: $person = getName()['firstname'] Quote Link to comment https://forums.phpfreaks.com/topic/256527-oop-database-class-and-query-method-help/#findComment-1315080 Share on other sites More sharing options...
trq Posted February 6, 2012 Share Posted February 6, 2012 $conn = mysql_connect(); $db = new Database($conn); Also can you use array attributes on methods eg.: Code: [select] $person = getName()['firstname'] Not unless your using php5.4 (which isn't stable yet). Quote Link to comment https://forums.phpfreaks.com/topic/256527-oop-database-class-and-query-method-help/#findComment-1315240 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.