duanfen1312 2013-12-18 23:25
浏览 69
已采纳

SELECT COUNT(`id`)FROM`table` WHERE merk LIKE'%$ searchq%'

I want to count how many results I get when i use my search engine..

Here is my html code:

<form id="header-search" action="car-list.php" method="get">
            <input type="text" name="search"  id="text" placeholder="Zoek hier.."   class="quick-search"  > </form>

Php code:

     $searchq = $_GET['search'];
     $searchq = str_replace(' ','-',$searchq);     
    $con =  mysql_connect("localhost","root","****");
if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("register-login", $con);

$result = mysql_query("SELECT COUNT(`car_id`)FROM `adver` WHERE merk LIKE '%$searchq%' OR titel LIKE '%$searchq%' OR model LIKE '%$searchq%'");
$row = mysql_fetch_array($result);
$total = $row[0];



$pages = new Paginator;  
$pages->items_total = $total;
$pages->mid_range = 5;  
$pages->paginate();  

Im doing this to know "$pages->items_total = $total;" for my pagination.

Code for display results:

$searchq = htmlspecialchars($searchq); 
$searchq = mysql_real_escape_string($searchq);                  
$photo=mysql_query("SELECT * FROM `adver` WHERE merk LIKE '%$searchq%' OR titel LIKE '%$searchq%' OR model LIKE '%$searchq%' ORDER BY car_id DESC $pages->limit")or die(mysql_error());

while($get_photo=mysql_fetch_array($photo)){ 


?>


                                        <a href="<?php echo 'car-details.php?merk='.$get_photo['merk'] .'&car_id=' .$get_photo['car_id'] .'&titel=' .$get_photo['titel'] ;?>">
                            <img src="<?php  echo $get_photo['path'] ; ?>" style="border:1px solid #021a40;" alt="<?php  echo $get_photo['merk'] ;?>" }/>

Everyyhing is working fine until, when I search for something that doesn't exist in my databse. So $result = 0. Then I get the fowllowing error:

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 '-10,10' at line 1

When I change my $result = mysql_query to ==> ("SELECT COUNT(car_id)FROM adver"); Then everything works fine exept my pagination numbers... Because I count every car_id, no matter what I'm searching.. I'm getting for exampple 3 empty pages..

I hope someone can help me with this problem,Thanks

  • 写回答

2条回答 默认 最新

  • dtncv04228 2013-12-18 23:38
    关注

    Pagination is always a pain with LIMITS, it seems that you specify a limit of -10,10 (so a negative start); probably in $pages->limit, this seems to be the issue.

    Is there any performance consideration in not using SQL_CALC_FOUND_ROWS? With that you can get the total count of results, regardless of using a LIMIT clause.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能