Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- // 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.
- define("DB2_HOST", "10.0.1.232");
- define("DB2_NAME", "DBNAME");
- define("DB2_USER", "DBUSER");
- define("DB2_PASS", "password");
- /**
- * Creates a PDO connection and executes a stored procedure
- * using bound values and parameters
- * Returns an associative array
- * array("CNUMBER", "CNAME", "CEMAIL", "CPHONE")
- *
- * If error, status1 = error, status2 = cause
- *
- * @param string $strContactNumber
- * @return array
- */
- function getContact($strContactNumber)
- {
- $arrContact = array();
- $strContactNumber = strtoupper($strContactNumber);
- // construct the connection string
- $dbName = DB2_NAME;
- $dbHost = DB2_HOST;
- $dbUsername = DB2_USER;
- $dbPassword = DB2_PASS;
- // odbc is used to tell ODBC what driver set to use
- $connectionString = "odbc:DRIVER={iSeries Access ODBC Driver}; ".
- "SYSTEM={$dbHost}; ".
- "DATABASE={$dbName}; ".
- "UID={$dbUsername}; ".
- "PWD={$dbPassword}; ";
- $intTries = 0;
- // attempt to create a PDO connection
- // try at least 5 times to connect
- while( $intTries <= 5 )
- {
- // increment the number of connection attempts
- $intTries++;
- try
- {
- $dbh = new PDO($connectionString, "", "");
- } //end try
- catch (PDOException $e)
- {
- $arrAvailability["CNUMBER"] = $strContactNumber;
- $arrAvailability["CNAME"] = "Error";
- $arrAvailability["CEMAIL"] = "";
- $arrAvailability["CPHONE"] = "";
- $dbh = null;
- } //end catch
- // if PDO was created, exit the loop
- if( $dbh )
- break;
- } //end while loop
- // if PDO object was created, then run the statement
- if( $dbh )
- {
- // execute a stored procedure
- $sql = "call TOSLIB.SP_CONTACT ( :CNUMBER, :CNAME, :CEMAIL, :CPHONE)";
- // create parameter variables
- $strContactName = "";
- $strContactEmail = "";
- // create a prepared statement
- $statement = $dbh->prepare($sql);
- // bindValue sets the value of the named parameter
- $statement->bindValue(':CNUMBER', $strContactNumber, PDO::PARAM_STR);
- // bind the parameters to the statement
- // bound parameters allows you to see the final result
- $statement->bindParam(':CNAME', $strContactName, PDO::PARAM_STR, 25);
- $statement->bindParam(':CEMAIL', $strContactEmail, PDO::PARAM_STR, 255);
- $statement->bindParam(':CPHONE', $strContactPhone, PDO::PARAM_STR, 15);
- // now execute the statement, the bound php variables will automagically be updatd
- $blnExecuted = $statement->execute();
- // if the statement executed, then get the status results
- if($blnExecuted)
- {
- // if there's a value, remove the special character at the end
- if( strlen(trim($strContactName)) > 0 )
- {
- // remove the last character
- $strContactName = substr($strContactName, 0, 24);
- } //end if
- // if there's a value, remove the special character at the end
- if( strlen(trim($strContactEmail)) > 0 )
- {
- // remove the last character
- $strContactEmail = substr($strContactEmail, 0, 254);
- } //end if
- // if there's a value, remove the special character at the end
- if( strlen(trim($strContactEmail)) > 0 )
- {
- // remove the last character
- $strContactPhone = substr($strContactPhone, 0, 14);
- } //end if
- $arrAvailability["CNUMBER"] = $strContactNumber;
- $arrAvailability["CNAME"] = $strContactName;
- $arrAvailability["CEMAIL"] = $strContactEmail;
- $arrAvailability["CPHONE"] = $strContactPhone;
- } //end if
- // else the statement failed, add error description
- else
- {
- $arrAvailability["CNUMBER"] = $strContactNumber;
- $arrAvailability["CNAME"] = "Error";
- $arrAvailability["CEMAIL"] = "";
- $arrAvailability["CPHONE"] = "";
- } //end else
- } //end if
- return $arrAvailability;
- } //end function
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement