Andy-H Posted February 3, 2009 Share Posted February 3, 2009 I wrote this class to handle my querys, I am new to OO and was just wondering if the numbering to identify the querys would slow it down? I think I will need to have multiple querys active at any given time. <?php class db { function db($host = 'localhost', $user = '', $pass = '', $DB = '') { $conn = mysql_connect($host, $user, $pass); $db = mysql_select_db($DB, $conn); } function query($str, $num = 1) { $this->query{$num} = mysql_query($str)or die('Query failed on line ' . __LINE__); } function num($num = 1) { return mysql_num_rows($this->query{$num}); } function obj($num = 1) { return mysql_fetch_object($this->query{$num}); } function assoc($num = 1) { return mysql_fetch_assoc($this->query{$num}); } function row($num = 1) { return mysql_fetch_row($this->query{$num}); } function arr($num = 1) { return mysql_fetch_array($this->query{$num}); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/143696-solved-database-class/ Share on other sites More sharing options...
flyhoney Posted February 3, 2009 Share Posted February 3, 2009 I rewrote your class a little bit just because I felt like it <?php class DB { private $conn; private $result; private $user; private $host; private $pass; private $db; public $queries; public function __construct($host = 'localhost', $user = '', $pass = '', $db = '') { $this->user = $user; $this->pass = $pass; $this->host = $host; $this->db = $db; $this->conn = mysql_connect($this->host, $this->user, $this->pass); mysql_select_db($this->db, $this->conn); } function query($query = '') { $this->queries[] = $query; $this->result = mysql_query($str)or die('Query failed on line ' . __LINE__); } function num() { return mysql_num_rows($this->result); } function obj() { return mysql_fetch_object($this->result); } function assoc() { return mysql_fetch_assoc($this->result); } function row($num = 1) { return mysql_fetch_row($this->result); } function arr($num = 1) { return mysql_fetch_array($this->result); } } ?> But to answer your question, no it wouldn't slow it down, but I challenge you to think of a situation in which you need to do what you are suggesting. Quote Link to comment https://forums.phpfreaks.com/topic/143696-solved-database-class/#findComment-753973 Share on other sites More sharing options...
Andy-H Posted February 3, 2009 Author Share Posted February 3, 2009 TBH, I just couldn't be bothered to think of loads of variable names and end up writing another script with irrelevant vars, i.e. $retard = $row[4]; lol So I wrote it like that so I could save my queries via a variable id inside the class. I suppose you make a good point tho lol also in your class rewrite you forgot to replace $str =P And the db user, pass, host, db vars dont need to be accessed anywhere apart from where they are defined so I dont think $this->user etc... Is irrelevant? Please correct me if I am wrong :S Quote Link to comment https://forums.phpfreaks.com/topic/143696-solved-database-class/#findComment-753975 Share on other sites More sharing options...
flyhoney Posted February 3, 2009 Share Posted February 3, 2009 And the db user, pass, host, db vars dont need to be accessed anywhere apart from where they are defined so I dont think $this->user etc... Is irrelevant? You're right, but if I was writing this class, I would be interested in adding error handling to more than just the constructor, in which case you want know more information about the current state of things. I would also be interested in writing some other methods, like, connect(), lastQuery() etc. Also, for documentation purposes, it is super helpful to explicitly declare your member variables so that someone else looking at the class can quickly see what they're working with. Quote Link to comment https://forums.phpfreaks.com/topic/143696-solved-database-class/#findComment-753979 Share on other sites More sharing options...
trq Posted February 3, 2009 Share Posted February 3, 2009 Does your class even work Andy-H? This method.... function query($str, $num = 1) { $this->query{$num} = mysql_query($str)or die('Query failed on line ' . __LINE__); } Your overiding the method itself with a property which I assume is meant to be an array. Arrays use [] to hold there elements. Ive not tested you code but that little snippet there looks like it should generate an error. At very least it should bring about some very unexpected results. And the db user, pass, host, db vars dont need to be accessed anywhere apart from where they are defined so I dont think $this->user etc... Is relevant? Its relevent because non of that stuff should actually be defined within your class. if that was the case you would need to edit the class itself every time you wanted to use it iin a different project. This goes against OOP principles. I have an entire framework that can run mutliple sites from the same code base. If I had to edit one of the classes itself to add my username / password and server details I would be screwed. Quote Link to comment https://forums.phpfreaks.com/topic/143696-solved-database-class/#findComment-753981 Share on other sites More sharing options...
Andy-H Posted February 3, 2009 Author Share Posted February 3, 2009 Yes, it works as expected. I just tested it now. Quote Link to comment https://forums.phpfreaks.com/topic/143696-solved-database-class/#findComment-753988 Share on other sites More sharing options...
flyhoney Posted February 3, 2009 Share Posted February 3, 2009 Braces can be used to index arrays in PHP, but I think PHP 6 is getting rid of that. But for future reference Andy-H, use [] not {}, it's best practice. Quote Link to comment https://forums.phpfreaks.com/topic/143696-solved-database-class/#findComment-753992 Share on other sites More sharing options...
trq Posted February 4, 2009 Share Posted February 4, 2009 Braces can be used to index arrays in PHP I am aware they can be, but that doesn't mean they should be. Quote Link to comment https://forums.phpfreaks.com/topic/143696-solved-database-class/#findComment-753994 Share on other sites More sharing options...
Andy-H Posted February 4, 2009 Author Share Posted February 4, 2009 I didnt use it for an array, I wasnt aware of the functionality but I was trying to extend the variable to $query1, $query2 etc... TY Quote Link to comment https://forums.phpfreaks.com/topic/143696-solved-database-class/#findComment-754005 Share on other sites More sharing options...
flyhoney Posted February 4, 2009 Share Posted February 4, 2009 Oh wow, yeah, I didn't realize that you were trying to do that. Yeah it's very surprising that this worked at all Quote Link to comment https://forums.phpfreaks.com/topic/143696-solved-database-class/#findComment-754434 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.