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 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。