MySQL and PHP Help with the Select Function?
-
I am making a 'Richest Person List' on my Website. This is the Code: <div class="box2"> <p class="chat">Richest Person of the Month!</p> <br></br> <?php // Make a MySQL Connection $host = "CENSORED"; //db host $user = "CENSORED"; // username $pass = "CENSORED"; // password $database = "minecraft"; // forum db $conn = mysql_connect($host,$user,$pass) or die("Connection Denied."); $db = mysql_select_db($database,$conn) or die(mysql_error()); $query = "SELECT username, MAX(balance) FROM iconomy"; $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ echo "The most richest is ". $row['username']. " with $" .$row['MAX(balance)']; echo " "; } ?> </div> Now, my EXPECTED result is: The Most Richest is (Richest Person's name) with $99999. The ACTUAL RESULT is: The Most Richest is (First Person on top of the MySQL Table) with $99999. How do I modify the code so that my EXPECTED result is my Actual Result? Additional Details: $query = "SELECT username, balance FROM iconomy WHERE balance = MAX(balance)"; and echo "The most richest is ". $row['username']. " with $" .$row['balance'] This won't work as it returns me with a 'Invalid use of group function' error.
-
Answer:
use sub query! select username, balance from iconomy where balance = (select max(balance) from iconomy) max() is an aggregate function so read more about aggregate functions to know how to use it correctly. http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
GregoryI... at Yahoo! Answers Visit the source
Related Q & A:
- How To Build Business Directory Using Php Mysql?Best solution by Stack Overflow
- How to select all articles and their similar articles from MySQL?Best solution by Stack Overflow
- How to prevent duplicate entries in MySQL and PHP?Best solution by Stack Overflow
- How to write a query for PHP and MySQL?Best solution by Stack Overflow
- Is it possible to conditionally select columns in MySQL?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.