jonny
jonny

Reputation: 7

Having trouble getting the last id in MySQL

I'm trying to get the last id added to the database using mysql_insert_id but it doesn't work. The error I get is:

mysql_insert_id() [function.mysql-insert-id]: Can't connect to local MySQL server through socket '/var/tmp/mysql.sock' (2) in /proj/co639/assessment2/nicp3/public_html/book_create.php on line 46

Warning: mysql_insert_id() [function.mysql-insert-id]: A link to the server could not be established in /proj/co639/assessment2/nicp3/public_html/book_create.php on line 46

I've done my connection like this:

$handle = new PDO( 'mysql:host=;dbname=', '', '');

What could be the issue?

Upvotes: 0

Views: 272

Answers (1)

Alen
Alen

Reputation: 1788

You are actually looking for this:

$handle->beginTransaction();
// here you execute your query
$handle->commit();
$id = $handle->lasInsertId();

Your code won't work because you mix two different classes.

More information can be found here: http://php.net/manual/en/pdo.lastinsertid.php

Upvotes: 1

Related Questions