質問

I am working on my website (it's sort of a practice project) and I am trying to create a line on the home page that displays the User with the most posts on the Forums. The Forum is a MyBB forum, so everything is very easy to look at. I have the general idea of how to query information from SQL, and have even gotten the posts a few times, but getting the person with the most posts username has eluded me. Here is my current code:

<?php

mysql_connect("host", "username", "pass") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
echo "Top User: ", SELECT username FROM `mybb_users` ORDER BY `mybb_users`.`postnum` DESC LIMIT 0, 30 ;
echo "Users Posts: ", SELECT MAX(postnum) FROM  `mybb_users` or die(mysql_error());

?>

Now, I have been messing with this for an hour or so, I am a noob, so I have finally come here to ask of y'alls help. I used to have the "mysql_query" prefix in front of the queries but they're removed here as a last attempt. I'm trying to get it to be very simple and look like this:

Top User: John Doe

Posts: 2313

I like figuring things out for myself, and I see coding as a fun thing to do, but this is out of my league. I looked around but my feeble mind could not handle the other answers. I'm not sure what else to say other than thank you for your help!

役に立ちましたか?

解決

Should be as simple as

 SELECT username, postnum FROM mybb_user ORDER BY postnum DESC LIMIT 1

or

SELECT username, count(username) as postCount FROM mybb_posts GROUP BY username ORDER BY postCount DESC limit 1

NOTE Do not use mysql* extension At very least use mysqli, but preferably PDO I am just showing it to you since that what you are using

    $result = mysql_query('SELECT username, postnum FROM mybb_user ORDER BY postnum DESC LIMIT 1');
    $row = mysql_fetch_assoc($result)
    echo $row['username'] . $row['postnum'];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top