语法错误,意外'?'在分页搜索中

时间:2013-05-30 12:49:56

标签: php pdo

我正在尝试在我的网站上运行搜索功能,当我通过(上一个/下一个)浏览时,我有分页功能。我已经复制了分页的源代码并编辑了查询以使用搜索功能。但是我收到了一个错误:

  

解析错误:语法错误,意外'?'在   第16行的C:\ xampp \ htdocs ** SNIP ** \ MySQL_DB \ search.php

我尝试将?替换为来自'%?%'的变量$term = $_POST['search']; 但是我得到了一个

  

警告:在第16行的C:\ xampp \ htdocs \ freedeals \ MySQL_DB \ search.php中除以零

搜索分页的源代码

<?php include 'connect_auth.php';?>
<?php $dbh=Connection() ?>
<?php
try {
$term = $_POST['search'];

//$term = "seg";
    // Find out how many items are in the table
    $total = $dbh->query('
        SELECT
            COUNT(*)
        FROM
            buy_car
        WHERE 
            description like '%?%'
        OR
            make like '%?%'

    ')->fetchColumn();



    // How many items to list per page
    $limit = 1;

    // How many pages will there be
    $pages = ceil($total / $limit);

    // What page are we currently on?
    $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
        'options' => array(
            'default'   => 1,
            'min_range' => 1,
        ),
    )));

    // Calculate the offset for the query
    $offset = ($page - 1)  * $limit;

    // Some information to display to the user
    $start = $offset + 1;
    $end = min(($offset + $limit), $total);

    // The "back" link
    $prevlink = ($page > 1) ? '<a href="?page=1" title="First page">&laquo;</a> <a href="?page=' . ($page - 1) . '" title="Previous page">&lsaquo;</a>' : '<span class="disabled">&laquo;</span> <span class="disabled">&lsaquo;</span>';

    // The "forward" link
    $nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">&rsaquo;</a> <a href="?page=' . $pages . '" title="Last page">&raquo;</a>' : '<span class="disabled">&rsaquo;</span> <span class="disabled">&raquo;</span>';

    // Display the paging information
    echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages, displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>';

    // Prepare the paged query
    $stmt = $dbh->prepare('
        SELECT
            *
        FROM
            buy_car
        WHERE 
            description like '%?%'
        OR
            make = '%?%'
        ORDER BY
            ID
            DESC
        LIMIT
            :limit
        OFFSET
            :offset
    ');

    // Bind the query params
    $stmt->bindParam(':limit', $limit, PDO:: PARAM_INT);
    $stmt->bindParam(':offset', $offset, PDO:: PARAM_INT);
    $stmt->execute();

    // Add comment
    $incr = 160;
    $style = true;

    // Do we have any results?
    if ($stmt->rowCount() > 0) {
        // Define how we want to fetch the results
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $iterator = new IteratorIterator($stmt);

        // Display the results
        foreach ($iterator as $row) {
          if($style==true){
                echo "<p style='background-color:#FFFD5C;border-width:1px;border-color:#000000;border-style:solid;
                border-width:1px;top:350px;width:800px;height:".$incr."px;'>";

                echo '<a href="freedeals/cars/'.$row{'ID'}.'">'.$row{'description'}.'</a>';
                echo "<p1 style='position:absolute ;left:700px;'>Price: &euro;".$row{'price'}."</p1>";
                echo '<br><a href="freedeals/cars/'.$row{'ID'}.'"><img src="images/uploads/'.preg_replace('~[\da-f]{32}-~', '', $row{'ID'}).'.jpeg" style="max-height: 100px; max-width: 100px;" ></a>'; 
                echo "<br>Make:".$row{'make'}."<br>Model:".$row{'model'}."<br>Year:".$row{'year'};
                echo "</p>";
                $style=false;

            }
        else if($style==false){
                echo "<p style='background-color:#D6D30D;border-width:1px;border-color:#000000;border-style:solid;
                border-width:1px;top:350px;width:800px;height:".$incr."px;'>";

                echo '<a href="freedeals/cars/'.$row{'ID'}.'">'.$row{'description'}.'</a>';
                echo "<p1 style='position:absolute ;left:700px;'>Price: &euro;".$row{'price'}."</p1>";
                echo '<br><a href="freedeals/cars/'.$row{'ID'}.'"><img src="images/uploads/'.preg_replace('~[\da-f]{32}-~', '', $row{'ID'}).'.jpeg" style="max-height: 100px; max-width: 100px;" ></a>'; 
                echo "<br>Make:".$row{'make'}."<br>Model:".$row{'model'}."<br>Year:".$row{'year'};
                echo "</p>";
                $style=true;

            }
        }

    } else {
        echo '<p>No results could be displayed.</p>';
    }

} catch (Exception $e) {
    echo '<p>', $e->getMessage(), '</p>';
}
ini_set('error_reporting', E_ALL);
?>

2 个答案:

答案 0 :(得分:0)

?不是有效的PHP表达式,%?%也不是。

如果您开始和结束字符串,则下一个符号将被解释为PHP代码,而不是字符串的一部分。

' SELECT … '%?%' '

请使用带语法突出显示的编辑器,在将来运行代码之前,您会提到这些错误。

但最好的办法是不要在该位置使用字符串中的引号,因为PDO已在现成的SQL语句中将字符串包装在引号中。只需将%预先/附加到插入的值。

答案 1 :(得分:-1)

通过使用单引号定义字符串以及字符串,您在查询中突破了字符串

$total = $dbh->query('
    SELECT
        COUNT(*)
    FROM
        buy_car
    WHERE 
        description like '%?%'
    OR
        make like '%?%'

')->fetchColumn();

您需要使用双引号定义查询或转义字符串

中的单个查询
$total = $dbh->query("
    SELECT
        COUNT(*)
    FROM
        buy_car
    WHERE 
        description like '%?%'
    OR
        make like '%?%'

")->fetchColumn();

OR

$total = $dbh->query('
    SELECT
        COUNT(*)
    FROM
        buy_car
    WHERE 
        description like \'%?%\'
    OR
        make like \'%?%\'

')->fetchColumn();