php pg_exec,PHP 执行查询

用户评论:

a dot mcruer at live dot com (2013-05-17 18:46:46)

A quick note for novice users: when gathering input from fields on a web form that maintains a database connection, *never* use pg_query to do queries from the field. Always sanitize input using pg_prepare and pg_execute.

MomAlwaysLikedMeBest at hotmail (2011-04-26 11:42:21)

Transactions can be accomplished using the following syntax:

pg_query($db,"BEGIN WORK");// Some insert/update/delete queries.$res=pg_query($db,"INSERT INTO customers (customer_id) VALUES ('$customer_id')");// Verify and end the transaction as appropriate.if (!$res) {pg_query($db,"ROLLBACK");

} else {pg_query($db,"COMMIT");

}?>

sd at dicksonlife dot com (2006-09-14 12:40:09)

Took me a while to track this down so I thought it might be useful for others:

If you use stored procedures and need to get result sets back from them:

function dbquery($link,$query){

pg_query($link,"BEGIN;");

$tr=pg_query($link,$query);

$r=pg_fetch_row($tr);

$name=$r[0];

$rs=pg_query($link,"FETCH ALL IN \"" . $name . "\";");

pg_query($link,"END;");

return $rs;

}

(Error checking removed for clarity)

zoli at makettinfo.hu (2006-05-21 08:07:22)

It would be better this way:

$result=pg_query($conn,"SELECT COUNT(*) AS rows FROM x WHERE a=b;");

if  (!$result) {

echo"query did not execute";

}

if ($line=pg_fetch_assoc($result)) {

if ($line['rows'] ==0) {

echo"0 records"}

}

else {

while ($row=pg_fetch_array($result)) {//do stuff with $row}

}?>

This solution doesn't raise the load of the system with the move of matching rows (perhaps 0,1, perhaps 100, 1000, ... rows)

mankyd (2006-05-13 07:21:41)

There was a typo in the code that I posted:

$result=pg_query($conn,"SELECT * FROM x WHERE a=b;");

if  (!$result) {

echo"query did not execute";

}

if (pg_num_rows($result) ==0) {

echo"0 records"}

else {

while ($row=pg_fetch_array($result)) {//do stuff with $row}

}?>

mankyd (2006-05-12 18:56:14)

Improving upon what jsuzuki said:

It's probably better to use pg_num_rows() to see if no rows were returned, as that leaves the resultset cursor pointed to the first row so you can use it in a loop.

Example:

$result=pg_query($conn,"SELECT * FROM x WHERE a=b;");

if  (!$result) {

echo"query did not execute";

}

if (pg_num_rows($result) ==0) {

echo"0 records"}

else {

while ($row=pg_fetch_array($result) {//do stuff with $row}

}?>

I, personally, also find it more readable.

jsuzuki at spamcop dot net (2005-11-27 05:58:02)

expanding on the note left by "cmoore" -

To check to see if the recordset returned no records,

$result=pg_query($conn,"SELECT * FROM x WHERE a=b;");

if  (!$result) {

echo"query did not execute";

}$rs=pg_fetch_assoc($result);

if (!$rs) {

echo"0 records"}?>

-jack

Jan-Willem Regeer (2005-09-27 16:58:36)

In reply to david dot bouriaud at ac-rouen dot fr:

All it is doing is internal caching. How can that be dangerous. If you are going to be deleting records after you have closed the connection it is your problem to make sure you have the latest and greatest records, and not some cached ones. Considering you are writing the script I don't see why it is a problem, you know what you are doing in the script, so it is quite useless for PHP to invalidate the cache, when that could be done upon exiting the script, which would mean there was less time spent cleaning out the cache when it counts most (when returning data to the user).

cmoore (2005-09-05 11:20:52)

One thing to note that wasn't obvious to me at first. If your query returns zero rows, that is not a "failed" query. So the following is wrong:

$result=pg_query($conn, "SELECT * FROM x WHERE a=b;");

if (!$result) {

echo "No a=b in x\n";

}

pg_query returns FALSE if the query can not be executed for some reason. If the query is executed but returns zero rows then you get back a resul with no rows.

Akbar (2004-12-01 08:07:46)

Use pg_query to call your stored procedures, and use pg_fetch_result when getting a value (like a smallint as in this example) returned by your stored procedure.

$pgConnection=pg_connect("dbname=users user=me");$userNameToCheckFor="metal";$result=pg_query($pgConnection,"SELECT howManyUsersHaveThisName('$userNameToCheckFor')");$count=pg_fetch_result($result,0,'howManyUsersHaveThisName');?>

yoshinariatsuo at yahoo dot com (2003-04-22 23:59:04)

create table from pg_query results.. hope it helps newbies...

function table_create($result)

{

$numrows = pg_num_rows($result);

$fnum = pg_num_fields($result);

echo "

echo "

";

for ($x = 0; $x < $fnum; $x++) {

echo "

";

echo strtoupper(pg_field_name($result, $x));

echo "

";

}

echo "

";

for ($i = 0; $i < $numrows; $i++) {

$row = pg_fetch_object($result, $i);

echo "

";

for ($x = 0; $x < $fnum; $x++) {

$fieldname = pg_field_name($result, $x);

echo "

";

echo $row->$fieldname;

echo "

";

}

echo"

";

}

echo "

";

return 0;

}

mentat at azsoft dot pl (2002-10-14 05:16:45)

$GLOBALS["PG_CONNECT"]=pg_connect(...);

....

function query ($sqlQuery,$var=0) {

if (!$GLOBALS["PG_CONNECT"]) return 0;

$lev=error_reporting (8); //NO WARRING!!

$result=pg_query ($sqlQuery);

error_reporting ($lev); //DEFAULT!!

if (strlen ($r=pg_last_error ($GLOBALS["PG_CONNECT"]))) {

if ($var) {

echo "

ERROR:

";

echo $sqlQuery;

echo "

";

echo $r;

echo "&lt/p>";

}

close_db ();

return 0;

}

return $result;

}

hierophantNOSPAM at pcisys dot net (2002-06-17 12:50:20)

Regarding david.bouriaud@ac-rouen.fr:

You misunderstand SQL. When a query is issued, results applicable at the time of the query are returned to the application (i.e. PHP). There is no further reference to the database required. Thus, all of the pg_fetch_* functions are acting on an internal data storage, NOT the database itself. This is because SQL does not have a concept of sets, or of state (except in limited circumstances provided by transactions). However, if you use a cursor instead, fetching only one record at a time, you may get an error if you delete the table. If you don't, it is an issue with Postgres, not PHP.

jvarner at dsrglobal dot com (2002-05-10 15:18:28)

That's why your code should never assume it has the very latest data unless it locks it.

david dot bouriaud at ac-rouen dot fr (2002-05-07 10:54:41)

Hi to all !

It seems that the old pg_exec function does not do what it is expected to.

In the doc, it is said that it returns a resource identifier on the successful querry that was send to the backend.

It seems to me that it is more than a resource identifier.

Follow the example :

$ConnId=pg_connect("blablabla");$ResId=pg_exec("select * from table",$ConnId);pg_close($ConnId);$row=pg_fetch_array($ResId,4);?>

I closed the connection voluntarily before the pg_fetch_array. It WORKS !

Now, imagine the following script :

$ConnId=pg_connect("blablabla");$ResId=pg_exec("select * from table",$ConnId);pg_close($ConnId);system("psql base -c delete from table");$row=pg_fetch_array($ResId,4);?>See how it could be harmful !!!! I think that the coders have done this for performances reasons, but it is not the right way do do so !!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值