function query($sql, $conn) { try { $result = db2_exec($conn, $sql); $count = 0; while ($row = db2_fetch_object($result)) { echo $row->NAME1 . ", " . $row->NAME2 . " " . $row->STREET . ", " . $row->CITY . ", " . $row->STATE . ", " . $row->ZIP . ", " . $row->COUNTY . ", " . $row->LONG . ", " . $row->LAT . "<br>"; $count++; } //print_r($result); if ($result) { echo "Query Successful<br>" . $count . " total entries<br>"; } } catch (Exception $e) { echo "Query Failed<br>"; echo "Exception: " . $e->getMessage() . "<br>"; echo db2_conn_error() . "<br>"; echo db2_conn_errormsg() . "<br>"; } }
/** * * * @todo Throw in your "transport/adapter" framework for a real OO look and feel .... * Throw new Exception("Fail execute ($sql) ".db2_stmt_errormsg(),db2_stmt_error()); * ... and retrieve via try/catch + Exception methods. * * @param $database * @param $user * @param $password * @param null $options 'persistent' is one option * @return bool */ public function connect($database, $user, $password, $options = null) { // Compensate for older ibm_db2 driver that may not do this check. if ($user && empty($password)) { $this->setErrorCode('08001'); $this->setErrorMsg('Authorization failure on distributed database connection attempt. SQLCODE=-30082'); return false; } if ($options) { if (isset($options['persistent']) && $options['persistent']) { $conn = db2_pconnect($database, $user, $password); } else { $conn = db2_connect($database, $user, $password); } if (is_resource($conn)) { return $conn; } } $this->setErrorCode(db2_conn_error()); $this->setErrorMsg(db2_conn_errormsg()); return false; }
/** * Rollback a transaction. * * @return void */ protected function _rollBack() { if (!db2_rollback($this->_connection)) { /** * @see Zend_Db_Adapter_Db2_Exception */ require_once 'Zend/Db/Adapter/Db2/Exception.php'; throw new Zend_Db_Adapter_Db2_Exception(db2_conn_errormsg($this->_connection), db2_conn_error($this->_connection)); } $this->_setExecuteMode(DB2_AUTOCOMMIT_ON); }
function insertIntoRestaurant($name1, $street, $city, $state, $zip, $long, $lat, $conn, $cities) { $sql = "insert into " . userAccount . ".restaurant values('" . db2_escape_string($name1) . "', NULL, '" . db2_escape_string($street) . "', '" . db2_escape_string($city) . "', '" . db2_escape_string($state) . "', '" . db2_escape_string($zip) . "', '" . COUNTY . "', " . $long . ", " . $lat . ", db2gse.ST_Point(" . $long . ", " . $lat . ", 1))"; if (array_key_exists($city, $GLOBALS['cities'])) { //Hashmap lookup to filter unwanted cities, O(1) /* $result = db2_exec( $GLOBALS['conn'] , $sql ); if(!$result){ //log failure //$sql .= "\r\n"; saveToFile(errorFile, $sql."\r\n"); }*/ try { $result = db2_exec($GLOBALS['conn'], $sql); //saveToFile(errorFile1, $sql."\r\n"); } catch (Exception $e) { //log failure //$sql .= "\r\n"; saveToFile(errorFile1, $sql . "\r\n"); echo "Query Failed<br>"; echo "Exception: " . $e->getMessage() . "<br>"; echo db2_conn_error() . "<br>"; echo db2_conn_errormsg() . "<br>"; } } else { //log rejected city //$sql .= "\r\n"; saveToFile(errorFile2, $sql . "\r\n"); } }
function ErrorNo() { if ($this->_haserrorfunctions) { if ($this->_errorCode !== false) { // bug in 4.0.6, error number can be corrupted string (should be 6 digits) return strlen($this->_errorCode) <= 2 ? 0 : $this->_errorCode; } if (empty($this->_connectionID)) { $e = @db2_conn_error(); } else { $e = @db2_conn_error($this->_connectionID); } // bug in 4.0.6, error number can be corrupted string (should be 6 digits) // so we check and patch if (strlen($e) <= 2) { return 0; } return $e; } else { return ADOConnection::ErrorNo(); } }
/** * This function rollbacks a transaction. * * @access public * @override * @throws Throwable_SQL_Exception indicates that the executed * statement failed * * @see http://www.php.net/manual/en/function.db2-rollback.php */ public function rollback() { if (!$this->is_connected()) { throw new Throwable_SQL_Exception('Message: Failed to rollback SQL transaction. Reason: Unable to find connection.'); } $command = @db2_rollback($this->resource); if ($command === FALSE) { throw new Throwable_SQL_Exception('Message: Failed to rollback SQL transaction. Reason: :reason', array(':reason' => @db2_conn_error($this->resource))); } @db2_autocommit($this->resource, DB2_AUTOCOMMIT_ON); $this->sql = 'ROLLBACK;'; }
/** * Returns a formatted error message from previous database operation. * DB2 distinguishes between statement and connnection errors so we * must check for both. * * @return string Error message with error number */ function lastError() { if (db2_stmt_error()) { return db2_stmt_error() . ': ' . db2_stmt_errormsg(); } elseif (db2_conn_error()) { return db2_conn_error() . ': ' . db2_conn_errormsg(); } return null; }
/** * Get the last error number * Return 0 if no error * @return integer */ public function lastErrno() { $connerr = db2_conn_error(); if ($connerr) { return $connerr; } $stmterr = db2_stmt_error(); if ($stmterr) { return $stmterr; } return 0; }
/** * Executes a prepared statement. * * @param array $params * @return void * @throws Zend_Db_Statement_Db2_Exception */ public function execute(array $params = array()) { if (!$this->_stmt) { $connection = $this->_connection->getConnection(); $sql = $this->_joinSql(); $this->_stmt = db2_prepare($connection, $sql); } if (!$this->_stmt) { require_once 'Zend/Db/Statement/Db2/Exception.php'; throw new Zend_Db_Statement_Db2_Exception(db2_conn_errormsg($connection), db2_conn_error($connection)); } $success = db2_execute($this->_stmt, $params); if (!$success) { require_once 'Zend/Db/Statement/Db2/Exception.php'; throw new Zend_Db_Statement_Db2_Exception(db2_stmt_errormsg($this->_stmt), db2_stmt_error($this->_stmt)); } $this->_keys = array(); if ($field_num = $this->columnCount()) { for ($i = 0; $i < $field_num; $i++) { $name = db2_field_name($this->_stmt, $i); $this->_keys[] = $name; } } $this->_values = array(); if ($this->_keys) { $this->_values = array_fill(0, count($this->_keys), null); } }
/** * Rollback a transaction. * * @return void */ protected function _rollBack() { if (!db2_rollback($this->_connection)) { throw new Db2Exception(db2_conn_errormsg($this->_connection), db2_conn_error($this->_connection)); } $this->_setExecuteMode(Db2_AUTOCOMMIT_ON); }
<?php require_once 'nav.php'; require_once 'config.php'; $connection = db2_connect($dbname, $username, $password); if (!$connection) { die('Not connected : ' . db2_conn_error()); } ?> <!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. --> <html> <head> <title>AuctionHub</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" type="text/css" href="resources/bootstrap-select/dist/css/bootstrap-select.min.css"> <style type="text/css"> /* Fixes submit button height problem in Firefox */ .tfbutton::-moz-focus-inner { border: 0; } .tfclear{ clear:both; }
/** * (non-PHPdoc) * @see DBManager::lastDbError() */ public function lastDbError() { if (db2_conn_error()) { return "IBM_DB2 connection error " . db2_conn_error() . ": " . db2_conn_errormsg(); } /* FIXME: * Added $connOnly parameter to skip the statement error check * as there is a statics bug in the DB2 driver which persists failures * http://pecl.php.net/bugs/bug.php?id=22854 */ if (!$this->ignoreErrors) { $error = db2_stmt_error(); if ($error) { return "IBM_DB2 statement error " . $error . ": " . db2_stmt_errormsg(); } } return false; }
function errorCode() { return db2_conn_error($this->_conn); }
$user = "******"; $pass = "******"; try { $conn = db2_connect($database, $user, $pass); } catch (Exception $e) { echo "Exception: " . $e->getMessage(); } if ($conn) { $sql = "select CID, SUBJECT, PRCID from course"; $stmt = db2_prepare($conn, $sql); if ($stmt) { $result = db2_execute($stmt); } else { echo "No results"; } echo '<table>'; echo '<thead><tr><th>' . 'CID' . '</th>' . '<th>' . 'SUBJECT' . '</th>' . '<th>' . 'PREREQ' . '</th></tr></thead>'; while ($row = db2_fetch_array($stmt)) { echo "<tbody><tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><td>" . $row[2] . "</td></tr></tbody>"; } echo '</table>'; db2_close($conn); } else { echo db2_conn_error() . "<br>"; echo db2_conn_errormsg() . "<br>"; echo "Connection failed.<br>"; } ?> </body>
/** * Executes a prepared statement. * * @param array $params OPTIONAL Values to bind to parameter placeholders. * @return bool * @throws Zend_Db_Statement_Db2_Exception */ public function execute(array $params = array()) { if (!$this->_stmt) { return false; } if (!$this->_stmt) { require_once 'Zend/Db/Statement/Db2/Exception.php'; throw new Zend_Db_Statement_Db2_Exception(db2_conn_errormsg($connection), db2_conn_error($connection)); } $retval = @db2_execute($this->_stmt, $params); if ($retval === false) { require_once 'Zend/Db/Statement/Db2/Exception.php'; throw new Zend_Db_Statement_Db2_Exception(db2_stmt_errormsg($this->_stmt), db2_stmt_error($this->_stmt)); } $this->_keys = array(); if ($field_num = $this->columnCount()) { for ($i = 0; $i < $field_num; $i++) { $name = db2_field_name($this->_stmt, $i); $this->_keys[] = $name; } } $this->_values = array(); if ($this->_keys) { $this->_values = array_fill(0, count($this->_keys), null); } return $retval; }