Jump to content

query giving back multi array


Stephen68

Recommended Posts

Hello I have two function in my class, one runs a query and the other fetches the row/s. The problem is

that it is sending back a multi dimension array. Any help would be great

 

<?php
#-----------------------------------------#
# Fucntion for running query on the db    #
#-----------------------------------------#
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

#-------------------------------------------------#
# Function for fetching all the information from  #
# the database.                                   #
#-------------------------------------------------#

function fetchRecords ($rows) {

$out = array();
    while ($results = mysql_fetch_assoc($rows)) {
	$out[] = $results;
}

//Test to see if there are any errors
if (!$rows) {
	$this->errorMsg('There was an error fetching record/s information, please contact site admin.');
	return 0;
}

return $out;
}#End of fetchReocrds function


?>

 

There are more functions and what not in there for connect to db and stuff as well. Here is how I call it.

 

<?php
$db = new admin(bla, bla);
$db->dbConnect();

$sql = "SELECT * FROM TABLE";
$results = $db->query($sql);
$rows = $db->fetchRecords($results);

print_r($rows);
?>

 

The print_r puts out an 2-d array not sure why?

Link to comment
https://forums.phpfreaks.com/topic/177382-query-giving-back-multi-array/
Share on other sites

Well if it didn't print out a 2-d array you would only get one row from that select all.. is that what you're after

 

 

This right here is what is making the 2D array... the little braces after out

$out[] = $results;

 

If you just want a 1D array then take them off

$out = $results;

No there is no way you could make multiple results into a one dimensional array, each record needs to go into its own index to keep separated.

 

These classes really add nothing in the way of functionality. People however persist and insist in writing them over and over again.

Change $out = array();    while ($results = mysql_fetch_assoc($rows)) {

$out[] = $results;

}to    while ($results = mysql_fetch_assoc($rows)) {

return $results;

}I wouldn't use error reporting for that function, since the query function will return an error if you have found one already, so it would show two errors if the query function fails.

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.