Stephen68 Posted October 9, 2009 Share Posted October 9, 2009 ok I have a function in a class that runs a query that I pass it, once I get the value back from that I can then run another function to get the rows. The problem is this, it's only returning one row.... <?php function fetchRecords ($rows) { $results = mysql_fetch_array($rows); //Test to see if there are any errors if (!$results) { $this->errorMsg('There was an error fetching record/s information, please contact site admin.'); return 0; } return $results; }#End of fetchReocrds function ?> I'm not sure why I'm only getting one row here is the query function <?php function query($sql) {## Start of query function $this->query_holder = mysql_query($sql,$this->conn); //Test to see if query ran if (!$this->query_holder) { //Query did not run show error message $this->errorMsg("<p>There was an error in running the query, please contact site admin."); return 0; } $this->numRows = mysql_affected_rows($this->conn); return $this->query_holder; }## End of query function ?> so what I do to get the data is something like this <?php //Db connection stuff $admin = new admin(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); //Start new instance of calss. $admin->dbConnect(); //Connect to MySQL //a query that I want to run $sql = "SELECT * FROM table"; $results = $admin->query($sql); $rows = $admin->fetchRecords($results); print_r($rows); echo $admin->numRows; //Loop through each row in array code ?> I know there are 20 records so why am i only getting one? the print_r($rows); only shows one record? any ideas why this is going on? and the echo $admin->numRows; shows that there are 20 records that are to be showen. Stephen Link to comment https://forums.phpfreaks.com/topic/177141-class-only-sending-one-record/ Share on other sites More sharing options...
trq Posted October 9, 2009 Share Posted October 9, 2009 mysql_fetch_array returns on erecord each time it is called until there are no more records in your result. Link to comment https://forums.phpfreaks.com/topic/177141-class-only-sending-one-record/#findComment-934044 Share on other sites More sharing options...
Stephen68 Posted October 10, 2009 Author Share Posted October 10, 2009 Ahhh... so I need to loop maybe... <?php function fetchRecords ($rows) { $rowArray[] = array(); while ($results = mysql_fetch_array($rows)) { $rowArray[] = $results; } //Test to see if there are any errors if (!$results) { $this->errorMsg('There was an error fetching record/s information, please contact site admin.'); return 0; } return $results; }#End of fetchReocrds function ?> something like that...? Link to comment https://forums.phpfreaks.com/topic/177141-class-only-sending-one-record/#findComment-934077 Share on other sites More sharing options...
corbin Posted October 10, 2009 Share Posted October 10, 2009 No.... It's a good start, but there's two problem with it: mysql_fetch_array returns two possible things: -An array that represents the data retrieved. -False if no more rows exist That means that the last time the while(cond) condition is checked, $results will always be set to false. So, your if(!$results) will always execute. You should check $rows for an error: if(!$rows) Or, if you want to do it fail-proof: if(!mysql_num_rows($rows)) (Worth noting that mysql_num_rows will slow down more than !$rows, although probably not noticeably unless you run it like a million times.) Your other problem is that you should return $rowArray since it is what the data is being pushed into. Link to comment https://forums.phpfreaks.com/topic/177141-class-only-sending-one-record/#findComment-934084 Share on other sites More sharing options...
trq Posted October 10, 2009 Share Posted October 10, 2009 You would be much better off looping within your calling code. eg; $sql = "SELECT * FROM table"; if ($results = $admin->query($sql)) { if ($admin->numRows) { while ($row = $admin->fetchRecords($results)) { print_r($row); } } } Link to comment https://forums.phpfreaks.com/topic/177141-class-only-sending-one-record/#findComment-934085 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.