0

Here is my code:

    <table border="2px solid #FFF" width="100%">
            <tr>
                <td width="50%"><Center>Username:</Center><br /></td>
                <td width="50%"><center>Numebr Of Warnings:</center><br /></td>
            </tr>
            </table>
            <?
            $query = mysql_query("SELECT * FROM warnings");
            $numrows = mysql_num_rows($query);
                if($numrows != "0"){
                   while($row = mysql_fetch_assoc($query)){
                        $warned = $row['username'];
                        $by = $row['by'];
                        $sql = mysql_query("SELECT * FROM warnings WHERE username='$warned'");
                        $warns = mysql_num_rows($sql);
                        ?>
                            <table border="2px solid #FFF" width="100%">
                                <tr>
                                    <td width="50%"><center><b><?php echo $warned; ?></b></center></td>
                                    <td width="50%"><center><b><?php echo $warns; ?></center></b></td>
                                </tr>
                            </table>
                        <?php
                   }
                }
                else
                    echo "No Currently Warned Users";
                ?>

            <hr />

And here is the result: enter image description here

How can I make it so that instead of showing the 2 results for the user Mrg..... I just want it to show one result with the number or rows there are.

Help would be appreciated.

4
  • There is no such thing as a "php table". What you should is a html table. Commented Apr 8, 2014 at 15:53
  • 1
    use group by with a sum function Commented Apr 8, 2014 at 15:55
  • What is the structure of your table? Commented Apr 8, 2014 at 16:01
  • by = varch (255). id = int. username = varchar (255). date = datetime Commented Apr 8, 2014 at 16:02

3 Answers 3

1

You can use DISTINCT in your query to avoid duplicate rows in your results:

SELECT DISTINCT username, `by` FROM warnings
Sign up to request clarification or add additional context in comments.

1 Comment

This added the 2 and the 1 to make three for all users.
0

Change the mysql_fetch_assoc with mysql_feth_array

You're getting the user with double record. Then for each user you are getting 2 lines

The best way to check it is to add pseudo-points to your code.

$query = mysql_query("SELECT * FROM warnings");
$arr=array();
while($row = mysql_fetch_assoc($query))
{
    $arr[]=$row;
}
echo "<pre>";
print_r($arr);    
echo "</pre>";
exit;

Here check if the $arr has double entries. Then change the mysql_fetch_assoc with mysql_fetch_array and try again

6 Comments

Sorry don't follow fully. What about echoing the table?
You can see if your array has double entries from records. I think the mysql_fetch_assoc when you're selecting records from db getting double records. If you have still problem I can help you with teamviewer or anything you need.
Sorry to be a pain... could you write the full code out for me including the table so I can understand and learn from it? Sorry. Thanks
No problem. I'm trying to help people. Please write the table struct here. I'll write the code for you.
by = varch (255). id = int. username = varchar (255). date = datetime. Only by and username are needed :). THANKS!
|
0

Here is the table

(by,id,username,date)

a1,1,u1,date

a2,2,u2,date

a3,3,u2,date

a4,4,u2,date

And here is the php code

<?php
$conn = mysqli_connect("localhost","username","password");
mysqli_select_db($conn,"dbname");

$warns = mysqli_query($conn,"select username, count(username) as warncount  from dtest.warnings W group by username");

while($line = mysqli_fetch_array($warns))
{
    echo $line['username']." has ".$line['warncount']. " warns <br/> ";
}
?>

8 Comments

Thanks but got this error: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/cocpages/public_html/mods.php on line 40. I did change the db connect files
try directly my code. Create a table with the above structre and check. If you need I can show you on my computer. Don't worry I'm not hacker, cracker :)
Didn't mean files I just connected to my DB. Table structure is correct. Might do a Teamviewer in a bit if no solution. Can we do a private chat here? Could you make one? :)
I'm here. I've gone out. Are you there ?
I'm here. Any idea on what we can do? I am thinking of changing the table so that instead of inserting a new row it updates the current one to add 1 to the current value if the username exists in the table
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.