Jump to content

Class only sending one record


Stephen68

Recommended Posts

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

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...?

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.