Solved

What am I doing wrong with this function?

Posted on 2014-02-28
8
267 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
8 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
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 34

Assisted Solution

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

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 51

Accepted Solution

by:
Julian Hansen earned 167 total points
Comment Utility
odbc_exec returns a result resource.

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

Assisted Solution

by:Ray Paseur
Ray Paseur earned 167 total points
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now