简体   繁体   English

PHP Echo不使用SQL返回表数据

[英]PHP Echo does not return Table data with SQL

I am working on a leaderboard. 我正在排行榜上。 The leaderboard gets data from my SQL Database. 排行榜从我的SQL数据库获取数据。 It is using the old method of MySQL because I'm not familiar with MySQLi yet. 它使用MySQL的旧方法,因为我还不熟悉MySQLi。

I don't want to add a row everytime, so I'm using a php method to repeat it. 我不想每次都添加一行,所以我正在使用一种php方法重复它。 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 这是我的完整代码: 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 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. 您正在尝试使用mysql_库进行查询,但尚未打开与它的连接。 You've opened a connection with the mysqli_ library . 您已经打开了与mysqli_库的连接。

Pick either mysqli_ or mysql_ (or PDO) and stick to it. 选择mysqli_mysql_ (或PDO)并坚持下去。 Don't switch database libraries mid-script. 不要在脚本中间切换数据库库。 (Don't pick mysql_, it is deprecated). (不选择mysql_,已弃用)。

In your connection page you are using mysqli_ library 在您的连接页面中,您正在使用mysqli_

if($mysqli->connect_errno) {

But in you complete code you are trying to get data using mysql library 但是在您完整的代码中,您尝试使用mysql库获取数据

if (mysql_num_rows($result)) {

Mysqli PHP: 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 . 您正在混合mysql和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. 您还需要将mysql fetch也更改为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) 从PHP 5.5.0起不推荐使用mysql功能(软)

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. 自PHP 5.5.0起不推荐使用此扩展,以后将删除。 Instead, the MySQLi or PDO_MySQL extension should be used. 相反,应使用MySQLi或PDO_MySQL扩展。 See also MySQL: choosing an API guide and related FAQ for more information. 另请参见MySQL:选择API指南和相关的FAQ,以获取更多信息。 Alternatives to this function include: mysqli_connect() PDO::__construct() 该函数的替代品包括:mysqli_connect()PDO :: __ construct()

Therefore it is better not to use Mysql in general, try to learn PDO. 因此最好不要一般使用Mysql,尝试学习PDO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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