Jump to content

Query Help


gawdz

Recommended Posts

Let me give you some insight im doing this to look for compatibility between users.

 

 

This is where i gather all the "ratings" the logged in user did.

 

$myself = "SELECT storyId,username FROM ratings WHERE username<>'Anonymous' AND username='".$session->username."'";
$result = mysql_query($myself);
while ($row = mysql_fetch_array($result)) {
$ratings[] = $row['storyId'];
}

 

Now im going to go through all the users and see how many times they did the exact same thing my logged in user did.

		$sql = "SELECT username FROM users";
		$result = mysql_query($sql);
			while ($row = mysql_fetch_array($result)) {
				foreach ($ratings as $value) {
				$compat = "SELECT id,username FROM ratings WHERE id='".$value."' AND username<>'Anonymous' AND username<>'".$session->username."' AND username='".$row['username']."'";
					$result2 = mysql_query($compat);
						while ($row2 = mysql_fetch_array($result2)) {
							$compatibles[] = $row2['username'];
								}
						}

					}

 

But the thing is it times out because i poorly made the query but im not sure how to improve it.

Link to comment
Share on other sites

If I understand what you are looking to accomplish here is some pseudo code that will help.  Avoid looping through a result set and issueing a query each time.  Instead utilize the IN and NOT IN operators in this case. 

 

$q1 = "SELECT storyId FROM ratings WHERE username='".$session->username."';
// store q1 in an array lets call that $storyArr
$q2 = "SELECT username FROM ratings WHERE username NOT  IN ('$session->username','Anonymous') AND storyId IN (".implode(',',$storyArr).")";
// $q2 is what you need and will have a list of compatible users...

 

 

 

 

Link to comment
Share on other sites

As s0c0 said, you should combine queries whenever possible. You might want to add DISTINCT to the final query in his solution though, otherwise, you will get the same user multiple times (if they have rated more than one story in the list). 

$q2 = "SELECT DISTINCT username FROM ratings 
WHERE username NOT  IN ('$session->username', 'Anonymous') 
AND storyId IN (".implode(',',$storyArr).")";

 

If you are looking for people who have rated ALL (not just ANY) of the same stories as the user, I think this will do it

$q2 = "SELECT username FROM ratings 
WHERE username NOT  IN ('$session->username', 'Anonymous') 
AND storyId IN (".implode(',',$storyArr).")
GROUP BY username HAVING COUNT(*) = " . count($storyArr);

 

Also, just a comment on your original query. It is really NOT necessary to say WHERE username IS NOT Anonymous when you are also saying WHERE username IS (current user). Since username can only be one value, if it IS (current user) is IS NOT anonymous -- unless of course the current user IS anonymous in which case your original query will return no rows.

 

$myself = "SELECT storyId,username FROM ratings 
WHERE username<>'Anonymous' AND username='".$session->username."'";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.