简体   繁体   English

使用for php语句插入postgres db

[英]insert into postgres db with for php statement

I'm writing a code with this function: 我正在用这个函数编写代码:

function foo() {

    $input = array('bar');

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

        $db=connection_pgsql() or die('Connessione al DBMS non riuscita');

            pg_prepare( "run","INSERT INTO tab1(name) VALUES  ($1)");
            pg_execute("run",$input);

            pg_prepare( "run","INSERT INTO tab2(name) VALUES  ($1)");
            pg_execute("run",$input);

            pg_prepare( "run","INSERT INTO tab3(name) VALUES  ($1)");
            pg_execute("run",$input);

        pg_close($db);


    }
}   

After the execution of this function there are only 执行此功能后,只有

20 rows in tab1, 20 rows in tab2 and 20 rows in tab3 20行中TAB1,在TAB2 20行和TAB3 20

and not 并不是

1000 rows in tab1, 1000 rows in tab2 and 1000 rows in tab3. 1000TAB1,1000行TAB2和1000行TAB3。

Is it a problem of php or postgresql configuration? 这是php或postgresql配置的问题吗?

You're not checking for errors. 你没有检查错误。

Check the return value of functions and use the error info functions provided by PHP. 检查函数的返回值并使用PHP提供的错误信息函数。

See: 看到:

You can also check the PostgreSQL error logs. 您还可以检查PostgreSQL错误日志。

It's much clearer if you use PDO; 如果你使用PDO会更清楚; see eg How is a Postgres "RAISE EXCEPTION" converted into a PDOException? 例如, 如何将Postgres“RAISE EXCEPTION”转换为PDOException? .

Also, don't open and close connections each loop iteration like this, the performance will be awful . 另外,不要像这样打开和关闭每个循环迭代的连接,性能会很糟糕 Preferably do all the work in a single transaction on a single connection. 最好在单个连接上进行单个事务中的所有工作。

check the resource returned or at least if it is not false . 检查返回的资源或至少是否为false Look at docs example, you should not just pg_execute("run",$input); 看看docs的例子,你不应该只是pg_execute("run",$input); , but 但是

$result = pg_execute("run",$input);

so you can run 所以你可以跑

var_dump($result);

This way you will see full dump of value returned on your html page. 这样,您将在html页面上看到返回的完整转储值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM