使用explode、split或preg_split来存储和获取多个数据库条目

发布于 2024-10-26 06:52:53 字数 384 浏览 2 评论 0原文

我试图弄清楚如何以及哪种方式最适合在数据库中存储和获取多个条目。使用explodesplitpreg_split。我需要实现的是,用户使用表单中的文本字段向不同用户发送多条消息,或者通过输入 ID(如“101,102,103”)与多个用户共享数据,并且 PHP 代码足够智能,可以通过以下方式获取每个 ID:在“,”之后分别选择它们。我知道这要求很高,但我需要这方面更熟练的人的帮助。我需要知道如何让 PHP 代码获取 ID 并能够使用它们的函数。就像从数据库单元格中抓取“101,102,103”并使用从该字符串中抓取的 ID 来抓取数据库中不同的存储信息一样。

我怎样才能实现这个目标?例子将会非常有帮助。

谢谢

I'm trying to figure out how and which is best for storing and getting multiple entries into and from a database. Either using explode, split, or preg_split. What I need to achieve is a user using a text field in a form to either send multiple messages to different users or sharing data with multiple users by enter their IDs like "101,102,103" and the PHP code to be smart enough to grab each ID by picking them each after the ",". I know this is asking a lot, but I need help from people more skilled in this area. I need to know how to make the PHP code grab IDs and be able to use functions with them. Like grabbing "101,102,103" from a database cell and grabbing different stored information in the database using the IDs grabbed from that one string.

How can I achieve this? Example will be very helpful.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏见 2024-11-02 06:52:53

如果我正确理解你的问题,如果你正在处理以逗号分隔的 ID 号字符串,那么将它们保留为这种格式可能是最简单的。原因是因为您可以在查询数据库时在 SQL 语句中使用它。

我假设您想要运行 SELECT 查询来获取已输入 ID 的用户,对吗?您需要使用 SELECT ... WHERE IN ... 类型的语句,如下所示:

// Get the ids the user submitted
$ids = $_POST['ids'];
// perform some sanitizing of $ids here to make sure 
// you're not vulnerable to an SQL injection
$sql = "SELECT * FROM users WHERE ID IN ($ids)";
// execute your SQL statement

或者,您可以使用 explode 创建每个单独的数组ID,然后循环遍历,以便您可以对每个值进行一些检查以确保其正确,然后使用 implode 将它们重新连接成一个可以在 SELECT 中使用的字符串。 .. WHERE IN ... 语句。

编辑:抱歉,忘记添加:就在数据库中存储用户 ID 列表而言,您可以考虑将逗号分隔列表存储为针对消息 ID 的字符串,但这有缺点(如果需要的话,很难在其他表上进行连接)。或者,更好的选择是创建一个查找类型表,该表基本上由两列组成:messageiduserid。然后,您可以根据 messageid 存储每个单独的 userid,例如,

messageid | userid
1 | 1
1 | 3
1 | 5

这种方法的好处是您可以使用此表来连接其他表(也许您有一个单独的 >message 表存储消息本身的详细信息)。

在此方法下,您将在消息表中创建一个新条目,获取 id,然后将 userids 字符串分解为单独的部分,最后创建 INSERT 语句以使用个人 ID 和消息 ID。您需要制定其他机制来处理消息用户 ID 列表的任何编辑以及删除。

希望这是有道理的!

If I understand your question correctly, if you're dealing with comma delimited strings of ID numbers, it would probably be simplest to keep them in this format. The reason is because you could use it in your SQL statement when querying the database.

I'm assuming that you want to run a SELECT query to grab the users whose IDs have been entered, correct? You'd want to use a SELECT ... WHERE IN ... type of statement, like this:

// Get the ids the user submitted
$ids = $_POST['ids'];
// perform some sanitizing of $ids here to make sure 
// you're not vulnerable to an SQL injection
$sql = "SELECT * FROM users WHERE ID IN ($ids)";
// execute your SQL statement

Alternatively, you could use explode to create an array of each individual ID, and then loop through so you could do some checking on each value to make sure it's correct, before using implode to concatenate them back together into a string that you can use in your SELECT ... WHERE IN ... statement.

Edit: Sorry, forgot to add: in terms of storing the list of user ids in the database, you could consider either storing the comma delimited list as a string against a message id, but that has drawbacks (difficult to do JOINS on other tables if you needed to). Alternatively, the better option would be to create a lookup type table, which basically consists of two columns: messageid, userid. You could then store each individual userid against the messageid e.g.

messageid | userid
1 | 1
1 | 3
1 | 5

The benefit of this approach is that you can then use this table to join other tables (maybe you have a separate message table that stores details of the message itself).

Under this method, you'd create a new entry in the message table, get the id back, then explode the userids string into its separate parts, and finally create your INSERT statement to insert the data using the individual ids and the message id. You'd need to work out other mechanisms to handle any editing of the list of userids for a message, and deletion as well.

Hope that made sense!

萌化 2024-11-02 06:52:53

好吧,考虑一下您建议的三个函数:

Then : **do not store several values in a single database column** : it'll be impossible to do any kind of useful work with that !

创建一个不同的表来存储这些数据,每行一个值 - 有几行对应于第一个表中的一行。

Well, considering the three functions you suggested :

  • explode() will work fine if you have a simple pattern that's always the same.
    • For instance, always ', ', but never ','
  • split() uses POSIX regex -- which are deprecated -- and should not be used anymore.
  • preg_split() uses a regex as pattern ; and, so, will accept more situations than explode().

Then : **do not store several values in a single database column** : it'll be impossible to do any kind of useful work with that !

Create a different table to store those data, with a single value per row -- having several rows corresponding to one line in the first table.

野心澎湃 2024-11-02 06:52:53

我认为你的问题更多地与 SQL 相关,而不是与 PHP 相关。

从技术上讲,您可以将 ids 存储到单个 MySQL 字段中的“set”字段中,并使用 IN 或 FIND_IN_SET 在您的条件中。查找实际上非常快,但这并不被认为是最佳实践,并且会创建非规范化的数据库。

什么是嵌套实践,并且标准化的是创建单独的关系表。因此,使用您的消息示例,您可能会有一个“用户”表、一个“消息”表和一个用于在用户之间关联消息的“users_messages”表。 “messages”表将包含消息信息,并且可能包含原始发件人的“user_id”字段(因为只能有一个),而“users_messages”表将仅包含“user_id”和“message_id”字段,其中包含将消息链接到其所属的各个用户的行。然后您只需要使用 JOIN 查询来检索数据,因此如果您要检索用户的收件箱,查询将如下所示:

SELECT
    messages.*
FROM
    messages
    LEFT JOIN users_messages ON users_messages.message_id = messages.message_id
WHERE
    users_messages.user_id = '(some user id)'

I think your problem is more with SQL than with PHP.

Technically you could store ids into a single MySQL field, in a 'set' field and query against it by using IN or FIND_IN_SET in your conditions. The lookups are actually super fast, but this is not considered best practice and creates a de-normalized database.

What is nest practice, and normalized, is to create separate relationship tables. So, using your example of messages, you would probably have a 'users' table, a 'messages' table, and a 'users_messages' table for relating messages between users. The 'messages' table would contain the message information and maybe a 'user_id' field for the original sender (since there can only be one), and the 'users_messages' table would simply contain a 'user_id' and 'message_id' field, containing rows linking messages to the various users they belong to. Then you just need to use JOIN queries to retrieve the data, so if you were retrieving a user's inbox, a query would look something like this:

SELECT
    messages.*
FROM
    messages
    LEFT JOIN users_messages ON users_messages.message_id = messages.message_id
WHERE
    users_messages.user_id = '(some user id)'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文