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

使用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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题