php pg_fetch_array,pg_fetch_array

用户评论:

[#1]

strata_ranger at hotmail dot com [2009-08-23 13:53:26]

Note that when using PGSQL_BOTH, numerically and associatively indexed fields are separate variables and treated as such:

$res=pg_query("Select 'foo' as bar");$data=pg_fetch_array($res,0,PGSQL_BOTH);var_dump($data);// Array(2)

// {

//   [0] => string(3) "foo"

//   ["bar"] => string(3) "foo"

// }

// This won't affect $data['bar']$data[0] ='bar';var_dump($data);// Array(2)

// {

//   [0] => string(3) "bar"

//   ["bar"] => string(3) "foo"

// }?>

If you want to have reference binding between your numeric and associative indexes, you'll have to establish that yourself:

$result=pg_query("Select 'foo' as bar");$data=pg_fetch_row($result);// Establish references between column name/number$from=$data;

foreach($fromas$cx=>$value)

{$key=pg_field_name($result,$cx);

if (is_string($key))$data[$key] =&$data[$cx];

}var_dump($data);// Array(2)

// {

//   [0] => &string(3) "foo"

//   ["bar"] => &string(3) "foo"

// }

// Note the reference binding between $data[0] and $data['bar']$data[0] ='baz';var_dump($data);// Array(2)

// {

//   [0] => &string(3) "baz"

//   ["bar"] => &string(3) "baz"

// }?>

[#2]

anonymous [2005-05-13 14:21:07]

Hopefully most people realize this on their own, but the examples below where people tried to get creative with getting numerical or associative (not both) keys in the result are rather pointless. See the pg_fetch_assoc() and pg_fetch_row() for the built in functions that do this automatically. It's generally a better idea to use one of these other functions unless you *need* to access fields by both collumn name *and* index.

[#3]

Dave O [2005-02-22 09:52:22]

I found this out through help from the mailing lists.  If you need to reset the internal counter, use the pg_result_seek, similar to:

pg_result_seek($result, 0)

...plagiarized from the comment on the function's doc page.

[#4]

devnull [2005-02-02 13:59:43]

In response to eth0's comment below about SELECT'ing from two tables where the tables have columns with the same names, you can get around this problem like this:

"SELECT table1.foo AS foo1, table2.foo AS foo2 FROM table1, table2"

In the associative array returned, the keys will be "foo1" and "foo2".

[#5]

enyo at www.red-link.com [2003-09-14 18:55:44]

Just because it is not really clear how to specify the result type, I poste this message.

I wrote a wrapper function which looks like this:

{$return= @pg_fetch_array($result,$row,$result_type);

return$return;

}?>

I think this way it is quite comfortable to get the arrays you want.

[#6]

akm at e-nterart dot pl [2003-06-17 09:45:47]

(Timesaver) Be aware of the fact that keys in array returned by this function are (well, at least as of 4.2.3) of the same case as SQL column names (e.g. if your column name is ID then key name is also ID, not id or Id), and the keys in associative array are CASE SENSITIVE!!! So don't be surprised if you get unexpected results. Double check SQL column names and the key names.

[#7]

jesse at sokieserv dot dhs dot org [2001-12-13 04:38:01]

As of PHP 4.1.0, you can now use code such as the following to iterate through a result set:

$conn = pg_connect("host=localhost dbname=whatever");

$result = pg_exec($conn, "select * from table");

while ($row = pg_fetch_array($result))

{

echo "data: ".$row["data"];

}

Can be a nice little time saver, PHP with MySQL has supported this for a while but I'm glad to see it extended to PostgreSQL...

[#8]

eth0 at fins [2001-09-28 11:15:57]

Please remember that if you have for example a table Customers with "cust_ID", "name" and "address" and another table Users with "u_ID","name" and "other" and then you SELECT WHERE cust_ID=u_ID then you'll get in the result array ONLY ONE "name" field, precisely the last one resulted from the select!!!

[#9]

elliot at nospam dot rightnowtech dot com [2001-07-22 21:50:49]

Just remember when you 'or die' to close your table(s) or you may get a confused look from non-internet explorer users.

[#10]

mkb at ele dot uri dot edu [2001-03-27 16:52:36]

The column names if you use PGSQL_ASSOC or PGSQL_BOTH are always in lowercase, no matter what the name is in the database or in the query.

[#11]

gherson at snet dot net [2001-03-06 10:30:58]

In addition to returning "false if there are no more rows", pg_fetch_array will also trigger an E_WARNING.  You can temporarily turn that error reporting level off and suck out all your data like so:

$errRptLvl=error_reporting();error_reporting($errRptLvl& ~(E_WARNING));

list($i,$j)=array(0,0);

while ($selection[$i++] =$this->fetchArray($j++));// (fetchArray is a pg_fetch_array wrapper.)error_reporting($errRptLvl);// Restore error reporting level.unset($selection[$i-1]);// Delete the last, empty row.return$selection;?>

[#12]

gherson at snet dot net [2001-01-02 21:14:22]

PGSQL_BOTH is the default, meaning your array size will be doubled.

If you specify this field (result type), include no quotes around it or you won't get any data, not even an error.

Here's my wrapper function:

function SQL_fetch_array($result_ndx, $row, $result_type=PGSQL_ASSOC) {

return pg_fetch_array($result_ndx, $row, $result_type);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值