neofex Posted February 14, 2007 Share Posted February 14, 2007 Hey all. I am new to PHP and MySQL but am learning fast. I have almost got a grip on object oriented code but have hit a snag. The mysqlquery function works okay when i call it with a $mysql->mysqlquery("the query here") but i am having trouble getting the object and array functions working. i need to setup while loops that move through the array or object set returned by the class. normally this would look like while ($array = mysql_fetch_array($result)) but when i try to do it with the class via while ($array = $mysql->array) i get a loop that is infinite and only spits out the first record. So far I have figured everything about php on my own, but this one is stumping for too long so i thought i would give the forums here a chance. Thank you guys and gals so much in advance for any help you can give. happy coding, Steve The reluctant programmer. <?php class mysql { public $db; public $server; public $username; public $password; public $conn; public $result; public $object; public $array; function mysql() { $this->db = ""; $this->server = ""; $this->username = ""; $this->password = ""; $this->conn = mysql_connect($this->server, $this->username, $this->password) or die ("Unable to connect to the database."); mysql_select_db($this->db) or die ("Unable to select the database."); } function mysqlquery($query) { $this->result = mysql_query($query) or die ("There was an error in the query."); } function mysqlfetchobject($query) { $this->result = mysql_query($query) or die ("There was an error in the query."); $this->object = mysql_fetch_object($this->result); return $this->object; } function mysqlfetcharray($query) { $this->result = mysql_query($query) or die ("There was an error in the query."); $this->array = mysql_fetch_array($this->result); return $this->array; } function _destruct() { mysql_free_result($this->result); mysql_close($this->conn); } } ?> Link to comment https://forums.phpfreaks.com/topic/38495-mysql-class-help/ Share on other sites More sharing options...
Balmung-San Posted February 14, 2007 Share Posted February 14, 2007 That's because you're not updating the pointer within the result. mysql_fetch_array keeps a pointer for that result tucked away, so that it will move through each record one at a time, allowing the while() loop to work. Your class only ever returns the first record because it does not update the pointer, and with the way your function is designed, it cannot update the pointer because you requery each time. Link to comment https://forums.phpfreaks.com/topic/38495-mysql-class-help/#findComment-184690 Share on other sites More sharing options...
neofex Posted February 14, 2007 Author Share Posted February 14, 2007 thanks for your prompt reply. I think i understand what you mean. I am still stumped on how to make this work in the class. Essentially i have to do a query of the database, put that into a result, and then into an object or array for while loops. I know it is asking a lot, but how might i achieve this in my class. I am doing all the research i can online too so as not to be a pest. thx Steve Link to comment https://forums.phpfreaks.com/topic/38495-mysql-class-help/#findComment-184700 Share on other sites More sharing options...
Balmung-San Posted February 14, 2007 Share Posted February 14, 2007 I'm not entirely sure how you would want to do it. I personally have never tried to abstract out the mysql functions, and if I was going to use an OO approach, I would use PDO. Link to comment https://forums.phpfreaks.com/topic/38495-mysql-class-help/#findComment-184705 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.