Thread: global $db;

Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2006
    Posts
    283
    Plugin Contributions
    0

    Default global $db;

    I am writing some custom code that requires communication with the db. In my functions I use: global $db; but I keep getting 'scolded' on programming forums for using globals.

    Is there a better way for me to use the db in code such as the following when communicatin with the zencart db?:

    Code:
    function beginProcess(){
        global $db; 
        $sql = "SELECT last_batch from ".TABLE_STATUS.";";            
        $lastBatch = $db->Execute($sql);
        $lastBatch=(int)$lastBatch->fields['last_batch'];
        echo "<BR/>Last Batch = ".$lastBatch;
    
        if ($lastBatch >=1 && $lastBatch <=3 ){
            $batch = $lastBatch +1;
        }else{
            $batch = 1;
        }
            processBatch($batch);
    }
    22 stores and counting! Ive been zenned.

  2. #2
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    13,909
    Plugin Contributions
    96

    Default Re: global $db;

    If you need to access the database from a function, either you need to pass the $db value as an input or, as you've done, declare it as a global variable.

 

 

Similar Threads

  1. global filtering
    By AhrbokTrexon in forum General Questions
    Replies: 0
    Last Post: 22 Mar 2008, 12:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg