boionfire81 Posted May 11, 2016 Share Posted May 11, 2016 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. Quote Link to comment Share on other sites More sharing options...
KyleMassacre Posted May 11, 2016 Share Posted May 11, 2016 It sounds like your trying to print an array instead of a string. Are you printing $attacker['username'] or just $attacker? If it's the latter, don't. Use the first option Quote Link to comment Share on other sites More sharing options...
boionfire81 Posted May 11, 2016 Author Share Posted May 11, 2016 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. Quote Link to comment Share on other sites More sharing options...
KyleMassacre Posted May 11, 2016 Share Posted May 11, 2016 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 } Quote Link to comment Share on other sites More sharing options...
boionfire81 Posted May 11, 2016 Author Share Posted May 11, 2016 cool. Thanks! What do I do about it only displaying the first attack? Quote Link to comment Share on other sites More sharing options...
KyleMassacre Posted May 11, 2016 Share Posted May 11, 2016 The while loop will fix that. Quote Link to comment Share on other sites More sharing options...
boionfire81 Posted May 11, 2016 Author Share Posted May 11, 2016 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 Quote Link to comment Share on other sites More sharing options...
TheMasterGeneral Posted May 12, 2016 Share Posted May 12, 2016 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. Quote Link to comment Share on other sites More sharing options...
boionfire81 Posted May 13, 2016 Author Share Posted May 13, 2016 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. Quote Link to comment Share on other sites More sharing options...
AdamHull Posted May 14, 2016 Share Posted May 14, 2016 (edited) $z=$db->query("SELECT `username` FROM `users` WHERE `userid` = {$a['attacked']}"); while($defender=$db->fetch_row($z)){ echo $defender['username']; } See if that works Edited May 14, 2016 by adamhull Quote Link to comment Share on other sites More sharing options...
boionfire81 Posted May 14, 2016 Author Share Posted May 14, 2016 $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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.