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 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)