Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

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?
0
brucegust
Asked:
brucegust
3 Solutions
 
Dan CraciunIT ConsultantCommented:
I suspect it's because $capxsum is an object, not an array.

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

HTH,
Dan
0
 
Dan CraciunIT ConsultantCommented:
You might also try
$row = odbc_fetch_row($capxsum);
echo $row["capxtotal"];

Open in new window

0
 
Ray PaseurCommented:
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Julian HansenCommented:
odbc_exec returns a result resource.

To return the value you have to call odbc_fetch_row
0
 
Ray PaseurCommented:
One other reason you may not be seeing anything would be if the function is not returning anything.  Check to see that you have a return statement.
http://php.net/manual/en/function.return.php
0
 
Ray PaseurCommented:
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

0
 
brucegustAuthor Commented:
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!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now