Fatal error: Call to undefined method PDOStatement::lastInsertId()

Amit Patil

I have created a database class for PDO connection. Inserted record and tried to get last insert id after that getting this error "Fatal error: Call to undefined method PDOStatement::lastInsertId()".

Database class:

class Database {
public static $link = null ;

public static function getLink( ) {
    if ( self :: $link ) {
        return self :: $link ;
    }

    $ini = "config.ini" ;
    $parse = parse_ini_file ( $ini , true ) ;
    $driver = $parse [ "db_driver" ] ;
    $dsn = "${driver}:" ;
    $user = $parse [ "db_user" ] ;
    $password = $parse [ "db_password" ] ;
    $options = $parse [ "db_options" ] ;
    $attributes = $parse [ "db_attributes" ] ;

    foreach ( $parse [ "dsn" ] as $k => $v ) {
        $dsn .= "${k}=${v};" ;
    }

    self :: $link = new PDO ( $dsn, $user, $password, $options ) ;

    foreach ( $attributes as $k => $v ) {
        self :: $link -> setAttribute ( constant ( "PDO::{$k}" )
            , constant ( "PDO::{$v}" ) ) ;
    }

    return self :: $link ;
}

public static function __callStatic ( $name, $args ) {
    $callback = array ( self :: getLink ( ), $name ) ;
    return call_user_func_array ( $callback , $args ) ;
}

} configuration file config.ini

db_driver=mysql
db_user=username
db_password=password

[dsn]
host=hostname
port=3306
dbname=databasename

[db_options]
PDO::MYSQL_ATTR_INIT_COMMAND=set names utf8

[db_attributes]
ATTR_ERRMODE=ERRMODE_EXCEPTION
############

Code to insert query:

$pdo = Database::prepare("INSERT INTO customer (first_name, last_name, join_date) VALUES (?, ?, ?)");
$pdo->bindParam(1, $firstName);
$pdo->bindParam(2, $lastName);
$pdo->bindParam(3, $currentDate);
$pdo->execute();
$custId = $pdo->lastInsertId();
$pdo->closeCursor();

Thanks in advance.

Amit Patil

The Object should be PDO to get lastInsertId(). Correction in call bellow.

$pdo = Database::getLink();
$statement = $pdo->prepare("INSERT INTO customer (first_name, last_name, join_date) VALUES (?, ?, ?)");
$statement->bindParam(1, $firstName);
$statement->bindParam(2, $lastName);
$statement->bindParam(3, $currentDate);
$statement->execute();
$custId = $pdo->lastInsertId();
$statement->closeCursor();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PHP Error: Fatal error: Call to undefined method

Fatal Error - Call to undefined method "Customersss::throwError()"

Fatal error: Call to undefined method db::_results()

Fatal error: Call to undefined method MongoCollection::insertMany()?

Fatal error: Call to undefined method connectDB::prepare()

PHP Classes - Fatal error: Call to undefined method

Fatal Error :: Call to undefined method in codeigniter

Fatal error Call to undefined method VmVendorPDF::convertHTMLColorToDec()

Fatal error: Call to undefined method DB::getInstance()

xampp - Fatal error: Call to undefined method

PHP: Fatal error: Call to undefined method mysqli

Fatal error: Call to undefined method DOMDocument::getElementsById()

PHP - Fatal Error: Call to undefined method - but method exist

Fatal error: Uncaught Error: Call to undefined method why?

PHP Fatal error: Call to undefined method stdClass::save() - Yii framework

Fatal error: Call to undefined method GuzzleHttp\Client::request() with Guzzle 6

php Fatal error: Call to undefined method TCPDF::addTTFfont() in

Fatal error: Call to undefined method Google_IO_Curl::authenticatedRequest()

Fatal error: Call to undefined method mysqli::num_rows()

PHP: Fatal error: Call to undefined method case_controller

Fatal error: Call to undefined method PDO::select() in line 9

Fatal error: Call to undefined method stdClass::_setSession() on line 58

Fatal error: Call to undefined function

PHPUnit Error Fatal error: Call to undefined method Mock_Game_073a8e20::method()

get error in wordpress+woocommerce: Fatal error: Call to undefined method WP_Error::get_message()

Call to undefined method error

PHP Fatal error: Uncaught Error: Call to undefined method PHPUnit\Util\Configuration::getExtensionConfiguration()

Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::get_result()

Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::fetch_assoc()