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 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么