Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

mysql - PHP explode data to be used in WHERE clause

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

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.

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.

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

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

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

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?

For ex. something like SELECT * FROM user_data WHERE user_status != '".$something."'.

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.

Is it possible something like this?

$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());

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...