PHP 爆炸要在 WHERE 子句中使用的数据 [英] PHP explode data to be used in WHERE clause

查看:44
本文介绍了PHP 爆炸要在 WHERE 子句中使用的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个需要维护用户特定内容的项目.

I am working on a project where I need to maintain user specific content.

一个用户在 24 小时内只能查看一项内容一次.24 小时后,它将再次提供给同一用户.

One user can view one content only one time in 24 hours. After 24 hours it will be made available to the same user again.

因此,当用户成功查看内容时,我会将用户的用户名以逗号分隔值存储在数据库中.

So when a user successfully views a content I store the username of the user in the database in a comma separated value.

例如:当用户 user1 成功查看内容时,他的用户名将存储在 user_status 中作为 user1 以及其他用户喜欢 user1,user2,user3,.......等.

For example: when the user user1 successfully views a content his username will stored in user_statusas user1 along with other users likes user1,user2,user3,.......,etc.

现在我想要的是检查数据库中是否存在用户名 user1 的内容.

Now what I want is to check the content if the username user1 exists in the database or not.

如果存在,那么他将在接下来的 24 小时内无法使用内容.

If it exists then content will not be made available to him for next 24 hours.

但由于某些原因,我无法使用 if...else 从逗号分隔的用户名组中检查特定用户.

But for some reasons I cannot check a particular user from the group of comma separated usernames using if...else.

我可以用 if...else 做到这一点并爆炸,但这不能满足我的需要.我想知道我可以在 SELECT 语句的 WHERE 子句中检查一个用户名吗?

I can do it with if...else and explode but that does not satisfies my need. I want to know that can I check for one username in the WHERE clause of SELECT statement?

例如.类似于 SELECT * FROM user_data WHERE user_status != '".$something."'.

是否可以通过先使用爆炸然后在 WHERE 子句中检查它或仅通过其他方法进行检查?如果是,那么如何?请php高手帮帮我.

Is it possible by using explode first then checking it in WHERE clause or just by some other method? If yes, then how? Please help me php experts.

有这种可能吗?

$uname = $_SESSION['username'];//username of the logged in user

$ths = "SELECT * FROM user_data";
        $its = mysql_query($ths) or die(mysql_error());
        $uhs = mysql_fetch_array($its);

                $user_status = explode(',', $uhs['user_status']);// first explode the comma separated usernames

        $ths = "SELECT * FROM user_data WHERE user_status != '".$uname."'";// then check if the logged in username does not exists in the database
        $its = mysql_query($ths) or die(mysql_error());

可以在 WHERE 子句中检查用户名的爆炸列表吗?是否可以?或者有什么方法可以做到这一点?如果是,那么如何?

Can the exploded list of usernames be checked in the WHERE clause? Is it possible? Or is there any method to do this? If yes then how?

推荐答案

我认为您的看法完全错误.对于一个非常简单的任务来说,这是大量的维护工作.你可以有另一个表,在那里你保留用户 id &数据 ID 与时间戳一起配对.

I think you are looking at it all wrong. That is a lot of maintenance for a very simple task. You could just have another table, where you keep user id & data id pairs along side with a timestamp.

例如,对于用户数据 1 和用户 1,您将保存:

For example, for user data 1 and user 1 you will save:

uid | did | ts
--------------------
  1 |   1 | TIMESTAMP

要检查用户是否观看了某个项目,只需使用用户 ID 和数据 ID 从该表中进行选择.如果没有找到行,或者当前时间戳减去行时间戳超过 24 小时,则允许用户查看数据,否则 - 不允许.

To check if a user watched an item, just select from that table using the user id and data id. If no row is found, or the the current timestamp minus the row timestamp is more than 24 hours, allow the user to view the data, otherwise - don't allow it.

这篇关于PHP 爆炸要在 WHERE 子句中使用的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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