Jump to content
MakeWebGames

ok how do I query this?


Recommended Posts

I'm trying to run an attack log and print it on the members profile so donators can see the attack logs between them and the player who's profile they are viewing.

This is my query right now

 

$attacks=$db->query("SELECT * FROM `attacklogs` WHERE `attacker` = {$r['userid']} OR `attacked` = {$r['userid']} AND `attacker` = {$ir['userid']} OR `attacked` = {$ir['userid']}");
$a = $db->fetch_row($attacks);
$att=$db->query("SELECT `username` FROM `users` WHERE `userid` = {$a['attacker']}");
$attacker=$db->fetch_row($att);
$d=$db->query("SELECT `username` FROM `users` WHERE `userid` = {$a['attacked']}");
$defender=$db->fetch_row($d);

 

This is what I get when I try to display the usernames ($defender/$attacker) Array to string conversion fail.

Link to comment
Share on other sites

Ok great! thanks. So this is what I have

 

$attacks=$db->query("SELECT * FROM `attacklogs` WHERE `attacker` = {$r['userid']} OR `attacked` = {$r['userid']} AND `attacker` = {$ir['userid']} OR `attacked` = {$ir['userid']} LIMIT 5");
$a = $db->fetch_row($attacks);
$att=$db->query("SELECT `username` FROM `users` WHERE `userid` = {$a['attacker']}");
$attacker=$db->fetch_row($att);
$d=$db->query("SELECT `username` FROM `users` WHERE `userid` = {$a['attacked']}");
$defender=$db->fetch_row($d);
print "<b>Attack Logs</b>


<center><table width=50% cellspacing=1 class='table' border='1' bordercolor='#636363'></center>
<tr>
<th>Time</th>
<th>Attacker</th>
<th>Defender</th>
<th>Result</th>
</tr>";
$d=date('F j, Y, g:i:s a',$a['time']);
print "<tr>
<td><center>$d</td>
<td><a href='viewuser.php?u={$a['attacker']}'>{$attacker['username']}</a></td>
<td><a href='viewuser.php?u={$a['attacked']}'>{$defender['username']}</a></td>
<td>{$attacker['username']} {$a['result']}</td>
</tr>";
print "</table> 

 

Only 2 issues. 1 it only displays 1 result, which I believe is the first attack, as todays attack didn't appear. And 2 if there are no attack logs, it gives an error say unable to execute mysql query.

Link to comment
Share on other sites

You need to check if any rows exists and if not then show something else. Then if it does exist you have to loop through them like (pseudo code):

"SELECT * FROM table WHERE parameter = 'this'";
if(query->num_rows())
{
   while($this = $that)
   {
       show my stuff
   }
}
else
{
   there is nothing here
}
Link to comment
Share on other sites

Did that

 

$attacks=$db->query("SELECT * FROM `attacklogs` WHERE `attacker` = {$r['userid']} OR `attacked` = {$r['userid']} AND `attacker` = {$ir['userid']} OR `attacked` = {$ir['userid']}  ORDER BY time ASC LIMIT 5");
if($db->num_rows($attacks)) {
print "<b>Attack Logs</b>


<center><table width='60%' cellspacing='1' class='table' border='1' bordercolor='#636363'></center>
<tr>
<th>Time</th>
<th>Attacker</th>
<th>Defender</th>
<th>Result</th>
</tr>";
$a = $db->fetch_row($attacks);
$att=$db->query("SELECT `username` FROM `users` WHERE `userid` = {$a['attacker']}");
$attacker=$db->fetch_row($att);
$d=$db->query("SELECT `username` FROM `users` WHERE `userid` = {$a['attacked']}");
$defender=$db->fetch_row($d);
while ($a = $db->fetch_row($attacks))
$d=date('n/j/y<br> g:i a',$a['time']);
print "<tr>
<td><center>$d</td>
<td><a href='viewuser.php?u={$a['attacker']}'>{$attacker['username']}</a></td>
<td><a href='viewuser.php?u={$a['attacked']}'>{$defender['username']}</a></td>
<td>{$attacker['username']} {$a['result']}</td>
</tr>";
print "</table>";
}

 

didn't work

Link to comment
Share on other sites

1) If you're just fetching/displaying a single cell from the database, use $db->fetch_single();

2) Are you even using MCCodes built-in error handler? No offense, but I believe some of these issues could be avoided if you did some debugging. (9 out of 10 times, it'll tell you where it failed, which will allow you to revise your code)

3) Also, I'd reassign the date variable to something other than $d, you've already got it assigned to the query to select the defender's name.

If after trying some debugging of your own, and using the error handler, you cannot figure it out, shoot me an IM and I'll do it for you personally. Free of charge.

Link to comment
Share on other sites

Changed the $d to $z

 

$z=$db->query("SELECT `username` FROM `users` WHERE `userid` = {$a['attacked']}");
$defender=$db->fetch_row($z);

 

And yeah the debug is on. That has helped me A LOT as I'm digging deep into the code. But this doesn't have an error it just simply only displays 1 result, not the last 5 results.

Link to comment
Share on other sites

$z=$db->query("SELECT `username` FROM `users` WHERE `userid` = {$a['attacked']}");
while($defender=$db->fetch_row($z)){
echo $defender['username'];
}

See if that works

I don't think that will work cause there are 2 total queries making the 1 view log. 1 in for attacking and 1 for defending.

Full queries (just queries)

 

$attacks=$db->query("SELECT * FROM `attacklogs` WHERE `attacker` = {$r['userid']} OR `attacked` = {$r['userid']} AND `attacker` = {$ir['userid']} OR `attacked` = {$ir['userid']}  ORDER BY time ASC LIMIT 5");
if($db->num_rows($attacks))
$a = $db->fetch_row($attacks);
$att=$db->query("SELECT `username` FROM `users` WHERE `userid` = {$a['attacker']}");
$attacker=$db->fetch_row($att);
$z=$db->query("SELECT `username` FROM `users` WHERE `userid` = {$a['attacked']}");
$defender=$db->fetch_row($z);
while ($a = $db->fetch_row($attacks))
$d=date('n/j/y<br> g:i a',$a['time']);
print "

 

I had to run the attack log query to check both sides of the fight. THEN the username query to pretty it up lol.

Link to comment
Share on other sites

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.   Paste as plain text instead

  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...