douzaoqu4447 2012-12-27 22:09
浏览 422

在第4行'ORDER BY date DESC LIMIT 1'附近使用的正确语法[关闭]

I keep getting the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY date DESC LIMIT 1' at line 4

function printSoldiersOfRank($rank, $branch)
{
  $soldier = new soldierClass;
  $sql = "SELECT * FROM soldier WHERE rank = $rank AND branch = '$branch'";
  $result = mysql_query($sql);
  while($row = mysql_fetch_array($result)):
    $soldier->getInfo($row['sID']);
    $soldier->printInfo(); 
  endwhile;

function printInfo()
  {
    $sql = "SELECT promoter, name, date
    FROM soldier s, log l
    WHERE s.sID = l.promoter AND l.promotee = $this->sid
    ORDER BY date DESC LIMIT 1";
    $result = mysql_query($sql) or die(mysql_error());
    $row = mysql_fetch_array($result);
    echo "<div class='soldierInfo'>";
    echo "<a href='index.php?page=soldier&sid=$this->sid'>";
    echo $this->name;
    echo "</a><br />";
    echo "Last Promoted: ";
    echo date("d M Y", $row['date']);
    echo "<br />By: <a href='index.php?page=soldier&sid=$row[0]'>";
    echo $row['name'];
    echo "</a></div>";
  }

I'm assuming the error is in my printInfo() function. All help is appreciated, thanks in advance.

  • 写回答

5条回答 默认 最新

  • douzhu5900 2012-12-27 22:35
    关注

    Don't assume that the error is anywhere. Instead create an error message that gives you all the information you need to debug. Here is an example.

    $sql = 
    "
    SELECT promoter, name, date
    FROM soldier s, log l
    WHERE ( s.sID = l.promoter AND l.promotee = $this->sid )
    ORDER BY date DESC 
    LIMIT 1
    "
    ;
    
    $result = mysql_query($sql);
    if (!$result)
    {
        echo PHP_EOL;
        echo "<br/>";
        echo "FAILED QUERY $sql";
        echo "<br/> ON ";
        echo __LINE__;
        echo " IN ";
        echo __FILE__;
        echo "<br/>";
        echo " BECAUSE ";
        die(mysql_error());
    }
    

    If you use something like that, you will be able to see the query that failed and the location of the failure with certainty. As it stands now, you have a query that uses "ORDER BY date DESC" and an error message that says "ORDER BY name ASC" so it leaves me to wonder if we are looking at the right script!

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置