【问题标题】:PHP Variable into MySQL Query?PHP变量进入MySQL查询?
【发布时间】:2013-12-28 10:52:42
【问题描述】:

我的 PHP 代码有一个小问题...我在论坛上搜索并复制了代码,但我一定是做错了什么。我正在尝试使用变量按名称、年份等排序我在页面上获得的结果。但它不起作用?

我有这个:

$orderby = 'name';    //'$_GET['orderby']' <---- I'd like to use this later to make it dynamic
$req = mysql_query('select id, name, year, genre, cover from table ORDER BY  `table`.`'$orderby'` ASC');

为什么代码不起作用?它没有给我一个错误,页面只是没有加载并保持空白!

【问题讨论】:

  • 什么,还没有关于在查询和 SQL 注入中使用 $_GET 的抱怨吗? :)

标签: php mysql variables get


【解决方案1】:

你忘记了串联:

$req = mysql_query('select id, name, year, genre, cover from table ORDER BY  `table`.`'.$orderby.'` ASC');

或者

$req = mysql_query("select id, name, year, genre, cover from table ORDER BY  `table`.`$orderby` ASC");

【讨论】:

    【解决方案2】:

    '$orderby' 更改为'.$orderby.'. 是 php 连接运算符。

    【讨论】:

      【解决方案3】:

      试试

      $req = mysql_query('select id, name, year, genre, cover from table ORDER BY  `table`.`' . $orderby . '` ASC');
      

      $req = mysql_query('select id, name, year, genre, cover from table ORDER BY  `table`.`$orderby` ASC');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-26
        • 1970-01-01
        • 1970-01-01
        • 2011-07-24
        • 1970-01-01
        • 1970-01-01
        • 2017-12-24
        • 1970-01-01
        相关资源
        最近更新 更多