Solved

What am I doing wrong with this function?

Posted on 2014-02-28
8
277 Views
Last Modified: 2014-02-28
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
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39894866
I suspect it's because $capxsum is an object, not an array.

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

HTH,
Dan
0
 
LVL 35

Assisted Solution

by:Dan Craciun
Dan Craciun earned 166 total points
ID: 39894873
You might also try
$row = odbc_fetch_row($capxsum);
echo $row["capxtotal"];

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39894898
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 57

Accepted Solution

by:
Julian Hansen earned 167 total points
ID: 39894951
odbc_exec returns a result resource.

To return the value you have to call odbc_fetch_row
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 167 total points
ID: 39894956
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39895033
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
 

Author Closing Comment

by:brucegust
ID: 39895192
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question