Object-Oriented Programming

Return a MySQL array

Class and Method

class OccupancyInfo
{
	
// ---------------------------------------------------------------------

	function __construct() {
		$this->servername='localhost';
		$this->username='admin';
		$this->password='******';
		$this->dbname='beds';
		$this->whichTable='occupancy';
	}

// ---------------------------------------------------------------------
	
	function getOccupancyInfo() {
		
		try {
			$conn = new PDO("mysql:host=$this->servername;dbname=$this->dbname", $this->username, $this->password);
			$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			
			$sql = "SELECT * from $this->whichTable";
			$stmt = $conn->query($sql);
			
			$array__occupancy_info = array();
			
			while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
				$array__occupancy_info[] = $row;
			}
			return $array__occupancy_info;
		}
		catch(PDOException $e) {
			echo $e->getMessage();
		}
	}
}