Advertisement
Guest User

Untitled

a guest
Apr 17th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.10 KB | None | 0 0
  1. <?
  2. // Esto lo puse aca por que un día me interesaria probar estas funciones. Quizás este codigo o si no, por lo menos un indicio en que buscar o investigar.
  3.  
  4.  
  5. define("DB2_HOST", "10.0.1.232");
  6.  
  7. define("DB2_NAME", "DBNAME");
  8.  
  9. define("DB2_USER", "DBUSER");
  10.  
  11. define("DB2_PASS", "password");
  12.  
  13. /**
  14.  * Creates a PDO connection and executes a stored procedure
  15.  * using bound values and parameters
  16.  * Returns an associative array
  17.  *      array("CNUMBER", "CNAME", "CEMAIL", "CPHONE")
  18.  *
  19.  * If error, status1 = error, status2 = cause
  20.  *
  21.  * @param string $strContactNumber
  22.  * @return array
  23.  */
  24. function getContact($strContactNumber)
  25. {
  26.     $arrContact = array();
  27.        
  28.     $strContactNumber = strtoupper($strContactNumber);
  29.        
  30.     // construct the connection string
  31.     $dbName = DB2_NAME;
  32.     $dbHost = DB2_HOST;
  33.     $dbUsername = DB2_USER;
  34.     $dbPassword = DB2_PASS;
  35.        
  36.     // odbc is used to tell ODBC what driver set to use
  37.     $connectionString = "odbc:DRIVER={iSeries Access ODBC Driver}; ".
  38.                         "SYSTEM={$dbHost}; ".
  39.                         "DATABASE={$dbName}; ".
  40.                         "UID={$dbUsername}; ".
  41.                         "PWD={$dbPassword}; ";
  42.  
  43.     $intTries = 0;
  44.                            
  45.     // attempt to create a PDO connection
  46.     // try at least 5 times to connect
  47.     while( $intTries <= 5 )
  48.     {      
  49.         // increment the number of connection attempts
  50.         $intTries++;
  51.                    
  52.         try
  53.         {
  54.             $dbh = new PDO($connectionString, "", "");
  55.                
  56.         }   //end try
  57.        
  58.         catch (PDOException $e)
  59.         {
  60.             $arrAvailability["CNUMBER"] = $strContactNumber;
  61.            
  62.             $arrAvailability["CNAME"] = "Error";
  63.            
  64.             $arrAvailability["CEMAIL"] = "";
  65.            
  66.             $arrAvailability["CPHONE"] = "";
  67.                
  68.             $dbh = null;
  69.                
  70.         }   //end catch
  71.            
  72.         // if PDO was created, exit the loop
  73.         if( $dbh )
  74.             break;
  75.            
  76.     }   //end while loop
  77.  
  78.     // if PDO object was created, then run the statement
  79.     if( $dbh )
  80.     {
  81.         // execute a stored procedure
  82.         $sql = "call TOSLIB.SP_CONTACT ( :CNUMBER, :CNAME, :CEMAIL, :CPHONE)";
  83.            
  84.         // create parameter variables
  85.         $strContactName = "";
  86.            
  87.         $strContactEmail = "";
  88.            
  89.         // create a prepared statement
  90.         $statement = $dbh->prepare($sql);
  91.            
  92.         // bindValue sets the value of the named parameter
  93.         $statement->bindValue(':CNUMBER', $strContactNumber, PDO::PARAM_STR);
  94.            
  95.         // bind the parameters to the statement
  96.         // bound parameters allows you to see the final result
  97.         $statement->bindParam(':CNAME', $strContactName, PDO::PARAM_STR, 25);
  98.            
  99.         $statement->bindParam(':CEMAIL', $strContactEmail, PDO::PARAM_STR, 255);
  100.            
  101.         $statement->bindParam(':CPHONE', $strContactPhone, PDO::PARAM_STR, 15);
  102.            
  103.         // now execute the statement, the bound php variables will automagically be updatd
  104.         $blnExecuted = $statement->execute();
  105.            
  106.         // if the statement executed, then get the status results
  107.         if($blnExecuted)
  108.         {
  109.             // if there's a value, remove the special character at the end
  110.             if( strlen(trim($strContactName)) > 0 )
  111.             {
  112.                 // remove the last character
  113.                 $strContactName = substr($strContactName, 0, 24);
  114.                    
  115.             }   //end if
  116.                
  117.             // if there's a value, remove the special character at the end
  118.             if( strlen(trim($strContactEmail)) > 0 )
  119.             {
  120.                 // remove the last character
  121.                 $strContactEmail = substr($strContactEmail, 0, 254);
  122.                    
  123.             }   //end if
  124.                
  125.             // if there's a value, remove the special character at the end
  126.             if( strlen(trim($strContactEmail)) > 0 )
  127.             {
  128.                 // remove the last character
  129.                 $strContactPhone = substr($strContactPhone, 0, 14);
  130.                    
  131.             }   //end if
  132.                
  133.             $arrAvailability["CNUMBER"] = $strContactNumber;
  134.  
  135.             $arrAvailability["CNAME"] = $strContactName;
  136.            
  137.             $arrAvailability["CEMAIL"] = $strContactEmail;
  138.                
  139.             $arrAvailability["CPHONE"] = $strContactPhone;
  140.            
  141.         }   //end if
  142.            
  143.         // else the statement failed, add error description
  144.         else
  145.         {
  146.             $arrAvailability["CNUMBER"] = $strContactNumber;
  147.            
  148.             $arrAvailability["CNAME"] = "Error";
  149.                
  150.             $arrAvailability["CEMAIL"] = "";
  151.            
  152.             $arrAvailability["CPHONE"] = "";
  153.                
  154.         }   //end else
  155.            
  156.     }   //end if
  157.        
  158.     return $arrAvailability;
  159.                
  160. }   //end function
  161.    
  162. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement