Link to home
Create AccountLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

What am I doing wrong with this function?

Here's my sql: SELECT sum (allocatedamount) as capxtotal from cer_Capx where cermodelid=@ModelID

Here's how I'm documenting it as a function:

   if(!function_exists('cer_capxsum')) {
        function cer_capxsum($mID) {
         global $edb, $_SESSION;
         
         $sql_data = "exec cer_CapxSum $mID";
          if ($_SESSION["portaladmin"] == 1 && $_SESSION["showsql"] != "no")
            echo display_detail("cer/cer_functions.php",$sql_data);
        $result_data = odbc_exec($edb, $sql_data);
         }  
    }
     

Open in new window

Here's how I'm calling it:

$capxsum = cer_capxsum($mID);

When I plug the sql directly into my SQL Management Studio, it fires fine. But when I do this:

echo $capxsum["capxtotal"];

I get nothing...

What am I missing? Why does the sql fire, why do I get my sum but not be able to echo it?

Thoughts?
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

I suspect it's because $capxsum is an object, not an array.

Try var_dump($capxsum) and see it's type.

HTH,
Dan
SOLUTION
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Add error_reporting(E_ALL) to the top of all of your PHP scripts.  Then things like incorrect data types and undefined variables will be visible and you won't be left in the dark, guessing.  This will not prevent logic errors, but it will reveal a multitude of potential problems.

Omit the "global" declaration for $_SESSION.  It's a superglobal.  Instead, print it out with var_dump($_SESSION) to see what data is in there.  It's a logic inflection point in your script.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Part of successful programming is using cleanly written code to demonstrate the logic you seek to achieve.  Unfortunately (unlike Python) PHP does not enforce coding standards so the programmer must deliberately write clean code.  If you line up the control structures you can often see what the logic is intended to do.

I do not know if this is the correct logic, but it will at least give you a starting point so you can visualize the logic flow and see what is in the variables.  You might give it a try and see if any surprises show up.

If you make these changes one thing is certain - your script will produce browser output when it calls the function.  If you make these changes and do not see browser output there is a logic error somewhere that is causing the function call to cer_capxsum() to be bypassed.

if(!function_exists('cer_capxsum')) 
{
    function cer_capxsum($mID)
    {
         global $edb;
         var_dump($edb);
         
         $sql_data = "exec cer_CapxSum $mID";
         var_dump($sql_data);
         
         if ($_SESSION["portaladmin"] == 1 && $_SESSION["showsql"] != "no")
         {
              $x = display_detail("cer/cer_functions.php", $sql_data);
              var_dump($x);
              
              $result_data = odbc_exec($edb, $sql_data);
              var_dump($result_data);
         }
         else
         {
             var_dump($_SESSION);
         }
    }
}
else
{
    trigger_error('Cer_CapXSum() ALREADY EXISTS', E_USER_ERROR);
}

Open in new window

Avatar of Bruce Gust

ASKER

I went back and retooled my code so I could better monitor what exactly was being chewed on and spit out. Here's what I did:

    if(!function_exists('cer_capxsum')) {
        function cer_capxsum($mID) {
         global $edb, $_SESSION;
         
         $data=array();
         $sql_data = "exec cer_CapxSum $mID";
          if ($_SESSION["portaladmin"] == 1 && $_SESSION["showsql"] != "no")
            echo display_detail("cer/cer_functions.php",$sql_data);
        $result_data = odbc_exec($edb, $sql_data);
            
            $count=0;
            while (odbc_fetch_row($result_data))
                  {
                  $count++;
                  $data[$count]["capxtotal"] = odbc_result($result_data, "capxtotal");
                   }  
             $data["count"]=$count;
             return $data;
    }

By doing this, I was able to incorporate the various pieces of counsel documented above and, as always, I'm grateful. Experts Exchange is the best Hamilton I part with every month!