Jump to content

Problem with joins


gurpreet94

Recommended Posts

I have created a file with this basic structure in the referals table:

refREFER = the referer, person who sent the referal link out

refREFED = the refered person, the one who signed up with the link

refREFERIP = the referer's IP

refREFEDIP = the refered person's IP

 

I'm trying to create a code that will list any user that has signed up on the same IP with a referal link. Everything works except the Refered column, which I can't seem to figure out how to do. Basically I have made the refREFER = the userid in the users table, so it can be output as a username. However I want to do the same with the refREFED but don't know how. Here's my code so far:

 

<?php
include "globals.php";
if($ir['user_level'] != 2 && $ir['user_level'] != 3) //User level check
{
    die("You can't access this page");
}

$q=mysql_query("SELECT u.userid, u.username, u.laston, r.* 
			FROM users u 
			LEFT JOIN referals r	ON r.refREFER=u.userid 
			WHERE r.refREFERIP=r.refREFEDIP 
			ORDER BY userid ASC",$c); //Selecting everything that's needed

print "<center><br /><b><font color=white><h1>Referral Multis</h1></b><br />

<table width=75% border=1>
<tr style='background:black'>
<th><font color=grey>Referer</font></th>
<th><font color=grey>Refered</font></th>
<th><font color=grey>IP</font></th> 
<th><font color=grey>Time</font></th>
</tr>";
while(($r=mysql_fetch_array($q) or die(mysql_error()))) //Making all of the select stuff into $r
{
$reftime = date('F j, Y g:i:s a', $r['refTIME']);
if($r['laston'] >= time()-60*60) { $on="<font color=green><b>Online</b></font>"; } else { $on="<font color=red><b>Offline</b></font>"; }
print "
<tr> 
<td><a href='viewuser.php?u={$r['userid']}'><font color=brown><b>{$r['username']}</b></font></a> [{$r['refREFER']}]</td>
<td> {$r['username']} - {$r['refREFED']}</td>
<td>{$r['refREFERIP']}</td>
<td>$reftime</td>
</tr>";
}
print "</table>";

$h->endpage();

?>

 

At the moment under the Refered column it shows the wrong username, but the correct ID. Any ideas?

Link to comment
Share on other sites

Is the issue that you want to look up 2 usernames with the one query?  You can join the users table twice like this:

 

SELECT u.userid, u.username, u.laston,
  u2.userid AS u2userid, u2.username AS u2username, u2.laston AS u2laston,
  r.*
FROM referals r
JOIN users u ON u.userid = r.refREFER
JOIN users u2 ON u2.userid = r.refREFED 
			WHERE r.refREFERIP=r.refREFEDIP 
			ORDER BY u.userid ASC

 

I've made it a JOIN because I don't understand why you used a LEFT JOIN there.  If you do have a good reason for doing so then you can add it back in.

Link to comment
Share on other sites

I'm going to take a stab at this, even though I am a little foggy on the database structure.

 

"SELECT 
       u.userid, u.username, u.laston, n.userid AS referredid, n.username AS referredusername, n.laston AS referredlaston, r.* 
FROM
       users AS u 
JOIN 
       referals AS r
JOIN
      users AS n
ON 
      r.refREFER=u.userid 
AND
     r.refREFED=n.userid
WHERE 
     r.refREFERIP=r.refREFEDIP 
ORDER BY 
   userid"

 

JOIN each and every row in the first table is joined to each and every row in the second table.

If there is no matching row for the right table in the ON or USING part in a LEFT JOIN, a row with all columns set to NULL is used for the right table.

Link to comment
Share on other sites

Neither of those worked. Here is my table structure:

 

Users: userid, username, lastip, and more

referals: refID (auto_increment), refREFER, refREFED, refTIME, refREFERIP, refREFEDIP, refCREDITED

 

I want it so that under the Referer column on the page it has the username of the refREFER, and under the column of Refered it has the username of the refREFED, as long as they are both the same IP - refREFEDIP

 

Link to comment
Share on other sites

With both of the above queries, under the column Refered, the referer's username came up, but the refered ID is correct. Also, the list is limited to 1, even though I have set up 3 entries with the same IP's. Here's an example:

 

 

refID  refREFER refREFED                 refTIME refREFERIP refREFEDIP refCREDITED

37         2 7 1193519079 127.0.0.1 127.0.0.1          0

 

This is displayed as:

 

Referer Refered IP         Time

Test [2] Test - 7 127.0.0.1 October 27, 2007 9:04:39 pm

 

However, ID 7 belongs to Klikoka

Link to comment
Share on other sites

Oh right, I didn't spot you done that. That works perfectly now, however it is still only loading up 1 record. There are a few in my database with the same IP, but it's only showing the first one. If I change the order from ASC to DESC it will show the same one ando nly one. I have tried limit but that doesn't work either

 

If I run this:

 

$q2=mysql_num_rows($q);

echo "$q2";

 

It prints out the number 1.

Link to comment
Share on other sites

Do all the entires with the same IP have matching records for both refREFER and refREFED in the users table?  By default, a join will ignore anything which doesn't match in all tables.

 

If that is the case, use a LEFT JOIN for both joins with the users table.  You won't get a username though because there isn't one, you'll just get a user id.

Link to comment
Share on other sites

I've been trying to add a bit more to this script, to make it so that if someone has referred more than 1 person, their name will come up in a table and a number showing how many referrals they have. I can't get the count function working properly as I've never really used it. Any help would be great

 

<?php
include "globals.php";
if($ir['user_level'] != 2 && $ir['user_level'] != 3) //User level check
{
    die("You can't access this page");
}

$q=mysql_query("SELECT u.userid, u.username, u.laston,
  u2.userid AS u2userid, u2.username AS u2username, u2.laston AS u2laston,
  r.*
FROM referals r
JOIN users u ON u.userid = r.refREFER
JOIN users u2 ON u2.userid = r.refREFED 
			WHERE r.refREFERIP=r.refREFEDIP 
			ORDER BY u.userid ASC"); //Selecting everything that's needed

print "<center><br /><b><font color=white><h1>Referral Multis</h1></b><br />

<table width=75% border=1>
<tr style='background:black'>
<th><font color=grey>Referer</font></th>
<th><font color=grey>Refered</font></th>
<th><font color=grey>IP</font></th> 
<th><font color=grey>Time</font></th>
</tr>";
while($r=mysql_fetch_array($q)) //Making all of the select stuff into $r
{
$reftime = date('F j, Y g:i:s a', $r['refTIME']);
if($r['laston'] >= time()-60*60) { $on="<font color=green><b>Online</b></font>"; } else { $on="<font color=red><b>Offline</b></font>"; }
print "
<tr> 
<td><a href='viewuser.php?u={$r['userid']}'><font color=brown><b>{$r['username']}</b></font></a> [{$r['refREFER']}]</td>
<td><a href='viewuser.php?u={$r['u2userid']}'><font color=brown><b>{$r['u2username']}</b></font></a> [{$r['refREFED']}]</td>
<td>{$r['refREFERIP']}</td>
<td>$reftime</td>
</tr>";
}
print "</table>";



$q2=mysql_query("SELECT u.userid, u.username, r.refREFERIP, r.refREFER, COUNT(*)
FROM referals r
JOIN users u ON u.userid = r.refREFER
			GROUP BY r.refREFER
			ORDER BY u.userid ASC") or die(mysql_error()); //Selecting everything that's needed for the 2nd part

print "<center><br /><b><font color=white><h1>Multiple referals (check these)</h1></b><br />

<table width=75% border=1>
<tr style='background:black'>
<th><font color=grey>Refered</font></th>
<th><font color=grey>IP</font></th> 
<th><font color=grey>No. of referrals</font></th> 
<th><font color=grey>Time</font></th>
</tr>";

while($r2=mysql_fetch_array($q2)) //Making all of the select stuff into $r
{
$reftime = date('F j, Y g:i:s a', $r['refTIME']);


$num_rows = mysql_num_rows($q2); 


if($r['laston'] >= time()-60*60) { $on="<font color=green><b>Online</b></font>"; } else { $on="<font color=red><b>Offline</b></font>"; }
print "
<tr> 
<td><a href='viewuser.php?u={$r2['userid']}'><font color=brown><b>{$r2['username']}</b></font></a> [{$r2['userid']}]</td>
<td>{$r2['refREFERIP']}</td>
<td>count({$r2['refREFER']})</td>
<td>$reftime</td>
</tr>";
}
print "</table>";

echo "$num_rows";
$h->endpage();

?>

 

The problem parts:

$q2=mysql_query("SELECT u.userid, u.username, r.refREFERIP, r.refREFER, COUNT(*)


<td>count({$r2['refREFER']})</td>

The echo "$num_rows" shows the number 11 at the bottom, but that's how many users have more than 1 referral. Would anyone like to provide any help so the number of referrals column could show how many referrals that user has? Thanks in advance

Link to comment
Share on other sites

The count will be named "count" by default, and can be accessed as $r2['count'].  But you can also rename it, eg:

 

$q2=mysql_query("SELECT u.userid, u.username, r.refREFERIP, r.refREFER, COUNT(*) AS referred_count

...

<td>{$r2['referred_count']}</td>

 

There's no need to use php's count() as MySQL has already counted the rows for you.

Link to comment
Share on other sites

The count(*) is based on the group by.  So if you group by r.refREFER, count(*) will be the number of result for each r.refREFER.

 

If the question is "How results are there for each X", then the solution is "SELECT X, count(*) FROM table GROUP BY X"

 

If the question is "What is the total of Y for each X", then the solution is "SELECT X, sum(Y) FROM table GROUP BY X"

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.