Steps for implementing a hash table in PHP and Mysql

I am new to programming language and I use PHP and mysql. I got a job to make hash tables in php. I need to do, save the elements that the user has collected, and then display. After some research on the Internet, I will take the following steps when implementing a hash table, please correct me if I am wrong:

  • Set up the tables:

    -> User table: uid (int [5]), username (varchar [128]), item_id (int [8], items_id_hash (int [50])

    -> Elements table: item_id (int [5]), item_name (varchar [128]), items_id_hash (int [50])

  • Create a hash function (how to create a hash function? Create it yourself or get it from the Internet?) To convert the key to a hash value and then paste it into the database. For example: hash item_id = 001 in the hash value = (for example) 12345. Then insert users into the table.

  • To display / search. Retrieve the hash values ​​from the user, and then compare it with the item table and display it.

Questions:

  • Are my steps right?
  • Where can I find a good php hash function? Can I use md5 or sha1 or salt?
+4
source share
4 answers

I think your idea of ​​a hash table is a little [nonexistent]. Hashtables breaks keys into similar lists. For example: a hash table based on the first letter of the name, so there will be 26 lists. Your hash is the first letter of the name, which then speeds up the search.

md5, sha1 are used to obtain hashes that are used to verify that the data has not been tampered with. they usually come in either 128-bit or 160-bit versions. Thus, it receives X data and sends it through a hash to create a 128-bit alphanumeric string, which should be the same no matter where it is done. This is usually a security issue.

EDIT: Extension to a question on how to get keys.

You can use the data module to create a key for use in a string. In the sample data,% X, where X is the total number of keys you would like to have. The problem is that X is hard to find; if you have 20 items, then making X in 20 is possible and makes it fast, since each element has its own line. But if you have 1000 items, then making% 1000 is impossible. Doing something like X = 75 will work better for this.

+4
source

You have two main problems:

1) The hash table parameter that you want to select (open | closed) the hash table.

2) A hashtable can be a simple array with key indexes and an array reference for collision cases.

3) You need to study the hash key generation algorithm ($ hash = ord ($ string [$ i]) + ($ hash <5) - $ hash; maybe enough), but you can select md5 / sha. If you know your key space, perhaps you can use unix gperf.

Here is my hash table implementation:

<?php /** A brief but simple closed hash table class. Jorge Niedbalski R. < jnr@niedbalski.org > **/ class HashTable { public $HashTable = array(); public $HashTableSize; public function __construct($tablesize) { if($tablesize) { $this->HashTableSize = $tablesize; } else { print "Unknown file size\n"; return -1; } } public function __destruct() { unset($this->HashTable); } public function generate_bucket($string) { for($i=0; $i <= strlen($string); $i++) { $hash = ord($string[$i]) + ($hash << 5) - $hash; } print "".$this->HashTableSize."\n"; return($hash%$this->HashTableSize); } public function add($string, $associated_array) { $bucket = $this->generate_bucket($string); $tmp_array = array(); $tmp_array['string'] = $string; $tmp_array['assoc_array'] = $associated_array; if(!isset($this->HashTable[$bucket])) { $this->HashTable[$bucket] = $tmp_array; } else { if(is_array($this->HashTable[$bucket])) { array_push($this->HashTable[$bucket], $tmp_array); } else { $tmp = $this->HashTable[$bucket]; $this->HashTable[$bucket] = array(); array_push($this->HashTable[$bucket], $tmp); array_push($this->HashTable[$bucket], $tmp_array); } } } public function delete($string, $attrname, $attrvalue) { $bucket = $this->generate_bucket($string); if(is_null($this->HashTable[$bucket])) { return -1; } else { if(is_array($this->HashTable[$bucket])) { for($x = 0; $x <= sizeof($this->HashTable[$bucket]); $x++) { if(($this->HashTable[$bucket][$x]['string'] == $string) && ($this->HashTable[$bucket][$x]['.$attrname.'] == $attrvalue)) { unset($this->HashTable[$bucket][$x]); } } } else { unset($this->HashTable[$bucket][$x]); } } /** everything is OK **/ return 0; } public function search($string) { $resultArray = array(); $bucket = $this->generate_bucket($string); if(is_null($this->HashTable[$bucket])) { return -1; } else { if(is_array($this->HashTable[$bucket])) { for($x = 0; $x <= sizeof($this->HashTable[$bucket]); $x++) { if(strcmp($this->HashTable[$bucket][$x]['string'], $string) == 0) { array_push($resultArray,$this->HashTable[$bucket][$x]); } } } else { array_push($resultArray,$this->HashTable[$bucket]); } } return($resultArray); } } $hash = new HashTable(16); $arr = array('nombre' => "jorge niedbalski"); $hash->add("astroza", $arr); $hash->add("astrozas", $arr); print_r($hash->search("astroza")); ?> 
+2
source

Do you mean the hash value (which you store in the table) and not the hash table?

I do not see how you could store this data in a useful form, in a hash table. (Suroots answer explains hash tables).

To create a hash value using MD5 try

hash ('md5', 'string to hash');

see http://au.php.net/function.hash for more details

+1
source

roa3

This code does not use a persistent data warehouse server, you can simply expand it to add mysql support.

You must implement this using the one_to_many relation (bucket, records) in the relational database.

Think about how to extend this base class.

Good luck.

+1
source

Source: https://habr.com/ru/post/893755/


All Articles