开发者

PDOStatement::bindParam() not setting AI value from MySQL insert?

I have a simple insert statement using PDO php class.

the 'id' column is identity (doy) with autoincrement.

$statement = $db->prepare('INSERT INTO demographics (id,location_id,male,ethnicity_id,birthyear) VALUES (:id,:location_id,:male,:ethnicity_id,:birthyear)');

$statement->bindParam(':id',$demo->id,PDO::PARAM_INT,4);
$statement->bindParam(':location_id', $demo->locationid,PDO::PARAM_INT);
$statement->bindParam(':male',$demo->male,PDO::PARAM_BOOL);
$statement->bindParam(':ethnicity_id',$demo->ethnicityid,PDO::PARAM_INT);
$statement->bindParam(':birthyear',$demo->birthyear,PDO::PARAM_INT);
$statement->execute();  

print_r($demo);

Even though the statement executes correctly (row is correctly written), $demo->id is null.

I have a very similar statement for a different table, and it correctly returns the i开发者_如何学Pythond column.

Any thoughts?


If id is autoincremented, you don't need to specify it when inserting into the table. What value are you giving to $demo->id?

If you need the id of the inserted entry, you could retrieve it using PDO::lastInsertId, then set the object's $id field with the value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜