Optimizing PHP by minimize the request to the database

Optimizing PHP by minimize the request to the database by putting into a array

The array key is a hash and the value is the id. The reason you hash the key is because your looking up more than one field such as the first_name, last_name and company. Instead of putting this all together into a long string you can hash it into 8 characters and when your going to run the database check the array first to see if the query was already ran if not run the sql and append it to the array. It’s a cache.

	function get_user($first_name, $last_name, $company) {
		$hash = hash('crc32', $first_name . $last_name . $company);

		if (array_key_exists($hash, $this->users_cache)) {
			echo 'from cache';
			$ownerID = $this->users_cache[$hash] ;
		}
		else{
		
			$ownerID = db_lookup("SELECT userID
				FROM `users`
				WHERE `last_name` = '" . trim($last_name) . "' 
				AND first_name = '" . trim($first_name) ."' 
				AND company LIKE '%". $company ."%'"
			);

			$this->users_cache[$hash] = $ownerID;

		}

		return $ownerID;
	}