使用Postgresql发送多个查询的提示 [英] Tips on sending multiple query with postgresql
问题描述
我有用于pgsql数据库的这段代码,我能够发送第一个查询,但是我需要一次将两个查询发送到不同的表中。
I have this code for pgsql database i am able to send the first query but i need to send both query at once they are different tables.
<?php
include_once('db/db.php');
$description = pg_escape_string($_POST['description']);
$startdate = pg_escape_string($_POST['startdate']);
$schedulestarttime = pg_escape_string($_POST['schedulestarttime']);
$scheduleendtime = pg_escape_string($_POST['scheduleendtime']);
//those 4 required for second query
$cycleid = ..; //this must be returning the cycleid of first query
$eventtypecode = 5;
$duration = 120;
$position = 6;
$query = "INSERT INTO cycles (description, startdate, schedulestarttime, scheduleendtime) VALUES('" . $description . "', '" . $startdate . "', '" . $schedulestarttime . "', '" . $scheduleendtime . "') RETURNING cycleid";
$result = pg_query($query);
if (!$result) {
$errormessage = pg_last_error();
echo "Error with query: " . $errormessage;
exit();
}
else {
$query = "INSERT INTO cycleelements (cycleid, eventtypecode, duration, position) VALUES ($cycleid, '" . $eventtypecode . "', '" . $duration . "', '" . $position . "')";
$result = pg_query($query);
if (!$result) {
$errormessage = pg_last_error();
echo "Error with query: " . $errormessage;
exit();
}
printf ("Successfully added | %s | %s | %s | %s | to the database", $cycleid, $eventtypecode, $duration, $position);
pg_close();
}
printf ("Successfully added | %s | %s | %s | %s | to the database", $description, $startdate, $schedulestarttime, $scheduleendtime);
pg_close();
?>
如果有人有给我的提示是我需要在代码中进行哪些更改,以便可以同时发送查询和在那儿,我还评论说,对于第二个查询,必须从第一个查询返回循环ID。
If someone has any tips for me what i need to change in the code so i can send both query and also there i commented that for the second query the cycleid must be returned from the first query.
谢谢,
问候
推荐答案
您可以在一个pg_query中使用多个语句。这些语句在单个事务中启动,因此如果出现问题,您可以回滚。
You can use multiple statements in a single pg_query. These statements are launched in a single transaction, so you can rollback if something went wrong.
请参见 http://www.php.net/manual/en/function.pg-query.php
这篇关于使用Postgresql发送多个查询的提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!