MySQL / PHP按顺序排序

时间:2013-02-26 01:14:13

标签: php mysql sql-order-by

我有以下查询 -

$query = mysql_query("SELECT * FROM test where admin = 'Yes' order by name = '$_GET[name]' DESC");

while($row = mysql_fetch_array($query))
{
   $id.=$row[id];
}

echo $id;

我想要一个从$ _GET [name]的id开始的id列表但是它返回一个列表,其下面的行的起始名称是?有任何想法吗?

非常感谢。

1 个答案:

答案 0 :(得分:1)

  1. 使用数组

  2. 您需要在字符串名称

  3. 周围使用引号
    $query = mysql_query("SELECT * FROM test where admin = 'Yes' order by name = '$_GET[name]' DESC");
    
    $ids = array();
    while($row = mysql_fetch_array($query))
    {
       $ids[] =$row['id'];
    }
    
    // Your IDs are now in an array which is easier to work with
    print_r($ids);
    

    仅供参考,you shouldn't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial

    你也对SQL injections

    持开放态度