472,351 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,351 software developers and data experts.

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 2758
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,658 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

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

Similar topics

9
by: JP SIngh | last post by:
Hi All I am trying to write a recursive function to list the managers and his employees in a tree like sctructure Manager 1 Emp1 Emp1.1 Emp...
2
by: LoserInYourFaceEngineer | last post by:
Hello All: I'm having trouble with a recursive function. The function is supposed to identify nested folders in a hierarchical folder...
2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function...
4
by: Nicolas Vigier | last post by:
Hello, I have in my python script a function that look like this : def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42): if type(arg1) is...
0
by: Otis Hunter | last post by:
I have been fighting with this for days and your expert help is needed! Below is the code I am executing which results with "Object doesn't...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit)...
2
by: RJN | last post by:
Hi I need help in writing a recursive function. My table structure is as below. InstanceId LevelId ParentId 100 1 ...
1
by: subah ponraj | last post by:
Hi Am facing a problem while passing Recordset object in a recursive function or to 3rd level of function. Please help. My javascript code is:...
3
by: AliRezaGoogle | last post by:
Dear Members, I have written a recursive function. It calls itself recursively. It is placed inside a thread. So I can easily suspend and resume...
9
by: pereges | last post by:
Hello I need some ideas for designing a recursive function for my ray tracing program. The idea behind ray tracing is to follow the...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.