dsjzmrz78089 2010-08-01 22:58
浏览 66
已采纳

Foreach循环带来致命错误:在非对象上调用成员函数bindParam()

That's the first time i get an error like this one, let me explain :

Here is my code :

function printSiteIndexedItems($co, $id){
  global $allSections;
  foreach($allSections as $aSection => $aSectionName){
   $tr = $co->prepare("SELECT COUNT(id) FROM ". $aSection ." WHERE site=:id AND valide=1");
   $tr->bindParam(':id', $id, PDO::PARAM_INT);
   $tr->execute();
   if($indexedItems = $tr->fetchColumn()) echo '<p>'. $aSectionName .' : '. $indexedItems .'</p>';
  }
 }

The first iteration works just fine, it prints what i want (a category name and the number of elements in it).

But after that first iteration, i get this classic error :

Fatal error: Call to a member function bindParam() on a non-object in

Indeed, $co is a valid PDO object as it works for the first iteration. But it seems that as soon as we enter the second one, it no longer is ? :o

I'm kinda new with PDO, so maybe it's a normal behavior i didn't acknowledge yet. Please help ! =)

  • 写回答

3条回答 默认 最新

  • dongzong2017 2010-08-01 23:07
    关注

    Looks like $co->prepare... returns FALSE for at least one of the stamtents you try to prepare.
    Either test if ( !$tr ) .... or set $co->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); to get an exception when PDO encounters an error.

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

报告相同问题?

悬赏问题

  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测