cgm225 Posted March 14, 2008 Share Posted March 14, 2008 I am using the mysql_fetch_array() function in one of my classes, but when I use the results of the mysql_fetch_array() in a while statement (a result which is not empty, and contains more than on entry (I checked it by simply print_r 'ing the array)), it only echos one row/entry, instead of looping through all of them as I want. Any ideas? Here is the part of my class using mysql_fetch_array()... public function Query($sql){ $this->sql = $sql; $this->result = mysql_query($this->sql,$this->connection) or die(mysql_error()); return $this->result; } public function FetchArray($query){ $rows = mysql_fetch_array($this->Query($query)); return $rows; } Here is the while statement in my script (which only echos a single entry from the $row array even though there are multiple entries in the array)... $query = "SELECT * FROM albums"; while($row = $gallery->FetchArray($query)) { echo $row['short_title']; } Quote Link to comment Share on other sites More sharing options...
turtleman8605 Posted March 14, 2008 Share Posted March 14, 2008 Using the FetchArray() method like that doesn't work, as it outputs a single static row. You have to have you while loop set up like this: while($row = mysql_fetch_array($sql)) { echo $row['attribute']; } I believe that the mysql_fetch_array() executes the query each time through the loop, accessing the next row until there are none left. So the way you are doing it only retrieves the first row of data. Quote Link to comment Share on other sites More sharing options...
trq Posted March 14, 2008 Share Posted March 14, 2008 You are passing mysql_fetch_array() an sql query. It expects a results resource. This should work. <?php public function Query($sql) { $this->sql = $sql; $this->result = mysql_query($this->sql,$this->connection) or die(mysql_error()); } public function FetchArray() { return mysql_fetch_array($this->result); } ?> then.... <?php $query = "SELECT * FROM albums"; $gallery->Query($query); while($row = $gallery->FetchArray()) { echo $row['short_title']; } ?> However, this class does look pretty sloppy and is qoing to be pretty difficult to debug when you do hit an error. Quote Link to comment Share on other sites More sharing options...
cgm225 Posted March 15, 2008 Author Share Posted March 15, 2008 Thank you both for your help.. it is working now. @Thorpe - I really appreciate your feedback on the class. With that being said, I have posted my full class below and wanted to know if you could give me some feedback on making it better for debugging? I am VERY new to OOP, and want to do this the right way. Any other feedback on naming conventions, syntax, making it more efficient, etc. would be greatly appreciated! class mysql { private $connection; protected $result; protected $sql; public function Connect($host, $name, $pass, $db) { $this->connection = mysql_connect($host,$name,$pass); mysql_select_db($db, $this->connection); return true; } public function Close(){ mysql_close($this->connection); return true; } public function Query($sql) { $this->sql = $sql; $this->result = mysql_query($this->sql,$this->connection) or die(mysql_error()); } public function FetchArray() { return mysql_fetch_array($this->result); } public function FetchNum(){ return mysql_num_rows($this->result); } } Thanks again so much! Quote Link to comment Share on other sites More sharing options...
trq Posted March 15, 2008 Share Posted March 15, 2008 I just don't see the point in the class to be honest. There is a perfectly good mysql OOP interface in the mysqli extension. Quote Link to comment Share on other sites More sharing options...
cgm225 Posted March 15, 2008 Author Share Posted March 15, 2008 I googled mysqli extension (because I didn't know what that was) Do I need to install that on top of my current php5 ? Or does it come with it? Also, how exactly do you use the OOP interface? Quote Link to comment Share on other sites More sharing options...
trq Posted March 15, 2008 Share Posted March 15, 2008 Its all covered in the manual. Quote Link to comment Share on other sites More sharing options...
cgm225 Posted March 15, 2008 Author Share Posted March 15, 2008 Thank you for your help! I am in the process of getting that installed now. I still may post my class in the OOP forum just for educational purposes, but I really appreciate your help! Quote Link to comment 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.