Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What am I doing wrong with this function?

Posted on 2014-02-28
8
Medium Priority
?
284 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 664 total points
ID: 39894873
You might also try
$row = odbc_fetch_row($capxsum);
echo $row["capxtotal"];

Open in new window

0
 
LVL 111

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
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.

 
LVL 59

Accepted Solution

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

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

Assisted Solution

by:Ray Paseur
Ray Paseur earned 668 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 111

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

722 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