doufei2355 2012-10-11 18:21
浏览 53
已采纳

GET变量未被访问

I have a simple 7 lined PHPscript, as shown below:

Code

require( "connect.php" );
$ctg = ($_GET['ctg'])? $_GET['ctg']:'movie';
$offset = ($_GET['off'])? $_GET['off']:30;
$query = "SELECT `msg`, `id`, `nick`, `date` FROM `entries` WHERE `ctg` = ? ORDER BY `id` DESC LIMIT 15 OFFSET ?";
$statemnt = $conn->prepare( $query );
$statemnt->bindParam( 1, $ctg, PDO::PARAM_STR );
$statemnt->bindParam( 2, $offset, PDO::PARAM_INT );
$statemnt->execute();
$results = $statemnt->fetchAll( PDO::FETCH_ASSOC );
echo "$ctg
$offset
" . count($results) . "
";

Problem

The problem I am facing is like this: When I execute the file in console(using php fetch.php), all the echo work great. When I open the page in browser with no $_GET variables, the same output is observed.

The output in browser is the same when I provide it with just the ctg indexed-variable(such as http://localhost/fetch.php?ctg=docu) the output is correct; like this:

docu
30
15

But when I pass the off index-variable(either as fetch.php?off=15 OR fetch.php?ctg=docu&off=15 OR fetch.php?off=15&ctg=docu); The output is always like this:

docu
15
0

Question

The output(and var_dump) both show correct values being passed to both variables, then why is the PDO statement not receiving any results? Why does the count($results) remain 0?

Any help is appreciated.


PS

I also included the following statement:

$statemnt->debugDumpParams();

which dumps this in browser(and console):

SQL: [102] SELECT `msg`, `id`, `nick`, `date` FROM `entries` WHERE `ctg` = ? ORDER BY `id` DESC LIMIT 15 OFFSET ?
Params:  2
Key: Position #0:
paramno=0
name=[0] ""
is_param=1
param_type=2
Key: Position #1:
paramno=1
name=[0] ""
is_param=1
param_type=1
  • 写回答

1条回答 默认 最新

  • dongrunying7537 2012-10-11 18:35
    关注

    Possibly the $off GET variable is being treated as a string whereas int is expected

    Change:

    $offset = ($_GET['off'])? $_GET['off']:30; 
    

    to

    $offset = ($_GET['off'])?(intval($_GET['off']):30; 
    

    Of course proper validation should exist for all your GETvariables

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?