douxian9010 2018-03-05 02:46
浏览 62
已采纳

使用PDO MySQL进行PHP搜索和分页 - SQLSTATE [42000]:语法错误

I am trying to figure out what is wrong.

Here is my code:

<?php
define("ROW_PER_PAGE",2);

require_once("config.php");

?>

<div class="container">

    <div class="row">
        <div class="col-md-12">
        <?
        $search_keyword = '';
        if(!empty($_POST['search']['keyword'])) {
            $search_keyword = $_POST['search']['keyword'];
        }

        $sql = "SELECT * FROM finance WHERE `name_first` LIKE :keyword ORDER BY `fID` `DESC` ";

        /* Pagination Code starts */
        $per_page_html = '';
        $page = 1;

        $start=0;
        if(!empty($_POST["page"])) {
            $page = $_POST["page"];
            $start=($page-1) * ROW_PER_PAGE;
        }

        try {
            $limit=" limit " . $start . "," . ROW_PER_PAGE;
            $pagination_statement = $db->prepare($sql);
            $pagination_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
            $pagination_statement->execute();

            } catch (PDOException $e) {
                echo "Error : Check your error message.";
                file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
        }

        $row_count = $pagination_statement->rowCount();

        if(!empty($row_count)){
            $per_page_html .= "<div style='text-align:center;margin:20px 0px;'>";
            $page_count=ceil($row_count/ROW_PER_PAGE);

            if($page_count>1) {
                for($i=1;$i<=$page_count;$i++){

                    if($i==$page){
                        $per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page current" />';
                    } else {
                        $per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page" />';
                    }

                }
            }

            $per_page_html .= "</div>";
        }

        try {
            $query = $sql.$limit;
            $pdo_statement = $db->prepare($query);
            $pdo_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
            $pdo_statement->execute();
            $result = $pdo_statement->fetchAll();

            } catch (PDOException $e) {
                echo "Error : Check your error message.";
                file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
        }

        ?>

        <form name='frmSearch' action='' method='post'>
            <div style='text-align:right;margin:20px 0px;'><input type='text' name='search[keyword]' value="<?php echo $search_keyword; ?>" id='keyword' maxlength='25'></div>
        <table class='tbl-qa'>
            <thead>
                <tr>
                    <th class='table-header' width='20%'>First Name</th>
                    <th class='table-header' width='40%'>Last Name</th>
                    <th class='table-header' width='20%'>Birth Date</th>
                </tr>
            </thead>
            <tbody id='table-body'>
            <?php
            if(!empty($result)) { 
                foreach($result as $row) {
                ?>
                <tr class='table-row'>
                    <td><?php echo $row['name_first']; ?></td>
                    <td><?php echo $row['name_last']; ?></td>
                    <td><?php echo $row['birth_date']; ?></td>
                </tr>
                <?php
                }
            }
            ?>
            </tbody>
        </table>
        <?php echo $per_page_html; ?>
        </form>





        </div>
    </div>
</div>

I received error message says ... SQLSTATE[42000]: Syntax error or access violation: 1064 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 'DESC' at line 1

I know some old style mysql code, but I am beginner for PDO.

Would you please tell me what is wrong / where to fix my code?

PHP: 5.6

MySQL: 5.6.37

I really appreciated your help.

Thank you.

  • 写回答

1条回答 默认 最新

  • duancuisan2503 2018-03-05 05:09
    关注

    Please remove the ` from the DESC part and it should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?