繁体   English   中英

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

[英]PHP Search and Pagination using PDO MySQL - SQLSTATE[42000]: Syntax error

我正在尝试找出问题所在。

这是我的代码:

<?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>

我收到一条错误消息:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;请参阅附录A。 检查与您的MySQL服务器版本相对应的手册,以找到在第1行的' DESC '附近使用的正确语法

我知道一些旧的mysql代码,但我是PDO的初学者。

您能告诉我什么地方错了/在哪里修复我的代码?

PHP:5.6

的MySQL:5.6.37

非常感谢您的帮助。

谢谢。

请从DESC部分中删除`,它应该可以工作。

暂无
暂无

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

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