2017-11-03 1 views
0

내가 코드PDO의 PostgreSQL은 지난

$dbh = new PDO("pgsql:host=localhost; port=5432; dbname=gpd; user=postgres; password=postgres"); 
$sth = $dbh->prepare("INSERT INTO res (res_name, res_order) VALUES ('London', 1)"); 
$sth->execute(); 
$id = $dbh->lastInsertId(); 
echo $id; 

기록 삽입이 ID를 리턴 '거짓'-1 삽입하지만, $ ID가 비어 있습니다.

내가 execure 쿼리를 INSERT INTO res (res_name, res_order) VALUES ('London', 1) RETURNING id

을 시도하고 값을 "-1"

+1

당신이 할 수 있어요 'res'의 테이블 구조를 공유 하시겠습니까? – Scoots

+0

표 고해상도 ( ID SERIAL PRIMARY KEY, RES_NAME VARCHAR (100) DEFAULT ''NOT NULL, res_order의 INT의 DEFAULT 1 NULL NOT )를 작성 –

답변

0

집에 갈을받은 나 자신은 어떻게해야하는지 이해

$dbh = new PDO("pgsql:host=localhost; port=5432; dbname=gpd; user=postgres; password=postgres");  
$sth = $dbh->prepare("INSERT INTO res (res_name, res_order) VALUES ('London', 1)");  
$sth->execute();  
$result = $sth->fetch(PDO::FETCH_ASSOC);  
echo "ID - ". $result["id"]; 

그것은 간단