Jump to content

Archived

This topic is now archived and is closed to further replies.

Cep

Custom Function gives incorrect results but logic is valid.

Recommended Posts

Hi,

I cannot work this one out and its beginning to do my head in.

Basically I have a function which will test to see if a records exists for an order. The idea being that if one exists the user is told they cannot add a new one. There is however a switch as a record may exist for an order but it may be a test order, this is flagged in another table which is related to the order via an ID field held in both tables.

My function will take in the necessary information to query the database and should then count the results. If a row is found the function returns a value of true if not it will return false.

This works perfectly fine for regular orders but when I then test to see if it is a test order using the switch when I know there is a test order the function seems to be returning false as if no records are there. I even rewrote the function to kill the php if no value was found for the switch (as if it wasnt being read in at all) but it seems to be receiving a value so I dont get it.

[code]
function order_check($clid, $byearid, $isprebudget) {

        if ($isprebudget=="No") {

        //Check that order for client as not already been put on system
        $sql = "SELECT * FROM `BsysOrders` WHERE clientID = {$clid} AND byearID = {$byearid}";

        $result = odbc_exec(db_src(), $sql) or die("SQL Error: Function order_check - unable to run select sql");

        $num_rows = odbc_record_count($result, db_src(), $sql);

        } elseif ($isprebudget=="Yes") {
         
        $sql = "SELECT * FROM `BsysOrders` WHERE clientID = {$clid} AND byearID = {$byearid}";

        $result = odbc_exec(db_src(), $sql) or die("SQL Error: Function order_check - unable to run select sql");

        $row = odbc_fetch_array(odbc_exec(db_src(), $sql)) or die ("SQL Error: Function order_check - unable to fetch array");

        $bborderid = $row['bborderID'];

        $newsql = "SELECT * FROM `BsysBudgetBooklet` WHERE bborderID = {$bborderid} AND Prebudget = '{$isprebudget}'";

        $newresult = odbc_exec(db_src(), $newsql) or die("SQL Error: Function order_check - unable to run 2nd select sql");

        $num_rows = odbc_record_count($newresult, db_src(), $newsql);
       
        } else {
        die("No prebudget value is being passed");
        }
       
        if ($num_rows==0) {
                $order_exists = false;
        } else {
                $order_exists = true;
        }

/*
        //Check that order for client as not already been put on system
        $sql = "SELECT * FROM `BsysOrders` WHERE clientID = {$clid} AND byearID = {$byearid}";

        $result = odbc_exec(db_src(), $sql) or die("SQL Error: Function order_check - unable to run select sql");

        $num_rows = odbc_record_count($result, db_src(), $sql);

        if ($num_rows==0) {
              $order_exists = false;
        } else {
              $row = odbc_fetch_array(odbc_exec(db_src(), $sql)) or die ("SQL Error: Function order_check - unable to fetch array");

              $bborderid = $row['bborderID'];

              $newsql = "SELECT bborderID FROM `BsysBudgetBooklet` WHERE bborderID = {$bborderid} AND Prebudget = '{$isprebudget}'";

              $newresult = odbc_exec(db_src(), $newsql) or die("SQL Error: Function order_check - unable to run 2nd select sql");

              $new_num = odbc_record_count($newresult, db_src(), $newsql);

              if ($new_num==0) {
                $order_exists = false;
              } else {
                $order_exists = true;
              }
        }
*/
return $order_exists;
}
[/code]


This is the odbc count records function which I know works but you may need to see what it is.

[code]
function odbc_record_count ($result, $db, $query) {
  $numRecords = odbc_num_rows ($result);
  if ($numRecords < 0)
  {
  $countQueryString = "SELECT count(*) as results FROM (".$query.")";
  $count = odbc_exec ($db, $countQueryString);
  $numRecords = odbc_result ($count, "results");
  }

  return $numRecords;
}
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.