Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get insert id properly? #38

Closed
shtse8 opened this issue Aug 20, 2017 · 1 comment
Closed

How to get insert id properly? #38

shtse8 opened this issue Aug 20, 2017 · 1 comment
Labels

Comments

@shtse8
Copy link
Contributor

shtse8 commented Aug 20, 2017

I am trying to get the last insert id. But I am using it in pool mode, will it affect the result by other inserts?

$resultSet = yield $pool->prepare("INSERT INTO `table` (`data`) VALUE (:data)", ['data' => $data]);
$insertId= $resultSet->getConnInfo()->insertId;

Is it the correct code to get the insertId?

@kelunik
Copy link
Member

kelunik commented Aug 20, 2017

You don't get a ResultSet for insert queries. You can use the following snippet, it resolves to a connection info object.

$info = yield $conn->prepare(
    "INSERT INTO bugs (title, data, submitter) " . 
    "VALUES (:title, :data, :submitter)",
    ["title" => $title, "data" => $data, "submitter" => $uid]
);
$id = $info->insertId;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants