doukuang6795 2012-10-29 18:01
浏览 10
已采纳

小改变打破了mysql查询[重复]

Possible Duplicate:
PHP PDO bindValue in LIMIT

I have this code that works quite well.

if (array_key_exists('cat', $_GET) === TRUE) {
    $category = $_GET['cat'];
} else {
    $category = '.*';
}


$conn = new PDO('mysql:host=localhost;dbname=news', 'root', '');
$stmt = $conn->prepare('SELECT * FROM stories 
                        WHERE category RLIKE :cat
                        ORDER BY score DESC LIMIT 0, 25');
$stmt -> execute(array(
    'cat' => $category,
    ));

$result = $stmt->fetchAll();

As you can see, it gets a category from the get request, and searches the database for everything in that category.

I'm also trying to add a bit so that there can be a page defined in the get request, and the query will start 25 rows later for each increase by one in page.

Here's what I wrote:

if (array_key_exists('cat', $_GET) === TRUE) {
    $category = $_GET['cat'];
} else {
    $category = '.*';
}

if (array_key_exists('page', $_GET) === TRUE) {
    $page = intval($_GET['page'])*25;
} else {
    $page = 0;
}


$conn = new PDO('mysql:host=localhost;dbname=news', 'root', '');
$stmt = $conn->prepare('SELECT * FROM stories 
                        WHERE category RLIKE :cat
                        ORDER BY score DESC LIMIT :page, 25');
$stmt -> execute(array(
    'cat' => $category,
    'page' => $page
    ));

$result = $stmt->fetchAll();

But now, the query is returning nothing, no matter what page is, or if there is a category.

Maybe I'm not dealing with integers right. Any idea why I'm getting this result?

  • 写回答

2条回答 默认 最新

  • douxigai8757 2012-10-29 18:32
    关注

    As in the referred example on how to apply values to LIMIT conditions:

    $stmt -> execute(array(
      'cat' => $category,
      'page' => (int) $page
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题