public static function castResult($result, array $a, Stub $stub, $isNested) { $a['num rows'] = pg_num_rows($result); $a['status'] = pg_result_status($result); if (isset(self::$resultStatus[$a['status']])) { $a['status'] = new ConstStub(self::$resultStatus[$a['status']], $a['status']); } $a['command-completion tag'] = pg_result_status($result, PGSQL_STATUS_STRING); if (-1 === $a['num rows']) { foreach (self::$diagCodes as $k => $v) { $a['error'][$k] = pg_result_error_field($result, $v); } } $a['affected rows'] = pg_affected_rows($result); $a['last OID'] = pg_last_oid($result); $fields = pg_num_fields($result); for ($i = 0; $i < $fields; ++$i) { $field = array('name' => pg_field_name($result, $i), 'table' => sprintf('%s (OID: %s)', pg_field_table($result, $i), pg_field_table($result, $i, true)), 'type' => sprintf('%s (OID: %s)', pg_field_type($result, $i), pg_field_type_oid($result, $i)), 'nullable' => (bool) pg_field_is_null($result, $i), 'storage' => pg_field_size($result, $i) . ' bytes', 'display' => pg_field_prtlen($result, $i) . ' chars'); if (' (OID: )' === $field['table']) { $field['table'] = null; } if ('-1 bytes' === $field['storage']) { $field['storage'] = 'variable size'; } elseif ('1 bytes' === $field['storage']) { $field['storage'] = '1 byte'; } if ('1 chars' === $field['display']) { $field['display'] = '1 char'; } $a['fields'][] = new EnumStub($field); } return $a; }
function PG_QueryStart($SQL, &$aFields, &$link, &$Errors) { $gMyHOST = "127.0.0.1"; $gMyDB = "osm"; $gMyUSER = "******"; $gMyPASS = "******"; $Errors = ""; $result = false; $conn_string = "host={$gMyHOST} port=5432 dbname={$gMyDB} user={$gMyUSER} password={$gMyPASS}"; $link = pg_connect($conn_string); if ($link === false) { $Errors .= "Impossibile connettersi a '{$gMyHOST}' come '{$gMyUSER}'"; } else { $result = pg_query($SQL); if ($result === false) { $Errors .= ($Errors[0] ? "" : "<br>") . "Errore durante la query '{$SQL}': " . pg_last_error(); } else { $nFields = pg_num_fields($result); for ($ind = 0; $ind < $nFields; $ind++) { $aFields[$ind] = array(pg_field_name($result, $ind), pg_field_type($result, $ind)); } } } return $result; }
function query_start($query) { // For reg expressions $query = trim($query); // Query was an insert, delete, update, replace if (preg_match("/^(insert|delete|update|replace)\\s+/i", $query)) { return false; } // Flush cached values.. $this->flush(); // Log how the function was called $this->func_call = "\$db->query_start(\"{$query}\")"; // Keep track of the last query for debug.. $this->last_query = $query; // Perform the query via std pg_query function.. if (!($this->result = @pg_query($this->dbh, $query))) { $this->print_error(); return false; } $this->num_queries++; // ======================================================= // Take note of column info $i = 0; while ($i < @pg_num_fields($this->result)) { $this->col_info[$i]->name = pg_field_name($this->result, $i); $this->col_info[$i]->type = pg_field_type($this->result, $i); $this->col_info[$i]->size = pg_field_size($this->result, $i); $i++; } $this->last_result = array(); $this->num_rows = 0; // If debug ALL queries $this->trace || $this->debug_all ? $this->debug() : null; return true; }
function postgis_query($string, $pgc = NULL) { $pgct = $pgc; if ($pgc == NULL) { $pgct = postgis_connect(); } @($result = pg_query($pgct, $string)); if ($pgc == NULL) { pg_close($pgct); } $retval = array(); if (!$result) { return $retval; } $arow = array(); for ($i = 0; $i < pg_num_fields($result); $i++) { $arow[pg_field_name($result, $i)] = pg_field_type($result, $i); } $retval[0] = $arow; $ctr = 1; while ($row = pg_fetch_row($result)) { $arow = array(); for ($i = 0; $i < count($row); $i++) { $arow[pg_field_name($result, $i)] = $row[$i]; } $retval[$ctr] = $arow; $ctr++; } pg_free_result($result); return $retval; }
public function getTypes() { $types = []; $count = pg_num_fields($this->result); for ($i = 0; $i < $count; $i++) { $nativeType = pg_field_type($this->result, $i); $types[pg_field_name($this->result, $i)] = [0 => isset(self::$types[$nativeType]) ? self::$types[$nativeType] : self::TYPE_AS_IS, 1 => $nativeType]; } return $types; }
/** * Constructor * * @param resource handle */ public function __construct($result, TimeZone $tz = NULL) { $fields = array(); if (is_resource($result)) { for ($i = 0, $num = pg_num_fields($result); $i < $num; $i++) { $fields[pg_field_name($result, $i)] = pg_field_type($result, $i); } } parent::__construct($result, $fields, $tz); }
function _performGetBlobFieldNames($result) { $blobFields = array(); for ($i = pg_num_fields($result) - 1; $i >= 0; $i--) { $type = pg_field_type($result, $i); if (strpos($type, "BLOB") !== false) { $blobFields[] = pg_field_name($result, $i); } } return $blobFields; }
function GetFields() { $_fields = array(); $_result = pg_query($this->_Link, $this->SelectCommand); $_num_fields = pg_num_fields($_result); for ($i = 0; $i < $_num_fields; $i++) { $_field = array("Name" => pg_field_name($_result, $i), "Type" => pg_field_type($_result, $i), "Not_Null" => 0); array_push($_fields, $_field); } return $_fields; }
/** * @param String strSQL * @return Array */ public function db_getfieldslist($strSQL) { $res = array(); $qResult = $this->connectionObj->query($strSQL); $fieldsNumber = $qResult->numFields(); for ($i = 0; $i < $fieldsNumber; $i++) { $stype = pg_field_type($qResult->getQueryHandle(), $i); $ntype = $this->getFeldTypeNumber($stype); $res[$i] = array("fieldname" => $qResult->fieldName($i), "type" => $ntype, "is_nullable" => 0); } return $res; }
public function getColumnMeta($column) { if ($column >= $this->columnCount()) { return false; } $result = array(); $result['native_type'] = pg_field_type($this->_result, $column); $result['table'] = pg_field_table($this->_result, $column); $result['name'] = pg_field_name($this->_result, $column); $result['len'] = pg_field_prtlen($this->_result, $column); $result['pdo_type'] = PDO::PARAM_STR; return $result; }
function db_getfieldslist($strSQL) { global $conn; $res=array(); $rs=db_query($strSQL,$conn); for($i=0;$i<db_numfields($rs);$i++) { $stype=pg_field_type($rs,$i); $ntype=db_fieldtypenum($stype); $res[$i]=array("fieldname"=>db_fieldname($rs,$i),"type"=>$ntype,"is_nullable"=>0); } return $res; }
/** * Generates an array of objects representing field meta-data. * * @return array */ public function getFieldData() : array { $retval = []; for ($i = 0, $c = $this->getFieldCount(); $i < $c; $i++) { $retval[$i] = new \stdClass(); $retval[$i]->name = pg_field_name($this->resultID, $i); $retval[$i]->type = pg_field_type($this->resultID, $i); $retval[$i]->max_length = pg_field_size($this->resultID, $i); // $retval[$i]->primary_key = (int)($fieldData[$i]->flags & 2); // $retval[$i]->default = $fieldData[$i]->def; } return $retval; }
public function __construct($resource) { if (!is_resource($resource) || is_resource($resource) && get_resource_type($resource) !== self::EXPECTED_RESOURCE_TYPE) { throw new DatabaseException('Invalid resource type passed, expected "' . $this->expectedResource . '" and got "' . get_resource_type($resource) . '"'); } $this->resource = $resource; $this->count = pg_num_rows($this->resource); // Build an array of column types $this->fieldCount = pg_num_fields($this->resource); for ($i = 0; $i < $this->fieldCount; ++$i) { $this->types[$i] = [pg_field_name($this->resource, $i), pg_field_type($this->resource, $i)]; } }
public function __construct($result) { $this->result = $result; $this->row = 0; $this->max = pg_num_rows($result); if ($this->max > 0) { $this->types = []; $num_fields = pg_num_fields($result); for ($i = 0; $i < $num_fields; $i++) { $this->types[pg_field_name($result, $i)] = pg_field_type($result, $i); } } }
/** * Structure of our fields (type, length and null) * * @param resource $resource * @return array */ public function field_structures($resource) { $result = []; if ($resource) { for ($i = 0; $i < pg_num_fields($resource); $i++) { $name = pg_field_name($resource, $i); $result[$name]['type'] = pg_field_type($resource, $i); $result[$name]['null'] = pg_field_is_null($resource, $i); $result[$name]['length'] = pg_field_size($resource, $i); } } return $result; }
/** * Build a new recordset to iterate over. * * @param resource $result A pg_query() result object to create a recordset from. */ public function __construct($result) { $this->result = $result; // Find out if there are any blobs. $numfields = pg_num_fields($result); for ($i = 0; $i < $numfields; $i++) { $type = pg_field_type($result, $i); if ($type == 'bytea') { $this->blobs[] = pg_field_name($result, $i); } } $this->current = $this->fetch_next(); }
/** * Field data * * Generates an array of objects containing field meta-data * * @access public * @return array */ function field_data() { $retval = array(); for ($i = 0; $i < $this->num_fields(); $i++) { $F = new stdClass(); $F->name = pg_field_name($this->result_id, $i); $F->type = pg_field_type($this->result_id, $i); $F->max_length = pg_field_size($this->result_id, $i); $F->primary_key = 0; $F->default = ''; $retval[] = $F; } return $retval; }
function fieldsMemory($rs, $idx, $formata = false, $mostra = false, $lEncode = false) { $oFields = new _db_fields(); $numFields = pg_num_fields($rs); $iTotalLinhas = pg_num_rows($rs); for ($i = 0; $i < $numFields; $i++) { $sValor = ""; $sFieldName = @pg_field_name($rs, $i); $sFieldType = @pg_field_type($rs, $i); if ($iTotalLinhas > 0) { $sValor = trim(@pg_result($rs, $idx, $sFieldName)); } if ($formata) { switch ($sFieldType) { case "date": if ($sValor != null) { $sValor = implode('/', array_reverse(explode("-", $sValor))); } break; default: $sValor = stripslashes($sValor); break; } } if ($mostra) { echo $sFieldName . " => " . $sValor . " <br>"; } if ($lEncode) { switch ($sFieldType) { case "bpchar": $sValor = urlencode($sValor); break; case "varchar": $sValor = urlencode($sValor); break; case "text": $sValor = urlencode($sValor); break; } } $oFields->{$sFieldName} = $sValor; } return $oFields; }
/** * Dynamic Get Function Override * * @param $name * A string containing the name of the property to get. * @return * Value of the property. */ public function __get($propertyName) { global $firePHP; if ($propertyName == 'columns') { if (!isset($this->_columns)) { //---- Get Columns $this->_columns = new anvilCollection(); $i = 0; // $sql = 'SHOW COLUMNS FROM '; while ($i < pg_num_fields($this->result)) { $newColumn = new anvilData_postgresql_Column(pg_field_name($this->result, $i), pg_field_type($this->result, $i)); $this->_columns->add($newColumn); $i++; } } return $this->_columns; } else { return parent::__get($propertyName); } }
/** * @param resource from pg_query() or pg_get_result() * @param string SQL used to create this result * @param resource from pg_connect() or pg_pconnect() * @param boolean|string * @return void */ public static function factory($result, $sql, $link, $return_objects) { // Detect errors, initialize values $postgresql_result = new Database_Postgresql_Result($result, $sql, $link, $return_objects); // No rows, nothing to transform if ($postgresql_result->total_rows === 0) { return $postgresql_result; } $booleans = FALSE; // Create list of boolean field names for ($i = pg_num_fields($result) - 1; $i >= 0; --$i) { if (pg_field_type($result, $i) === 'bool') { $booleans[] = pg_field_name($result, $i); } } // No booleans to transform, regular result set is fastest if ($booleans === FALSE) { return $postgresql_result; } return new Database_Postgresql_Result_Boolean($postgresql_result, $booleans); }
function aff_table($res) { echo "<table border=\"1px\" witdh='50%'>"; echo "<tr>"; for ($i = 0; $i < pg_num_fields($res); $i++) { echo "<th>" . pg_field_name($res, $i) . "</th>"; } echo "</tr>"; for ($i = 0; $i < pg_num_rows($res); $i++) { echo "<tr>"; for ($j = 0; $j < pg_num_fields($res); $j++) { if (pg_field_type($res, $j) == "bytea") { $table = pg_field_table($res, 0); echo "<td><img width='60%' src='photo.php?table=" . $table . "&id=" . pg_field_name($res, 0) . "&valeur=" . pg_fetch_result($res, $i, 0) . "&champ=" . pg_field_name($res, $j) . "'></td>"; } else { echo "<td>" . pg_fetch_result($res, $i, $j) . "</td>"; } } echo "</tr>"; } echo "</table>"; }
/** * pg_field_type() wrapper * @param ResultWrapper|resource $res ResultWrapper or PostgreSQL query result resource * @param int $index Field number, starting from 0 * @return string */ function fieldType($res, $index) { if ($res instanceof ResultWrapper) { $res = $res->result; } return pg_field_type($res, $index); }
/** * Field data. * * Generates an array of objects containing field meta-data * * @return array */ public function field_data() { $retval = []; for ($i = 0, $c = $this->num_fields(); $i < $c; $i++) { $retval[$i] = new stdClass(); $retval[$i]->name = pg_field_name($this->result_id, $i); $retval[$i]->type = pg_field_type($this->result_id, $i); $retval[$i]->max_length = pg_field_size($this->result_id, $i); } return $retval; }
function _generate_pgxml($result = NULL) { if (!$result) { return false; } echo "<?xml version=\"1.0\""; echo " encoding=" . '"' . "US-ASCII" . '"'; echo " ?>\n"; echo "<data>\n"; // Output header row $j = 0; echo "\t<header>\n"; $totalFields = @pg_num_fields($result); for ($i = 0; $i < $totalFields; $i++) { $name = htmlspecialchars(@pg_field_name($result, $i)); $type = htmlspecialchars(@pg_field_type($result, $i)); echo "\t\t<column name=\"{$name}\" type=\"{$type}\" />\n"; } echo "\t</header>\n"; echo "\t<records>\n"; $totalRows = @pg_num_rows($result); for ($k = 0; $k < $totalRows; $k++) { $j = 0; echo "\t\t<row>\n"; for ($l = 0; $l < $totalFields; $l++) { $name = htmlspecialchars(@pg_field_name($result, $l)); $v = htmlspecialchars(@pg_fetch_result($result, $k, $l)); if ($v != null) { $v = htmlspecialchars($v); } echo "\t\t\t<column name=\"{$name}\"", $v == null ? ' null="null"' : '', ">{$v}</column>\n"; } echo "\t\t</row>\n"; } echo "\t</records>\n"; echo "</data>\n"; }
function field_type ($sql,$f) { if(isset($sql) && isset($f)) { if(strtoupper($_SESSION["gestor"])==strtoupper("mysql")) $this->fetcharray = mysql_field_type($sql,$f); if(strtoupper($_SESSION["gestor"])==strtoupper("postgre")) $this->fetcharray = pg_field_type($sql,$f); return $this->fetcharray; } }
public function execute($sql) { // hide errors $ini_err = ini_get('display_errors'); ini_set('display_errors', 0); $res = false; $this->res_errMsg = null; // --- process sql if ($this->dbType == 'postgres') { $this->res_data = pg_query($this->dbConn, $sql); if (!$this->res_data) { $this->res_errMsg = pg_last_error($this->dbConn); } else { $this->res_errMsg = pg_result_error($this->res_data); $this->res_affectedRows = pg_affected_rows($this->res_data); $this->res_rowCount = pg_num_rows($this->res_data); $this->res_fieldCount = pg_num_fields($this->res_data); $res = new dbRecordSet($this->dbType, $this->res_data, $this->res_rowCount, $this->res_fieldCount); // -- parse field names for ($i = 0; $i < $this->res_fieldCount; $i++) { $this->res_fields[$i] = pg_field_name($this->res_data, $i); $this->res_fieldsInfo[$i] = array(); $this->res_fieldsInfo[$i]['type'] = pg_field_type($this->res_data, $i); $this->res_fieldsInfo[$i]['len'] = pg_field_size($this->res_data, $i); $this->res_fieldsInfo[$i]['is_null'] = pg_field_is_null($this->res_data, $i); $this->res_fieldsInfo[$i]['prt_len'] = pg_field_prtlen($this->res_data, $i); } } // log error if ($this->res_errMsg != '') { // put here code to log error } } // --- mysql if ($this->dbType == 'mysql') { $this->res_data = mysql_query($sql, $this->dbConn); if (!$this->res_data) { $this->res_errMsg = mysql_error($this->dbConn); } else { @($this->res_errMsg = mysql_error($this->res_data)); @($this->res_affectedRows = mysql_affected_rows($this->res_data)); @($this->res_rowCount = mysql_num_rows($this->res_data)); @($this->res_fieldCount = mysql_num_fields($this->res_data)); @($res = new dbRecordSet($this->dbType, $this->res_data, $this->res_rowCount, $this->res_fieldCount)); // -- parse field names for ($i = 0; $i < $this->res_fieldCount; $i++) { $this->res_fields[$i] = mysql_field_name($this->res_data, $i); $this->res_fieldsInfo[$i] = array(); $this->res_fieldsInfo[$i]['type'] = mysql_field_type($this->res_data, $i); $this->res_fieldsInfo[$i]['len'] = mysql_field_len($this->res_data, $i); $this->res_fieldsInfo[$i]['flags'] = mysql_field_flags($this->res_data, $i); } } // log error if ($this->res_errMsg != '') { // put here code to log error } } $this->res_sql = $sql; // show debug info if on if ($this->debug == true) { print "<pre>" . $sql . "<hr>"; if ($this->res_errMsg != '') { print "<span style='color: red'>" . $this->res_errMsg . "</span><hr>"; } print "</pre>"; } // restore errors ini_set('display_errors', $ini_err); return $res; }
public function execute($sql) { global $sys_dbPrefix; global $ses_userid; // hide errors $ini_err = ini_get('display_errors'); ini_set('display_errors', 0); $res = false; // --- process sql if ($this->dbType == 'postgres') { $this->res_data = pg_query($this->dbConn, $sql); if (!$this->res_data) { $this->res_errMsg = pg_last_error($this->dbConn); } else { $this->res_errMsg = pg_result_error($this->res_data); $this->res_affectedRows = pg_affected_rows($this->res_data); $this->res_rowCount = pg_num_rows($this->res_data); $this->res_fieldCount = pg_num_fields($this->res_data); $res = new phpRecordSet($this->dbType, $this->res_data, $this->res_rowCount, $this->res_fieldCount); // -- parse field names for ($i = 0; $i < $this->res_fieldCount; $i++) { $this->res_fields[$i] = pg_field_name($this->res_data, $i); $this->res_fieldsInfo[$i] = array(); $this->res_fieldsInfo[$i]['type'] = pg_field_type($this->res_data, $i); $this->res_fieldsInfo[$i]['len'] = pg_field_size($this->res_data, $i); $this->res_fieldsInfo[$i]['is_null'] = pg_field_is_null($this->res_data, $i); $this->res_fieldsInfo[$i]['prt_len'] = pg_field_prtlen($this->res_data, $i); } } // log error if ($this->res_errMsg != '') { $userid = $ses_userid != null ? $ses_userid : 'null'; $ssql = "INSERT INTO " . $sys_dbPrefix . "log_error(domain, url, userid, sql, error)\n\t\t\t\t\t\t VALUES('" . $_SERVER["HTTP_HOST"] . "', '" . pg_escape_string($_SERVER["REQUEST_URI"]) . "', {$userid}, \n\t\t\t\t\t\t\t'" . pg_escape_string($sql) . "', '" . pg_escape_string($this->res_errMsg) . "');"; pg_query($this->dbConn, $ssql); } } // --- mysql if ($this->dbType == 'mysql') { $this->res_data = mysql_query($sql, $this->dbConn); if (!$this->res_data) { $this->res_errMsg = mysql_error($this->dbConn); } else { $this->res_errMsg = mysql_error($this->res_data); $this->res_affectedRows = mysql_affected_rows($this->res_data); $this->res_rowCount = mysql_num_rows($this->res_data); $this->res_fieldCount = mysql_num_fields($this->res_data); $res = new phpRecordSet($this->dbType, $this->res_data, $this->res_rowCount, $this->res_fieldCount); // -- parse field names for ($i = 0; $i < $this->res_fieldCount; $i++) { $this->res_fields[$i] = mysql_field_name($this->res_data, $i); $this->res_fieldsInfo[$i] = array(); $this->res_fieldsInfo[$i]['type'] = mysql_field_type($this->res_data, $i); $this->res_fieldsInfo[$i]['len'] = mysql_field_len($this->res_data, $i); $this->res_fieldsInfo[$i]['flags'] = mysql_field_flags($this->res_data, $i); } } // log error if ($this->res_errMsg != '') { $userid = $ses_userid != null ? $ses_userid : 'null'; $ssql = "INSERT INTO " . $sys_dbPrefix . "log_error(domain, url, userid, sql, error)\n\t\t\t\t\t\t VALUES('" . $_SERVER["HTTP_HOST"] . "', '" . mysql_escape_string($_SERVER["REQUEST_URI"]) . "', {$userid}, \n\t\t\t\t\t\t\t'" . mysql_escape_string($sql) . "', '" . mysql_escape_string($this->res_errMsg) . "');"; mysql_query($this->dbConn, $ssql); } } // show debug info if on if ($this->debug == true) { print "<pre>" . $sql . "<hr>"; if ($this->res_errMsg != '') { print "<span style='color: red'>" . $this->res_errMsg . "</span><hr>"; } print "</pre>"; } // restore errors ini_set('display_errors', $ini_err); return $res; }
function queryFactoryMeta($zp_field, $zp_res) { $this->type = @pg_field_type($zp_res, $zp_field); $this->max_length = @pg_field_len($zp_res, $zp_field); }
/** * Gets information about a table of the DataBase * * @param string $tableName The table to get the info * @access public * @return Array with the information about the table */ public function getTableFields($tableName) { $fields = array(); // Obtain the Primary_Key of the table $pkQuery = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='{$tableName}' AND constraint_name='{$tableName}_id_key' ; "; $pkRes = pg_query(self::$_link, $pkQuery); $pkArr = pg_fetch_array($pkRes); // Obtain the number of fields in the table $query = "SELECT * FROM {$tableName} LIMIT 1"; $res = pg_query(self::$_link, $query); $fcount = pg_num_fields($res); if ($data !== false) { for ($i = 0; $i < $fcount; $i++) { $name = pg_field_name($res, $i); $type = pg_field_type($res, $i); //$flags = $data[pg_field_name($res,$i)]; $flags = array(); if (in_array($name, $pkArr)) { $flags[] = "primary_key"; } $field = array("name" => $name, "value" => null, "type" => $type, "flags" => $flags); $fields[] = $field; } return $fields; } else { return null; } }
/** * Fetches the next row from the current result set * * @return unknown */ function fetchResult() { if ($row = pg_fetch_row($this->results)) { $resultRow = array(); foreach ($row as $index => $field) { list($table, $column) = $this->map[$index]; $type = pg_field_type($this->results, $index); switch ($type) { case 'bool': $resultRow[$table][$column] = $this->boolean($row[$index], false); break; case 'binary': case 'bytea': $resultRow[$table][$column] = pg_unescape_bytea($row[$index]); break; default: $resultRow[$table][$column] = $row[$index]; break; } } return $resultRow; } else { return false; } }