【问题标题】:how to solve PHP MYSQL ORDER BY error in mysql_query如何解决 mysql_query 中的 PHP MYSQL ORDER BY 错误
【发布时间】:2014-04-21 03:31:59
【问题描述】:

mysql_query 有问题,ORDER BY 的请求来自参数函数

所有参数都已经被转义并由我硬编码(不是来自用户表单或其他),此函数用于生成 HTML 选择列表

这是BD的一个例子

BD name = Year and table is :

id------fr-----en
1914 1914 1914
1915 1915 1915
...
2014 2014 2014

很容易 id = fr (法语) = en = (英语) = Year

这是我的带参数函数:

function generateSelectList($bd, $table, $name, $id, $language, $orderBy, $print0, $tabindex, $class, $style, $label, $value0, $title)

所以当我使用这个功能时:

<?php generateSelectList("gaiurba_GayUrban", "Year", "Year", "ID", $_SESSION['LanguageCode'], "", "true", "21", "splash_small required", "", "label_year", "", " "); ?>

在我的 HTML 代码中一切正常,选择列表从 1914 年开始 $orderBy 参数为空""

所以,我需要从 2014 年开始我的选择列表 (ORDER BY id DESC)

我尝试使用这个:

<?php generateSelectList("gaiurba_GayUrban", "Year", "Year", "ID", $_SESSION['LanguageCode'], "id DESC", "true", "21", "splash_small required", "", "label_year", "", " "); ?>

$orderBy parameter = "id DESC"mysql_query 永远不会执行和死亡。

这是我的函数 generateSelectList 的起始代码:

    try {
    $accessDb = new Connexion($bd);
    $connexion = $accessDb->openConnexion();

    if ($orderBy != "" || $orderBy != null) {
        echo $orderBy;
        $sql = "SELECT $id, $language FROM $table ORDER BY $orderBy";
    } else {
        $sql = "SELECT $id, $language FROM $table";
    }

    echo " sql = " . $sql;

    $resultat = mysql_query($sql) or die(mysql_error());
    $nbResultat = mysql_num_rows($resultat);

    echo '<select id="' . $name . '" class="' . $class . '" tabindex="' . $tabindex . '" name="' . $name . '" style="' . $style . '" title="' . $title . '">';

    while ($row = mysql_fetch_array($resultat)) { ... }

当我使用没有$orderBy 参数的函数时,一切正常并且工作正常。 当我使用跟踪日志 (echo) $orderBy = "id DESC" 和我的 $sql = "SELECT ID, fr FROM Year ORDER BY id DESC";

我在 $resultat = mysql_query($sql) or die(mysql_error()); 之前添加了 echo 就在

之后

在我的网站上,我看到 echo $orderBy,echo $sql echo AFTER,之后什么都没有 如果我使用不带 $orderBy 参数的 generateSelectList 版本,我会看到 echo $orderBy, echo $sql,从 1914 年开始在我的 HTML 选择列表之后回显并在之后回显。

当直接在phpMyAdmin 中执行这个$sql 查询时工作正常!

这真的很简单,我真的不明白为什么不起作用。

【问题讨论】:

    标签: php mysql sql-order-by


    【解决方案1】:

    您是否尝试在要选择的字段上加上单引号?

    【讨论】:

    • 是的,但没有成功!
    • 你选择的ID和按ID排序的ID都不一样吧?如果是,请尝试按 ID desc 排序
    • MySql 对列 ID 的名称不区分大小写或 id 在我的数据库中相同名称是:id fr en,如果我使用 select ID 或 select id 正在工作,尝试很多次:)
    猜你喜欢
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    • 2011-12-20
    相关资源
    最近更新 更多