简体   繁体   English

PDO Echo 10值按降序排列

[英]PDO Echo 10 values order by descending

I'm trying to echo out 10 numbers from my database in descending order sort of like a Highscores table. 我试图从数据库中以降序的方式回荡10个数字,就像Highscores表一样。

This is my code 这是我的代码

$conn = new PDO("mysql:host=HOST;dbname=NAME", "NAME", "PASSWORD");
$hs = $conn->query("SELECT exp FROM login ORDER BY number DESC LIMIT 10");


<? echo $hs?>

I'm new to PDO/PHP I didn't get any errors it just doesn't print anything from my table its just blank ;/ 我是PDO / PHP的新手,我没有任何错误,只是没有在我的表中打印任何内容,只是空白; /

you have to fetch the results in a array, and then echo the elements of the array. 您必须在数组中获取结果,然后回显数组中的元素。

$db = new PDO("mysql:host=$db_hostname;dbname=$database", $db_username, $db_password);

$sql = "SELECT exp FROM login ORDER BY number DESC LIMIT 10";

if ($stmt = $db->query($sql)) //PDO::query() returns a PDOStatement on success or false on failure.
{
    //If we got a PDOStatement as a return value from PDO::Query() fetch the results and echo.

    if($numbers = $stmt->fetchAll(PDO::FETCH_ASSOC)) //This will fetch all results in associative array.
    {
        //If the array contained data, then echo them.
        foreach ($numbers as $num)
        {
            echo $num['exp'] . "<br />";
        }
    }
    else
    {
        //If the PDOStatement returned an empty array. Let us know.
        echo "No data in the array";
    }
}
else
{
    //If PDO::Query returned false, then something is wrong with our query. Or connection or whatever.
     echo "Query failed.";
}

In queries that return large results I wouldn't use $stmt->fetchAll(). 在返回大结果的查询中,我不会使用$ stmt-> fetchAll()。 I would use fetch in a while loop like this: 我会在while循环中使用fetch,如下所示:

$db = new PDO("mysql:host=$db_hostname;dbname=$database", $db_username, $db_password);

$sql = "SELECT exp FROM login ORDER BY number DESC LIMIT 10";

if ($stmt = $db->query($sql)) //PDO::query() returns a PDOStatement on success or false on failure.
{
    //If we got a PDOStatement as a return value from PDO::Query() !!!ECHO WHILE FETCHING!!! 
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) //This loop will keep going for as many rows as the PDOStatement returns.
    {
        echo $row['exp'] . "<br />";
    }
}
else
{
    //If PDO::Query returned false, then something is wrong with our query. Or connection or whatever.
     echo "Query failed.";
}

The difference between the first code chunk and the second is that in 1st chunk, we fetch all the results in a array and print them. 第一个代码块和第二个代码块之间的区别在于,在第一个代码块中,我们将所有结果提取到数组中并打印出来。 In the second one tho, we print the data as we retrieve them one by one with PDOStatement::fetch() 在第二个方法中,我们使用PDOStatement :: fetch()逐一检索数据,从而打印数据。

You need to fetch the data from the object. 您需要从对象中获取数据。 I'll provide a link rather than code that will be helpful to you. 我将提供一个链接而不是代码,它将对您有所帮助。

It should give you good introduction. 它应该给您很好的介绍。 It covers your question. 它涵盖了您的问题。

http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers#Running_Simple_Select_Statements http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers#Running_Simple_Select_Statements

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

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