简体   繁体   中英

PHP Echo does not return Table data with SQL

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?

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);
?>

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;
    }

*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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM