1

I am working on a leaderboard. The leaderboard gets data from my SQL Database. It is using the old method of MySQL because I'm not familiar with MySQLi yet.

I don't want to add a row everytime, so I'm using a php method to repeat it. But I got a issue with it. It is returning nothing what I've put in the echo.

This is my complete code: https://gist.github.com/matthijs110/accb1b0b69570c4d3f50

It looks fine to me, and I can't find any errors. The result of the current code looks like this:

http://puu.sh/8HP64.png

About the warning: I don't know why and how it came there, It says the problem is this line:

if (mysql_num_rows($result)) {

I've used that if check multiple times on other pages without any issues.

What is wrong?

6
  • 1
    It seems you don't have a working query. Perhaps TABLE_NAME should be a variable instead of a constant? Commented May 11, 2014 at 9:10
  • I would suggest you try it without the funk around the table name: SELECT Username, Wins FROM theTableName ORDER BY Wins DESC and see how you go. Commented May 11, 2014 at 9:14
  • @Fluffeh Edited the post, forgot to add the Database connection code. And I've done that multiple times :/ Commented May 11, 2014 at 9:15
  • The warning you are seeing mostly means that you have a syntax error in your MySQL query. Are you sure that these col names are capitalized? Use echo mysql_error($db);to see the error. BTW Stop using mysql_* functions, they are deprecated and insecure. Use mysqli_* instead. Commented May 11, 2014 at 9:18
  • @wumm I'm going to use MySQLi a.s.a.p. And yes, they are capitalized. This the result when echoing the error: puu.sh/8HPJM.png Commented May 11, 2014 at 9:21

3 Answers 3

1

You are attempting to make a query using the mysql_ library but you haven't opened a connection with it. You've opened a connection with the mysqli_ library.

Pick either mysqli_ or mysql_ (or PDO) and stick to it. Don't switch database libraries mid-script. (Don't pick mysql_, it is deprecated).

In your connection page you are using mysqli_ library

if($mysqli->connect_errno) {

But in you complete code you are trying to get data using mysql library

if (mysql_num_rows($result)) {

Mysqli PHP:

<?php
// Create connection
$con=mysqli_connect("example.com","peter","abc123","my_db");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT Username,Wins FROM Leaderboard_Tag ORDER BY Wins DESC");
$rank = 1;
if (mysqli_num_rows($result) >= 1) {
while ($row = mysqli_fetch_assoc($result)) {
$Username=$row['Username'];
$Wins=$row['Wins'];
                                echo "<td>$rank</td>
                                    <td>Ava</td>
                                    <td>$Username</td>
                                    <td>$Wins</td>";
                        $rank++;
                            }
 }
mysqli_close($con);
?>
Sign up to request clarification or add additional context in comments.

2 Comments

So what do I need to change to use the complete MySQLi Libary?
@user3625127 use this instead mysqli_num_rows($result)
0

*important

from php 5.5.0 mysql function is deprecated(soft)

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: mysqli_connect() PDO::__construct()

Therefore it is better not to use Mysql in general, try to learn PDO.

Comments

0

your only problem here it maybe you forgot to include your connection file.

   include("your connection file here");

EDIT:

you are mixing between mysql and mysqli .

change this

    if (mysql_num_rows($result)) {

to

    if ($result->num_rows > 0)) {

EDIT:

you need to change the mysql fetch also to mysqli.

change this

    while ($row = mysql_fetch_assoc($result)) {

to

     $result->execute(); // Execute the prepared query.
    $result->store_result();
    $result->bind_result($Username, $Wins); 
    while($row = $result->fetch()){
            echo $Username;
            echo $Wins;
    }

2 Comments

I don't have a connection file. I put it at the top of the page. Complete code: gist.github.com/matthijs110/accb1b0b69570c4d3f50
Okay, I've used that, no more errors, but it still doesn't return anything from the echo. puu.sh/8HQmo.png

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.