469,134 Members | 1,315 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,134 developers. It's quick & easy.

how to access adodb object in recursive function of php

245 100+
Hi Geeks,
I am using adodb for accessing my records from MYSQL Database.

I define an object of adodb "$db" which is used for executing queries in my configuration file which is accessible in each and every page of the application.

Now when i try to access $db object in a function it says

Expand|Select|Wrap|Line Numbers
  1. Fatal error: Call to a member function Execute() on a non-object
I try to google my problem. There i find a solution that i must pass the adodb object $db in that function. As my function is recursive for getting categories from category table then i need to call the function again if it has child in it. Now again when i tried to pass that $db object in my function recursive function it behave abnormally. The recursive function getting the reference of that adodb object not the actual object. so how can i pass that object again in my recursive function.

Here is code of my function.

Function calling
Expand|Select|Wrap|Line Numbers
  1. <div class="suckerdiv">
  2.     <ul id="suckertree1">
  3.     <?php createMenu(0, $db);?>
  4.     </ul>
  5. </div>
  6.  
Recursive function
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function createMenu($parId, &$db){
  3.     $q="select cat_id, parent_id, cat_name from ".CATEGORY." where parent_id='$parId'";
  4.     $rs = $db->Execute($q);
  5.     if($rs->RecordCount()>0){
  6.         while(!$rs->EOF){
  7.             $q="select cat_id, cat_name from ".CATEGORY." where parent_id=".$rs->fields('cat_id')."";
  8.             $rs = $db->Execute($q);
  9.             $noRows=$rs->RecordCount();
  10.             if($noRows > 0){
  11.                 ?>
  12.                 <li><a href="products/<?php echo $rs->fields('cat_id');?>"><?php echo $rs->fields('cat_name');?></a>
  13.                     <ul>
  14.                        <?php createMenu($row_cat['cat_id'],$db);?>
  15.                     </ul>
  16.                 </li>
  17.             <?php }else{?>
  18.                 <li><a href="products/<?php echo $rs->fields('cat_id');?>"><?php echo $rs->fields('cat_name');?></a></li><? 
  19.             }
  20.         }
  21.     }//end while
  22. }?>
  23.  
May 13 '11 #1
2 2649
neovantage
245 100+
I have fixed it. The problem was when i call the function again in recursion i am passing parameter $db which must be &$db.

Secondly i was not fetching the record properly. The exact format is
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function createMenu($parId, &$db){
  3.     $q="select cat_id, parent_id, cat_name from ".CATEGORY." where parent_id='$parId'";
  4.     $rs = $db->Execute($q);
  5.     if($rs->RecordCount()>0){
  6.         while(!$rs->EOF){
  7.             $q_s="select cat_id from ".CATEGORY." where parent_id='".$rs->fields('cat_id')."'";
  8.             $rsTotal = $db->Execute($q_s);
  9.             $noRows = $rsTotal->RecordCount();
  10.             if($noRows > 0){
  11.             ?>
  12.             <li><a href="products/<?php echo $rs->fields('cat_id');?>"><?php echo $rs->fields('cat_name');?></a>
  13.                 <ul><?php createMenu($rs->fields('cat_id'),&$db);?></ul>
  14.             </li>
  15.             <?
  16.             }else{
  17.                 echo '<li><a href="products/'.$rs->fields('cat_id').'">'.$rs->fields('cat_name').'</a></li>';
  18.             }
  19.             $rs->MoveNext();
  20.         }
  21.     }//end while
  22. }?>
  23.  
I am posting my exact code that it may be helpful for those who want to use recursive function with adodb object.

Thanks
May 13 '11 #2
Dormilich
8,651 Expert Mod 8TB
you can optimise your code by using Prepared Statements. that means that you can create one query and pass this query around (not the ADOdb base object) instead of creating the same query over and over again.
May 14 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by JP SIngh | last post: by
2 posts views Thread by LoserInYourFaceEngineer | last post: by
2 posts views Thread by | last post: by
4 posts views Thread by Nicolas Vigier | last post: by
3 posts views Thread by AliRezaGoogle | last post: by
9 posts views Thread by pereges | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.