PDO last insert id returns null or no method found

Mevia

Ive been searching for long time some solution but none of them are working, tried all the topics from stackoverflow and nothing.

This is my connection script:

private function __construct() {
    try {
        $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));
        $this->_pdo->exec("SET NAMES utf8");
    } catch(PDOException $e) {
        die($e->getMessage());
    }
}

This is my method to query:

public function query($sql, $params = array()) {
    $this->_error = false;
    if($this->_query = $this->_pdo->prepare($sql)) {
        $x = 1;
        if(count($params)) {
            foreach($params as $param) {
                $this->_query->bindValue($x, $param);
                $x++;
            }
        }

        if($this->_query->execute()) {
            $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount();
        } else {
            $this->_error = true;
        }
    }

    return $this;
}

I had to redone my account activation system and put activation codes to separate table, so when new user come and register i need to grab insert id to place in activation codes table under user_id. The problem is i dont know how to alter script i provided to be able to grab that id.

I tried $this->_pdo->lastInsertId();

and $this->_db->lastInsertId();

in first case i get null all the time in second case i get no method lastInsertId() been found in class

maybe i place it in wrong place or do it wrong, help would be nice, thank you in advance;)

btw im using pdo under mysql server

Alex
if($this->_query->execute()) {
    $id = $this->_pdo->lastInsertId();
    $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
    $this->_count = $this->_query->rowCount();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Insert Returning last ID with PDO

From Dev

MySQL : Stored procedure returns null for last_insert_id

From Dev

PDO not being able to return the last_insert_id

From Dev

Get last insert id return null

From Dev

Implement a method that generates the last insert id

From Dev

Implement a method that generates the last insert id

From Dev

LAST INSERT ID SQL Subquery returns more than one row

From Dev

why does LAST_INSERT_ID() returns foreign key

From Dev

LAST INSERT ID SQL Subquery returns more than one row

From Dev

How to get last inserted id from insert method laravel 5.1

From Dev

Getting last insert id 0 in magento with Zend_Db_Adapter_Pdo_Mysql in magento

From Dev

Is it mandatory to insert a new record just before calling lastInsertId() to get the last inserted ID in PDO?

From Dev

PHP PDO class Query method returns true or false when no record found

From Dev

Trying to reach the returned ID I get after I insert value but mysqli_insert_id() returns null

From Dev

Getting ID of last insert

From Dev

Using where id = last_insert_id() returns empty result set

From Dev

Moq Returns method returns null

From Dev

The getActionBar method returns null

From Dev

Method override returns null

From Dev

querySelector method returns null

From Dev

A method that returns the object last in focus?

From Dev

SQL, select last insert ID by LAST_INSERT_ID()

From Dev

$("#id").scrollTop() returns null

From Dev

$("#id").scrollTop() returns null

From Dev

Get last insert id of Postgresql

From Dev

Mysqli last insert id not work

From Dev

Dropdown - Insert selected values - pdo method not working

From Dev

Meteor - insert failed: Method not found

From Dev

How to insert row if ID is not found?

Related Related

  1. 1

    Insert Returning last ID with PDO

  2. 2

    MySQL : Stored procedure returns null for last_insert_id

  3. 3

    PDO not being able to return the last_insert_id

  4. 4

    Get last insert id return null

  5. 5

    Implement a method that generates the last insert id

  6. 6

    Implement a method that generates the last insert id

  7. 7

    LAST INSERT ID SQL Subquery returns more than one row

  8. 8

    why does LAST_INSERT_ID() returns foreign key

  9. 9

    LAST INSERT ID SQL Subquery returns more than one row

  10. 10

    How to get last inserted id from insert method laravel 5.1

  11. 11

    Getting last insert id 0 in magento with Zend_Db_Adapter_Pdo_Mysql in magento

  12. 12

    Is it mandatory to insert a new record just before calling lastInsertId() to get the last inserted ID in PDO?

  13. 13

    PHP PDO class Query method returns true or false when no record found

  14. 14

    Trying to reach the returned ID I get after I insert value but mysqli_insert_id() returns null

  15. 15

    Getting ID of last insert

  16. 16

    Using where id = last_insert_id() returns empty result set

  17. 17

    Moq Returns method returns null

  18. 18

    The getActionBar method returns null

  19. 19

    Method override returns null

  20. 20

    querySelector method returns null

  21. 21

    A method that returns the object last in focus?

  22. 22

    SQL, select last insert ID by LAST_INSERT_ID()

  23. 23

    $("#id").scrollTop() returns null

  24. 24

    $("#id").scrollTop() returns null

  25. 25

    Get last insert id of Postgresql

  26. 26

    Mysqli last insert id not work

  27. 27

    Dropdown - Insert selected values - pdo method not working

  28. 28

    Meteor - insert failed: Method not found

  29. 29

    How to insert row if ID is not found?

HotTag

Archive