0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

php+postgresqlでinsert後のレコードのidを取得する

Last updated at Posted at 2016-09-02

テーブルにレコード追加後、紐づくレコードを関連テーブルに追加したい時

<?php
//DB接続
$conn = pg_pconnect(****);

//sql文作成
$sql = "INSERT INTO users (name) VALUES('hoge') RETURNING id";

//sql発行
$result = pg_query($conn, $sql);

//結果配列を取得
$row = pg_fetch_row($result);

//$idに追加したレコードのidをセット
$id = $row[0];
?>
0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?