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 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败