在PHP和Mysql中实现哈希表的步骤 [英] Steps in implementing hashtable in PHP and Mysql

查看:67
本文介绍了在PHP和Mysql中实现哈希表的步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程语言的新手,并且正在使用PHP和mysql.我有一个任务要在php中做一个哈希表.我需要做的是,存储用户收集的项目,然后显示它.在互联网上进行了一些研究之后,在实现哈希表时,我将执行以下步骤,如果我输入错了,请纠正我:

I am new to programming language and I am using PHP and mysql. I got an assignment to do a hashtables in php. What I need to do is, store items that a user collected and then display it. After do some research over the internet, I will do the following steps when implement the hashtable, please correct me if I am wrong:

  1. 设置表格:

  1. Set up the tables:

->用户表:uid(int [5]),用户名(varchar [128]),item_id(int [8],item_id_hash(int [50])

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

->项目表:item_id(int [5]),item_name(varchar [128]),item_id_hash(int [50])

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

创建一个哈希函数(如何创建哈希函数?由我自己创建还是从Internet获取?),以将密钥转换为哈希值,然后插入数据库.例如:将item_id = 001哈希值哈希值=(例如)12345.然后插入用户表.

Create a hash function (how to create a hash function? Create by myself or get from internet?) to convert a key into a hash value and then insert into database. E.g.: hash item_id = 001 into hash value = (e.g) 12345. Then insert into users table.

显示/搜索.从用户检索哈希值,然后将其与items表进行比较并显示出来.

To display/search. Retrieve the hash values from the user and then compare it to the items table and display it.

问题:

  1. 我的步骤正确吗?
  2. 在哪里可以找到良好的PHP哈希函数?我可以使用md5或sha1或盐吗?

推荐答案

我认为您对哈希表的想法有些[已废止].哈希表将键分解为相似的列表.例如:基于名称的第一个字母的哈希表,因此将有26个列表.您的哈希是名称的第一个字母,这样可以更快地进行搜索.

I think your idea of a hashtable is a little [defunct]. Hashtables break down keys into lists that are alike. For example: hashtable based on first letter of name, so there would be 26 lists. Your hash is the first letter of the name, which then makes it quicker to search through.

md5,sha1用于派生散列,这些散列用于验证数据是否被篡改.它们通常以128位或160位版本提供.因此,它需要X数据,并通过散列将其发送出去,以得出128位的字母数字字符串,无论在何处完成,该字符串都应该相同.这通常是安全的事情.

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

编辑:扩展有关如何派生密钥的问题.

Expanding on Question of how to derive keys.

您可以利用数据的模数来创建用于行的键.在示例数据%X中,其中X是您想要的键总数.问题是X很难找到.如果您有20个项目,那么将X设为20是可行的,并且可以快速搜索,因为每个项目都有自己的行.但是,如果您有1000个项目,那么执行%1000是不可行的.为此,执行X = 75之类的操作会更好.

You can utilize a modulus of the data to create a key to use for the row. In the example data % X where X is the total number of keys you would like to have. The issue with this is that X is difficult to find; if you have 20 items, then making X into 20 is feasible and makes it a quick search as each item has it's own row. But if you have 1000 items, then doing % 1000 is NOT feasible. Doing something like X = 75 would work better for this.

这篇关于在PHP和Mysql中实现哈希表的步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆