-3

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

解析错误:语法错误,意外的“?” 在第 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);
?>
4

2 回答 2

0

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

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

' SELECT … '%?%' '

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

但是最好的办法是在字符串中的那个位置根本不使用引号,因为 PDO 已经在准备好的 SQL 语句中将字符串包装在引号中。只需预先/附加%到插入的值。

于 2013-05-30T12:53:08.357 回答
-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();

或者

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

')->fetchColumn();
于 2013-05-30T12:51:59.633 回答