Top 10?

Hello,

After a search of 2hours but ending up with empty hands,
i decided to join phphelp.com.
And i don’t know if this question belongs here, or at the mysql help but…

I finally got my vote counting script to work,
it puts in an ID, username and keeps track how many time someone has voted on that username,
and i want to take this is step further with making an TOP 10 voters page with the stored data in my database.

Here is the problem, i have no clue how to do this.
I’m quite new to mysql in combination with php so…
can someone help me out with this one?

You can just sort and limit using mysql

e.g.

SELECT * FROM `myTable` ORDER BY `myVotesColumn` DESC LIMIT 10;

Ok,
i used your line of code, and set up an connection with my db.
Now how do i actually show the top 10 in the page?

I tried to set the data into a var,
and echo that, but without succes.

Am i missing something,
or is this just not the way to echo these results?

[php]<?
/* MYSQL connectie */
$conn = new Mysqli(“localhost”, “username”, “password”, “tylerwx94_cwvote”) or die ('Error: ’ . mysql_error());

/* mysql selectie */
$results = mysqli_query($conn, “SELECT players FROM tylerwx94_cwvote ORDER BY votes DESC LIMIT 10”);

echo $results;
?>[/php]

You need to fetch & loop your results, for example

[php]
while ($row = mysqli_fetch_assoc($results)) {
echo $row[‘players’] . “\n”;
}
[/php]

i tried your example in my code and tested it,
but it gave me the following warning:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in ...

I’ve looked online, tried different things what solved this problem for others,
but with no luck.

Maybe someone here knows what the problem is?

[php]<?
/* MYSQL connectie */
$conn = new Mysqli(“localhost”, “username”, “password”, “tylerwx94_cwvote”) or die ('Error: ’ . mysql_error());

/* mysql selectie */
$results = mysqli_query($conn, “SELECT players FROM tylerwx94_cwvote ORDER BY votes DESC LIMIT 10”);

/*Test voorbeeld fetch en loop */
while ($row = mysqli_fetch_assoc($results)) {
echo $row[‘players’] . “\n”;

}
?>[/php]

sorry for double post,
but i added a check before the loop, just to see if there might be an error before te loop.
Now it will only show: "Error: " no error message at all behind it?

code:
[php]
/* mysql selectie */
$results = mysqli_query($conn, “SELECT players FROM tylerwx94_cwvote ORDER BY votes DESC LIMIT 10”) or die('Error: ’ . mysql_error());

while ($row = mysqli_fetch_assoc($results)) {
echo $row[‘players’] . “\n”;

}
?>[/php]

You can’t mix mysql_ with mysqli_

If you want to test the connection:

[php]
$conn = @mysqli_connect(“localhost”, “username”, “password”, “tylerwx94_cwvote”);
if (!$conn) {
die('Connect Error: ’ . mysqli_connect_error());
}
[/php]

well i tested the connection just now,
but there isn’t a connection error.

Still:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in...

If mysqli_query() is returning boolean false then your query is failing for some reason.

So try doing…

mysqli_query()or die(mysqli_error())

Sponsor our Newsletter | Privacy Policy | Terms of Service