MYSQL Help Needed

bl4zed

Newbie
Joined
Aug 28, 2009
Messages
13
Reaction score
0
Im working on a project right now but cant seem to figure this out. So heres what I want it to do, User goes to video.php, it checks if they already seen the video, and here where i need help, how do i get a new video that they have not seen. Thanks for the help.
 
The easiest way is to use cookies, E.g. Store the ID of the video in a cookie then create an SQL query:
PHP:
<?php
$custom = "";
if(isset($_COOKIE['watched'])){
	$cookies = explode(",",$_COOKIE['watched']);
	foreach($cookies as $cookie){
		$custom.=" AND videoid!'=".."'";
		}
	$sql = mysql_query("SELECT * FROM videos WHERE ".$custom." LIMIT 1");
	if(mysql_num_rows($sql)>1){
		$fetch = mysql_fetch_array($sql);
		// Send user to other video
		header("location:http://yoursite.com/videos/".$fetch['videoid'].".html");
		exit;
		}
	}
?>
 
Is there a way to do it without cookies and just with mysql?
 
Are the users registered members?

Are you talking about videos specific to users?
 
Is there a way to do it without cookies and just with mysql?

Yes. But to do this you would only be able to determine members by IP address (unless you already have a members DB) which isn't good for users sharing the same connection. Not to mention that this would make your database far too cluttered/bloated for no reason.
 
Last edited:
have int primary keys for both videos & registered users
create a memory table with two columns: user_id, video_id
Code:
CREATE TABLE user_view_history ( user_id int(10) unsigned DEFAULT NULL, video_id int(10) unsigned DEFAULT NULL, UNIQUE KEY uid_vid (user_id,video_id) ) ENGINE= MEMORY
when a user watches a video do
Code:
INSERT IGNORE INTO user_view_history SET user_id = ..., video_id=...
to retrieve unwatched videos
Code:
SELECT videos.id FROM videos, user_view_history WHERE user_view_history.user_id != ' current user id '  AND videos.id = user_view_history.video_id ORDER BY RAND() LIMIT 1

order by rand shuffles order but trashes performance so make sure you don't select *; do another query instead
 
Back
Top