Jump to content

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;

So if I will have to make another function that will just grab one for and another one that will grab

more then one run? The one the will give me all the rows will just have to be a multi array? or is there

a way I could make it a 1 d array?

 

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.