简体   繁体   English

如何解决mysql_query中的PHP MYSQL ORDER BY错误

[英]how to solve PHP MYSQL ORDER BY error in mysql_query

I have a problem with mysql_query with request with ORDER BY come from a parameter function 我的mysql_query有一个问题,它的ORDER BY请求来自参数函数

All parameters is already escaped and is hardcoded by me (not from a user forms or whathever) and this function serve to generate an HTML Select List 我已经对所有参数进行了转义并对其进行了硬编码(而不是从用户表单或任何形式),并且此函数用于生成HTML选择列表

This is an example of by BD 这是BD的一个例子

BD name = Year and table is :

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

it is easy id = fr (for french) = en = (for english) = Year 这很容易id = fr(对于法语)= en =(对于英语)=年

this is my function with parameters : 这是我的函数与参数:

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

so when i use this function : 所以当我使用这个功能:

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

in my HTML code everything is ok and the select list start at year 1914 $orderBy parameter is empty "" 在我的HTML代码中,一切正常,选择列表开始于1914年,$ orderBy参数为空“”

So, i need to starting my select list from 2014 ( ORDER BY id DESC ) 因此,我需要从2014年开始选择列表( ORDER BY id DESC

I try to use this : 我尝试使用这个:

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

with $orderBy parameter = "id DESC" and mysql_query never execute and die. $orderBy parameter = "id DESC"并且mysql_query永远不会执行并死掉。

This is the starting code for my function generateSelectList : 这是我的函数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)) { ... }

When I use my function without $orderBy parameter everything is ok and work fine. 当我使用没有$orderBy参数的函数$orderBy一切正常,并且工作正常。 When I use trace log ( echo ) $orderBy = "id DESC" and my $sql = "SELECT ID, fr FROM Year ORDER BY id DESC"; 当我使用跟踪日志( echo$orderBy = "id DESC"和我的$sql = "SELECT ID, fr FROM Year ORDER BY id DESC";

I add echo just before $resultat = mysql_query($sql) or die(mysql_error()); 我在$resultat = mysql_query($sql) or die(mysql_error());之前添加echo and just after 然后

On my website I see echo $orderBy , echo $sql echo AFTER and nothing after that if I use the version of generateSelectList without $orderBy parameter I see echo $orderBy, echo $sql , echo AFTER my HTML select list starting at year 1914 and echo AFTER. 在我的网站上,我看到echo $orderBy ,echo $sql echo AFTER,此后什么也没有,如果我使用不带$orderBy参数的generateSelectList版本,则看到echo $orderBy, echo $sql ,echo之后我的HTML选择列表始于1914年,并且之后回声。

When execute this $sql query directly in phpMyAdmin work fine! 当直接在phpMyAdmin执行此$sql查询时,效果很好!

It's really simple and I really don't understand why is not working. 这真的很简单,我真的不明白为什么不起作用。

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

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

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