<-?-p-h-p


	class BaseService 
	{
		protected $table;
		
		
		public function save($data) 
		{
			$this->connect();

			$data = $data->data;
			$data = mysql_real_escape_string($data);

			$query = "INSERT INTO $this->table (`data`) VALUES ('$data');";
			$result = mysql_query($query);
			$error = mysql_error();
			$recnum = mysql_insert_id();
			mysql_close();

			if ($error) 
				return "error: " . $error;
			else  
				return $recnum;
		}


		public function load($id) 
		{
			$this->connect();

			$query = "SELECT data FROM $this->table WHERE id = '$id'";
			$result = mysql_query($query);
			$row = mysql_fetch_object($result);
			$error = mysql_error();
			mysql_close();

			if ($error) 
				return "error: " . $error;
			elseif (mysql_num_rows($result) == 0) 
				return "Not found";

			$data = new ByteArray($row->data);
			return $data;
		}
		
		
		public function getNumberOfRecords() 
		{
			$this->connect();

			$query = "SELECT * FROM $this->table;";
			$result = mysql_query($query);
			$numrows = mysql_num_rows($result);
			$error = mysql_error();
			mysql_close();

			if ($error) 
				return "error: " . $error;
			else
				return $numrows;
		}
		
		protected function connect()
		{
			mysql_connect("myserver", "username", "password");
			mysql_select_db("mydatabase");
		}
	}

	// no closing "?->" here on purpose