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; }
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; }
/** * 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; }
/** * 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; }
/** * 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; }
} for ($i = 0; $i < $rows; $i++) { pg_fetch_array($result, $i, PGSQL_NUM); } for ($i = 0; $i < $rows; $i++) { pg_fetch_object($result); } for ($i = 0; $i < $rows; $i++) { pg_fetch_row($result, $i); } for ($i = 0; $i < $rows; $i++) { pg_fetch_result($result, $i, 0); } pg_result_error($result); pg_num_rows(pg_execute($db, "php_test", array(100))); pg_num_fields(pg_execute($db, "php_test", array(100))); pg_field_name($result, 0); pg_field_num($result, $field_name); pg_field_size($result, 0); pg_field_type($result, 0); pg_field_prtlen($result, 0); pg_field_is_null($result, 0); $result = pg_prepare($db, "php_test2", "INSERT INTO " . $table_name . " VALUES (\$1, \$2);"); pg_result_error($result); pg_free_result($result); $result = pg_execute($db, "php_test2", array(9999, "A'BC")); pg_last_oid($result); pg_free_result($result); } pg_close($db); echo "OK";
function query($query) { //去掉查询语句的前后空格 $query = trim($query); //初始化返回值为0 $return_val = 0; //清空缓存.. $this->flush(); //记录此函数如何被调用,用于调试... $this->func_call = "\$db->query(\"{$query}\")"; //跟踪最后查询语句,用于调试.. $this->last_query = $query; //通过pg_query函数执行查询操作.. if (!($this->result = @pg_query($this->dbh, $query))) { $this->print_error(); return false; } //记录查询次数,用于调试... $this->num_queries++; //执行insert, delete, update, replace操作 if (preg_match("/^(insert|delete|update|replace)\\s+/i", $query)) { //获取操作所影响的记录行数 $this->rows_affected = pg_affected_rows($this->result); $return_val = $this->rows_affected; //获取最后插入记录id if (preg_match("/^(insert)\\s+/i", $query)) { $this->insert_id = $this->get_insert_id($query); //$return_val = $this->insert_id; } } else { //获取字段信息 $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++; } //获取查询结果 $i = 0; while ($row = @pg_fetch_object($this->result, $i)) { //php5中不支持第三个参数PGSQL_ASSOC?可能已经被删除!但手册上还没有说明。 //取得包含数组的结果对象 $this->last_result[$i] = $row; $i++; } @pg_free_result($this->result); //获取查询结果行数 $this->num_rows = $i; $return_val = $this->num_rows; } //是否显示所有的查询信息 $this->debug_all ? $this->debug() : null; return $return_val; }
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; }
function query($query) { // For reg expressions $query = trim($query); // Flush cached values.. $this->flush(); // Log how the function was called $this->func_call = "\$db->query(\"{$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++; // If there was an insert, delete or update see how many rows were affected // (Also, If there there was an insert take note of the last OID $query_type = array("insert", "delete", "update", "replace"); // loop through the above array foreach ($query_type as $word) { // This is true if the query starts with insert, delete or update if (preg_match("/^{$word}\\s+/i", $query)) { $this->rows_affected = pg_affected_rows($this->result); // This gets the insert ID if ($word == "insert") { $this->insert_id = $this->get_insert_id($query); // If insert id then return it - true evaluation return $this->insert_id; } // Set to false if there was no insert id $this->result = false; } } // In other words if this was a select statement.. if ($this->result) { // ======================================================= // 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++; } // ======================================================= // Store Query Results $i = 0; while ($i < @pg_num_rows($this->result)) { $row = @pg_fetch_object($this->result, $i); // Store relults as an objects within main array $this->last_result[$i] = $row; $i++; } // Log number of rows the query returned $this->num_rows = $i; @pg_free_result($this->result); // If debug ALL queries $this->debug_all ? $this->debug() : null; // If there were results then return true for $db->query if ($i) { return true; } else { return false; } } else { // If debug ALL queries $this->debug_all ? $this->debug() : null; // Update insert etc. was good.. return true; } }
function field_size($result, $fila) { if ($size = pg_field_size($result, $fila)) { return $size; } else { return false; } }
function fieldSize($rs, $off_set_field) { if ($this->bd->sgdb == 'MySQL') { return mysql_field_len($rs, $off_set_field); } else { return pg_field_size($rs, $off_set_field); } }
/** * @param int|string $fieldNameOrNum Field name or index. * * @return int Storage required for field. -1 indicates a variable length field. * * @throws \Icicle\Exception\InvalidArgumentError If the field number does not exist in the result. */ public function fieldSize($fieldNameOrNum) : int { return \pg_field_size($this->handle, $this->filterNameOrNum($fieldNameOrNum)); }
dump( pg_last_oid($result) ); pg_free_result($result); } elseif( pg_result_status($result) == PGSQL_EMPTY_QUERY ) { dump( 0 ); dump( 0 ); pg_free_result($result); } elseif( pg_result_status($result) == PGSQL_TUPLES_OK ) { $width = pg_num_fields($result); $height = pg_num_rows($result); dump($width); dump($height); for( $i = 0; $i < $width; ++$i ) { $type = pg_field_type( $result, $i ); dump( pg_field_name( $result, $i ) ); dump( $type ); dump( pg_field_size( $result, $i ) ); } for( $i = 0; $i < $height; ++$i ) { $row = pg_fetch_row( $result ); for( $j = 0; $j < $width; ++$j ) if( is_null($row[$j]) ) dump( '' ); else dump( ' '.$row[$j] ); } pg_free_result( $result ); } else { $e = pg_result_error($result); pg_free_result($result); err( $e ); }
/** * Get column information * @param int * @return array */ protected function fetch_field($intOffset) { $arrData['name'] = @pg_field_name($this->resResult, $intOffset); $arrData['max_length'] = @pg_field_size($this->resResult, $intOffset); $arrData['not_null'] = @pg_field_is_null($this->resResult, $intOffset); $arrData['type'] = @pg_field_type($this->resResult, $intOffset); return $arrData; }
function fetch_field(&$result, $i) { trigger_before('fetch_field', $this, $this); $field = new dbfield(); $field->name = pg_field_name($result, $i); $field->type = pg_field_type($result, $i); $field->size = pg_field_size($result, $i); return $field; }
public function FieldSize($fieldNo) { return pg_field_size($this->ds, $fieldNo); }
/** * Get the column information * @param integer * @return object */ protected function fetch_field($intOffset) { $objData = new stdClass(); $objData->name = @pg_field_name($this->resResult, $intOffset); $objData->max_length = @pg_field_size($this->resResult, $intOffset); $objData->not_null = @pg_field_is_null($this->resResult, $intOffset); $objData->type = @pg_field_type($this->resResult, $intOffset); return $objData; }
/** * Returns metadata for all columns in a result set. * * @return array */ public function getColumnsMeta() { $hasTable = version_compare(PHP_VERSION, '5.2.0', '>='); $count = pg_num_fields($this->resultSet); $meta = array(); for ($i = 0; $i < $count; $i++) { // items 'name' and 'table' are required $meta[] = array('name' => pg_field_name($this->resultSet, $i), 'table' => $hasTable ? pg_field_table($this->resultSet, $i) : NULL, 'type' => pg_field_type($this->resultSet, $i), 'size' => pg_field_size($this->resultSet, $i), 'prtlen' => pg_field_prtlen($this->resultSet, $i)); } return $meta; }
function field_info($query, $count) { if ($this->debug) { echo "<pre style=\"color : green\">Getting column information {$this->dbpath} <p style=\"color:purple;\"> {$query} </p></pre>"; } $nooffields = 0; //Validate the sql statement and make adjustments switch ($this->dbtype) { /* Firebird Functionality */ case "firebird": //write some things here $col_info = ibase_field_info($query, $count); break; /* SQLite Functionality */ /* SQLite Functionality */ case "sqlite": putenv("TMP=" . $this->tmppath); $name = sqlite_field_name($query, $count); //echo $name; $col_info["alias"] = $name; $col_info["name"] = $name; break; /* Oracle Functionality */ /* Oracle Functionality */ case "oracle": $column_name = oci_field_name($query, $count); $column_type = oci_field_type($query, $count); $column_size = oci_field_size($query, $count); $column_prec = oci_field_precision($query, $count); $column_scale = oci_field_scale($query, $count); $col_info["name"] = $column_name; $col_info["alias"] = $column_name; $col_info["length"] = $column_size; $col_info["prec"] = $column_prec; $col_info["type"] = $column_type; $col_info["scale"] = $column_scale; break; /* PGSQL Functionality */ /* PGSQL Functionality */ case "pgsql": $col_info["name"] = pg_field_name($query, $count); $col_info["alias"] = NULL; // always set to NULL $col_info["relation"] = NULL; // always set to NULL $col_info["length"] = pg_field_size($query, $count); $col_info["type"] = pg_field_type($query, $count); break; } if ($this->debug) { echo "<pre style=\"color : blue\">Column Info fetched for Column {$count} \n </pre>"; } return $col_info; }
/** * Returns information about a table or a result set * * NOTE: only supports 'table' and 'flags' if <var>$result</var> * is a table name. * * @param object|string $result MDB2_result object from a query or a * string containing the name of a table. * While this also accepts a query result * resource identifier, this behavior is * deprecated. * @param int $mode a valid tableInfo mode * * @return array an associative array with the information requested. * A MDB2_Error object on failure. * * @see MDB2_Driver_Common::tableInfo() */ function tableInfo($result, $mode = null) { if (is_string($result)) { return parent::tableInfo($result, $mode); } $db =& $this->getDBInstance(); if (PEAR::isError($db)) { return $db; } $resource = MDB2::isResultCommon($result) ? $result->getResource() : $result; if (!is_resource($resource)) { return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, 'Could not generate result resource', __FUNCTION__); } if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { if ($db->options['field_case'] == CASE_LOWER) { $case_func = 'strtolower'; } else { $case_func = 'strtoupper'; } } else { $case_func = 'strval'; } $count = @pg_num_fields($resource); $res = array(); if ($mode) { $res['num_fields'] = $count; } $db->loadModule('Datatype', null, true); for ($i = 0; $i < $count; $i++) { $res[$i] = array('table' => function_exists('pg_field_table') ? @pg_field_table($resource, $i) : '', 'name' => $case_func(@pg_field_name($resource, $i)), 'type' => @pg_field_type($resource, $i), 'length' => @pg_field_size($resource, $i), 'flags' => ''); $mdb2type_info = $db->datatype->mapNativeDatatype($res[$i]); if (PEAR::isError($mdb2type_info)) { return $mdb2type_info; } $res[$i]['mdb2type'] = $mdb2type_info[0][0]; if ($mode & MDB2_TABLEINFO_ORDER) { $res['order'][$res[$i]['name']] = $i; } if ($mode & MDB2_TABLEINFO_ORDERTABLE) { $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; } } return $res; }
function pg_fetch_field($result, $file_number) { $pg_to_php = array('bit' => 'bit', 'boolean' => 'bool', 'box' => 'box', 'character' => 'bpchar', 'char' => 'bpchar', 'bytea' => 'bytea', 'cidr' => 'cidr', 'circle' => 'circle', 'date' => 'date', 'daterange' => 'daterange', 'real' => 'float4', 'double precision' => 'float8', 'inet' => 'inet', 'smallint' => 'int2', 'smallserial' => 'int2', 'integer' => 'int4', 'serial' => 'int4', 'int4range' => 'int4range', 'bigint' => 'int8', 'bigserial' => 'int8', 'int8range' => 'int8range', 'interval' => 'interval', 'json' => 'json', 'lseg' => 'lseg', 'macaddr' => 'macaddr', 'money' => 'money', 'decimal' => 'numeric', 'numeric' => 'numeric', 'numrange' => 'numrange', 'path' => 'path', 'point' => 'point', 'polygon' => 'polygon', 'text' => 'text', 'time' => 'time', 'time without time zone' => 'time', 'timestamp' => 'timestamp', 'timestamp without time zone' => 'timestamp', 'timestamp with time zone' => 'timestamptz', 'time with time zone' => 'timetz', 'tsquery' => 'tsquery', 'tsrange' => 'tsrange', 'tstzrange' => 'tstzrange', 'tsvector' => 'tsvector', 'uuid' => 'uuid', 'bit varying' => 'varbit', 'character varying' => 'varchar', 'varchar' => 'varchar', 'xml' => 'xml'); $arr['name'] = pg_field_name($result, $field_number); $arr['table'] = pg_field_table($result, $field_number); $arr['max_length'] = pg_field_size($result, $field_number); $arr['not_null'] = @pg_field_is_null($result, $field_number, $arr['name']); $arr['primary_key'] = -1; $arr['unique_key '] = -1; $arr['multiple_key'] = -1; $arr['numeric'] = -1; $arr['blob'] = -1; $arr['type'] = $pg_to_php[pg_field_type($result, $file_number)]; $arr['unsigned'] = -1; $arr['zerofill'] = -1; return (object) $arr; }
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; }
public function columnData($col = '') { if (empty($this->query)) { return false; } $columns = []; $count = $this->numFields(); for ($i = 0; $i < $count; $i++) { $fieldName = pg_field_name($this->query, $i); $columns[$fieldName] = new \stdClass(); $columns[$fieldName]->name = $fieldName; $columns[$fieldName]->type = pg_field_type($this->query, $i); $columns[$fieldName]->maxLength = pg_field_size($this->query, $i); $columns[$fieldName]->primaryKey = NULL; $columns[$fieldName]->default = NULL; } if (isset($columns[$col])) { return $columns[$col]; } return $columns; }
/** * 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 query($query) { // Initialise return $return_val = 0; // Flush cached values.. $this->flush(); // For reg expressions $query = trim($query); // Log how the function was called $this->func_call = "\$db->query(\"{$query}\")"; // Keep track of the last query for debug.. $this->last_query = $query; // Count how many queries there have been $this->num_queries++; // Use core file cache function if ($cache = $this->get_cache($query)) { return $cache; } // If there is no existing database connection then try to connect if (!isset($this->dbh) || !$this->dbh) { $this->connect($this->dbuser, $this->dbpassword, $this->dbname); } // Perform the query via std postgresql_query function.. $this->result = @pg_query($this->dbh, $query); // If there is an error then take note of it.. if ($str = @pg_last_error($this->dbh)) { $is_insert = true; $this->register_error($str); $this->show_errors ? trigger_error($str, E_USER_WARNING) : null; return false; } // Query was an insert, delete, update, replace $is_insert = false; if (preg_match("/^(insert|delete|update|replace)\\s+/i", $query)) { $this->rows_affected = @pg_affected_rows($this->result); // Take note of the insert_id if (preg_match("/^(insert|replace)\\s+/i", $query)) { //$this->insert_id = @postgresql_insert_id($this->dbh); //$this->insert_id = pg_last_oid($this->result); // Thx. Rafael Bernal $insert_query = pg_query("SELECT lastval();"); $insert_row = pg_fetch_row($insert_query); $this->insert_id = $insert_row[0]; } // Return number fo rows affected $return_val = $this->rows_affected; } else { $num_rows = 0; //if ( $this->result ) //may be needed but my tests did not //{ // ======================================================= // 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++; } // ======================================================= // Store Query Results //while ( $row = @pg_fetch_object($this->result, $i, PGSQL_ASSOC) ) doesn't work? donno //while ( $row = @pg_fetch_object($this->result,$num_rows) ) does work while ($row = @pg_fetch_object($this->result)) { // Store results as an objects within main array $this->last_result[$num_rows] = $row; $num_rows++; } @pg_free_result($this->result); //} // Log number of rows the query returned $this->num_rows = $num_rows; // Return number of rows selected $return_val = $this->num_rows; } // disk caching of queries $this->store_cache($query, $is_insert); // If debug ALL queries $this->trace || $this->debug_all ? $this->debug() : null; return $return_val; }