首页 > 解决方案 > “我想使用带有 ajax 的选择选项显示记录数”

问题描述

“您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'limit 0,6' 附近使用正确的语法”

“我已经编写查询以选择 2 行并选择子类别,但是当我选择选项时,它给了我 sql 语法错误”

<div class="pull-right">
                            <div class="page-filter">
                            <input type="hidden" value="<?php echo $_GET['sub']?>" id="sub_val"/>
                            <select id="select_menu" onchange="show_all();">
                            <span class="text-uppercase">Show:</span>
                                        <option value="1">10</option>
                                        <option value="2">20</option>
                                        <option value="3">30</option>
                                        <option value="4">50</option>
                                        </select>


                            </div>

<?php

$i = $_GET['index'];
$s = $_GET['sub'];

//echo $c;
if(isset($_GET['page']))
{
    $page = $_GET['page'];
}
else
{
    $page = 1;
}

$productPerPage = 6;
$startNum = ($page - 1)* $productPerPage ; // (2-1) * 3 

require_once("connection.php");


if($i==0)
{
    $result = mysqli_query($con,"select  TOP 3 * from products where subcategory='".$s."' order by asc limit ".$startNum.",".$productPerPage) or die(mysqli_error($con));

}


if($i==1)
{
    $result = mysqli_query($con,"SELECT TOP 3 * FROM products where subcategory='".$s."' limit ".$startNum.",".$productPerPage) or die(mysqli_error($con));

}
if($i==2)
{
    $result = mysqli_query($con,"SELECT TOP 4 * FROM products where subcategory='".$s."' limit ".$startNum.",".$productPerPage) or die(mysqli_error($con));

}
if($i==3)
{
    $result = mysqli_query($con,"SELECT TOP 5 * FROM products where subcategory='".$s."' limit ".$startNum.",".$productPerPage) or die(mysqli_error($con));

}

while($row = mysqli_fetch_array($result))
{
?>

<div class="col-md-4 col-sm-6 col-xs-6">
                                <div class="product product-single">
                                    <div class="product-thumb">
                                        <div class="product-label">
                                            <span><?php echo $row[4];?></span>
                                            <span class="sale"><?php echo $row[5];?></span>

                                        </div>
                                        <button class="main-btn quick-view"><a href="product-page.php?id=<?php echo $row[0];?>"><i class="fa fa-search-plus"></i> Quick view</button></a>
                                                <img src="<?php echo $row[9];?>" height="150" width="100" style="margin-top:60px;"/> 
                                    </div>
                                    <div class="product-body">
                                        <h3 class="product-price"><?php echo $row[3];?><del class="product-old-price"><?php echo $row[2];?></del></h3>
                                        <div class="product-rating">
                                            <i class="fa fa-star"></i>
                                            <i class="fa fa-star"></i>
                                            <i class="fa fa-star"></i>
                                            <i class="fa fa-star"></i>
                                            <i class="fa fa-star-o empty"></i>
                                        </div>
                                        <h2 class="product-name"><a href="#"><?php echo $row[1];?></a></h2>
                                        <div class="product-btns">
                                            <button class="main-btn icon-btn"><i class="fa fa-heart"></i></button>
                                            <button class="main-btn icon-btn"><i class="fa fa-exchange"></i></button>
                                            <button class="primary-btn add-to-cart"><i class="fa fa-shopping-cart"></i> Add to Cart</button>
                                        </div>
                                    </div>
                                </div>
                            </div>      



<?php
}


?>




“我想要使用选择选项的记录数,但它给了我 sql 语法错误”

标签: phpsql

解决方案


MySQL 版本会出现此错误。

您可以使用以下查询(示例):

SELECT * FROM products LIMIT 0,5

在你的情况下:

"SELECT * 
FROM products
where subcategory=$s
ORDER BY  column_name ASC 
LIMIT $startNum , $productPerPage"

推荐阅读