how to get list of categories from a non-relational database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

how to get list of categories from a non-relational database

Post by Luke »

I have a database that I didn't design... nor do I have any control over... and it is set up like this:

Code: Select all

category	memid	address		etc.
"restaurants"	23	1234 Ros rd.	Bla
"lawyers"	234	3214 Poo rd.	BLAB
"restaurants"	23213	1123 Foo Rd.	asdf
"landscapers"	675	5485 Lelu rd.	asdfkj
"restaurants"	7642	234 Poopies	234
What would be the most efficient way to select all categories without duplicates IE:
restaurants
lawyers
landscapers
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Well... here's what I'm doing for now... please let me know if there's a better way to accomplish this:

Code: Select all

public function loadCategories(){
		$db = $this->DB();
		$sql = "SELECT `category` FROM `" . $this->_table . "`";
		$results = $db->execute($sql);
		if ($results)  {
			while (!$results->EOF) {
				$ret[] = $results->fields[0];
				$results->MoveNext();
			}
			$results->Close();
		}
		return array_unique($ret);
	}
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Probably

Code: Select all

select distinct category
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

awesome! That's exactly what I was looking for! :D

Code: Select all

public function loadCategories(){
		$db = $this->DB();
		$sql = "SELECT DISTINCT `category` FROM `" . $this->_table . "`";
		$results = $db->execute($sql);
		if ($results)  {
			while (!$results->EOF) {
				$ret[] = $results->fields[0];
				$results->MoveNext();
			}
			$results->Close();
		}
		return $ret;
	}
Post Reply