Login
Username: Password:
Remember me

[Sign Up] / [Recover Password]

Theme / Language

Popular
New Torrents
new name
Size: 18.40 MB
Most active
new name
S: 74 L: 3
New Posters
Peer User ID - Bug
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / Torrent Trader / v3 Support

2020-03-16 02:00:56
We have this bit of code on our torrents-details.php that works great, it resolves peer data very well, but usernames/avatars are completely incorrect. I can't figure this out, but this is our code:


<?
// Updated Stats & table with avatar and cleaner design - Feb 28, 2020
if ($row["external"] != 'yes') {
   echo "<br /><span style='margin-left:-10px'><b>" . T_("PEERS_LIST") . "</b> <small><span style='color:#ED2324'>beta</span></small></b></span><br />";
   $query  = SQL_Query_exec("SELECT * FROM peers WHERE torrent = $id ORDER BY seeder DESC");
   $result = mysql_num_rows($query);
   if ($result == 0) {
       echo "<span style='font-weight:normal;margin-left:-10px'>No active peers on the network</span><br />";
   } elseif ($row["external"] = 'yes' && $CURUSER) {
?>
       <!-- Stats Table Styling -->
       <style>.table_col1, .table_tr { border-top:1px solid #fff; background:#F5F5F5; font-weight: normal; text-align:center; margin-left:-20px } .table_head { font-weight: normal; text-align:left; padding-left:10px }</style>
       <div style="margin-left:-20px;width:100%">
           <table border="0" cellpadding="0" cellspacing="0" width="100%" class="table_table">
           <tr>
               <th class="table_head"><? echo T_("PORT"); ?></th>
               <th class="table_head">UL</th>
               <th class="table_head">DL</th>
               <!--<th class="table_head"><? echo T_("RATIO"); ?></th>-->
               <th class="table_head"><? echo T_("_LEFT_"); ?></th>
               <th class="table_head"><? echo "%"; ?></th>
               <th class="table_head"><? echo T_("SEED"); ?></th>
               <th class="table_head"><? echo T_("CONNECTED_SHORT"); ?></th>
               <th class="table_head"><? echo T_("CLIENT"); ?></th>
               <th class="table_head"><? echo T_("USER_SHORT"); ?></th>
           </tr>

<?
       while ($row1 = mysql_fetch_assoc($query)) {
           if ($row1["downloaded"] > 0) {
               $ratio = $row1["uploaded"] / $row1["downloaded"];
               $ratio = number_format($ratio, 3);
           } else {
               $ratio = "---";
           }
           $percentcomp = sprintf("%.2f", 100 * (1 - ($row1["to_go"] / $row["size"])));
           //if ($site_config["MEMBERSONLY"]) {
             //$res             = SQL_Query_exec("SELECT users.id, users.username, users.avatar, users.privacy FROM users LEFT JOIN peers ON peers.userid = users.id WHERE users.id=" . $row1["userid"]); // Testing, seems to work as-is?
               $res             = SQL_Query_exec("SELECT id, username, avatar, privacy FROM users WHERE id=" . $row1["userid"]);
               $arr             = mysql_fetch_array($res);
               //$arr["username"] = "<a href='/user/?id=$arr[id]'>" . class_user($arr[username]) . "</a>";
               $arr["username"] = "<a href='$site_config[SITEURL]/user/?id=$arr[id]'><img src='$site_config[SITEURL]/$arr[avatar]' style='border-radius:50%;height:45px;width:45px'></a>";
           //}
           if (!$arr["username"])
               //$arr["username"] = "Unknown User";
               $arr["username"] = "<img src='" . $site_config['SITEURL'] . "/images/noavatar.png' style='border-radius:50%;height:45px;width:45px' />";
           if ($arr["privacy"] != "strong" || ($CURUSER["control_panel"] == "yes")) { // If Admin, show the extended details, or else show basic details to everyone else
               print("<tr class='table_tr'><td class='table_col2'>66" . $row1["port"] . "</td><td class='table_col1' style='width:300px'>" . mksize($row1["uploaded"]) . "</td><td class='table_col2' style='width:300px'>" . mksize($row1["downloaded"]) . "</td><td class='table_col1' style='display:none'>" . $ratio . "</td><td class='table_col2' style='width:300px'>" . mksize($row1["to_go"]) . "</td><td class='table_col1'>" . $percentcomp . "%</td><td class='table_col2'>$row1[seeder]</td><td class='table_col1' style='text-transform:capitalize;font-weight:bold'>$row1[connectable]</td><td class='table_col2' style='padding-left:15px;padding-right:15px'>" . htmlspecialchars($row1["client"]) . "</td><td class='table_col1' width='90' align='right'>$arr[username]</td></tr>");
           } else {
               print("<tr><td class='table_col2'>" . $row1["port"] . "</td><td class='table_col1'>" . mksize($row1["uploaded"]) . "</td><td class='table_col2'>" . mksize($row1["downloaded"]) . "</td><td class='table_col1'>" . $ratio . "</td><td class='table_col2'>" . mksize($row1["to_go"]) . "</td><td class='table_col1'>" . $percentcomp . "%</td><td class='table_col2'>$row1[seeder]</td><td class='table_col1'>$row1[connectable]</td><td class='table_col2'>" . htmlspecialchars($row1["client"]) . "</td><td class='table_col1'>Private</td></tr>");
           }
       }
       echo "</table></div>";
   }
}
?>


I've tried playing with

SQL_Query_exec("SELECT id, username, avatar, privacy FROM users WHERE id=" . $row1["userid"]);


And trying available variables, but it only takes $row1. So I went to see about JOINing tables, since the code is taking the long way around to perform this anyway:

$res = SQL_Query_exec("SELECT users.id, users.username, users.avatar, users.privacy FROM users LEFT JOIN peers ON peers.userid = users.id WHERE users.id=" . $row1["userid"]);


Still no luck. Has this little feature even worked for anybody using TorrentTrader? If so, its safe to assume I've done something to announce.php - I just need confirmation on if this was originally a "experimental" feature or not. I can start overwriting files and crossing my fingers but that doesn't play nice with a actively updated 'peers' table and just causing problems.

Seems easier to just write-off this and get rid of it but.. hey the code in there, why not utilize it?

[ATTACH type="full"]5397[/ATTACH]
Users - Bug - Peers.png
Users - Bug - Peers.png
Unknown User
2020-03-16 17:14:13
The logic in this code is flawed. The reason why this is not good is because you are looping sql query, any loop of sql query will result in significant decrease of page load.

Remove this:
 $res             = SQL_Query_exec("SELECT id, username, avatar, privacy FROM users WHERE id=" . $row1["userid"]);
$arr             = mysql_fetch_array($res);


Try like this
$query  = SQL_Query_exec("
SELECT peers.*, users.*
FROM peers
LEFT JOIN users
ON (peers.userid = users.id)
WHERE peers.torrent = $id
ORDER BY peers.seeder DESC
");
2020-03-16 23:59:03
SELECT peers.*, users.* FROM peers LEFT JOIN users ON (peers.userid = users.id) WHERE peers.torrent = $id ORDER BY peers.seeder DESC
2020-03-16 23:59:46
SELECT peers.*, users.* FROM peers LEFT JOIN users ON (peers.userid = users.id) WHERE peers.torrent = $id ORDER BY peers.seeder DESC
2020-03-17 00:03:22
Your query (corrected above) is working like a charm, I commented vars in the loop and directed the rest to $query outside the loop, nice! thnx - this is a optimization though no?
Unknown User
2020-03-17 12:49:46
"STorrents2020, post: 68165, member: 20382":

Your query (corrected above) is working like a charm, I commented vars in the loop and directed the rest to $query outside the loop, nice! thnx - this is a optimization though no?

For readability you should always practice writing it like i did in a new row, thats what you must do anyway if you ever work as web developer, never in single line.

You still  have several hiccups which are bad in this, will fix when I get home

Edit: never user mysqli_num_rows and pdo->rowCount in your code, the way it works it allocates additional memory, loops through whole table, fetches data and locks table.

Always use SELECT COUNT() because with correct index structure it will speed up process for more than 200%

Of course, weight structure of index tables into SQL are wrong, whoever made them, had little to no knowledge of indexes.

1st way:
This requires LEFT JOIN to always return results in order to count data.
$query  = SQL_Query_exec("
SELECT peers.*, users.*, COUNT(peers.id) as 'found_num'
FROM peers
LEFT JOIN users
ON (peers.userid = users.id)
WHERE peers.torrent = $id
ORDER BY peers.seeder DESC
");


2nd way:
This has a better structure and can be customized for your needs when counting
$query  = SQL_Query_exec("
SELECT peers.*, users.*,
(
   SELECT COUNT(peers.id)
   FROM peers
   WHERE peers.torrent = $id
) as 'found_num'
FROM peers
LEFT JOIN users
ON (peers.userid = users.id)
WHERE peers.torrent = $id
ORDER BY peers.seeder DESC
");
2020-03-18 00:18:11
This is the issue, is 6 people of the same picture and ID will be listed for the same torrent ID. This is where this query is a real breaker for TT

The query resolves my ID (1) just fine (thank you) but I'm now trying to resolve my avatar picture (users.avatar) like it does with ID. On the MySQL website, maybe I can dig something up. Right now, it seems to pull a random avatar photo and multiply them across the peer list (since this is unique to the torrent ID it strikes me confused). I've "Force Clean" each test, and even left it alone for a day, it doesn't make a difference.
Unknown User
2020-03-18 13:19:27
Can you query the sql i wrote into cli or phpmyadmin so i can see thw output, then paste it here

It works excellent in my example: (3 users, all of them have 3 different avatars and 3 different names)
[URL unfurl="true"]http://sqlfiddle.com/#!9/0247fa/5[/url]
2020-03-19 01:19:22
{
   "data":
   [
       {
           "name": "Mystic Vale NSP + XCI + UPDATE + 2 DLCs",
           "id": 2653,
           "torrent": 411,
           "peer_id": "-UM1870-«!»pnÒxô",
           "ip": "<removed>",
           "port": 6999,
           "uploaded": 0,
           "downloaded": 0,
           "to_go": "0",
           "seeder": "yes",
           "started": "2020-03-18 08:44:32",
           "last_action": "2020-03-18 11:44:39",
           "connectable": "no",
           "client": "uTorrentMac/1870(43796)",
           "userid": "390",
           "passkey": "2788b2af9d02730",
           "id": 390,
           "username": "casann",
           "password": "<removed>",
           "secret": "BLOB",
           "email": "<removed>@gmail.com",
           "status": "confirmed",
           "added": "2020-02-26 06:36:45",
           "last_login": "2020-03-18 06:25:04",
           "last_access": "2020-03-18 06:25:05",
           "editsecret": "BLOB",
           "privacy": "normal",
           "stylesheet": 1,
           "language": "1",
           "info": null,
           "acceptpms": "yes",
           "ip": "<removed>",
           "class": 1,
           "avatar": "images/avatars/87f017a9acf4b8cd4acb73118147c0cd6fb59db5.jpg",
           "avatar_thumb": "images/avatars/87f017a9acf4b8cd4acb73118147c0cd6fb59db5_thumb.jpg",
           "uploaded": 83236314575,
           "downloaded": 180128153158,
           "title": "",
           "donated": 0,
           "country": 14,
           "notifs": "",
           "enabled": "yes",
           "modcomment": null,
           "gender": "Male",
           "client": "",
           "age": 0,
           "warned": "no",
           "signature": "",
           "last_browse": 1584450115,
           "forumbanned": "no",
           "invited_by": 0,
           "invitees": "",
           "invites": 0,
           "invitedate": "0000-00-00 00:00:00",
           "commentpm": "yes",
           "passkey": "2788b2af9d02730",
           "page": "Viewing Torrent ID 407",
           "team": 0,
           "tzoffset": 0,
           "hideshoutbox": "no",
           "moods": 1,
           "comments": 0,
           "found_num": 1
       }
   ]
}


Results exported to JSON so easy read. Everything is some other user except this is valid for me:

- User-agent
- Port (uTorrent)

All else is another user. I am seeding this torrent. This is where it fails.  Clicking on the avatar is the correct user ID it seems, but the picture is all wrong, and some torrents I'm seeding it shows me perfectly fine.
Unknown User
2020-03-19 01:47:18
Then the problem is not SQL, the problem comes from somewhere else.
2020-03-19 20:48:03
"shadows, post: 68186, member: 20311":

Then the problem is not SQL, the problem comes from somewhere else.


Well I appreciate the time to investigate this very much, I think we may drop it as feature since its as far from accurate as possible.
Users online

No users online

Search



Statistics

    Torrents

  • Tracking: 3
  • Todays torrents: 0
  • Seeders: 0
  • Leechers: 0
  • Peers: 0
  • Downloaded: 89.96 GB
  • Uploaded: 9.86 GB

  • Members

  • We have: 838
  • New today: 0
  • Guest today: 9

  • Online

  • Total online: 1
  • Members: 0
  • Guests online: 1
  • Comments: 3
  • Messages: 1,728