PDO not being able to return the last_insert_id

Quick Duck
   <?php
    session_start();


            $userid = $_SESSION['userid'];
            $categorynumber=0;
            $categorynumber = create_category($userid);
            //keep the category number for future reference
            if($categorynumber > 0){
                $_SESSION['categorynumber'] = $categorynumber;
            }

            echo $categorynumber;



    ?>

echo $categorynumber prints 0

    function create_category($userid)
{

    // connect to database with PDO
    $dsn = 'mysql:host='.DB_HOST.';dbname='.DB_DATABASE;
    $dbh = new PDO($dsn, DB_USER, DB_PASSWORD);

    // insert category and get the category_id
    $categorynumber = 0;
    $stmt = $dbh->prepare("INSERT INTO categories (userid, name) VALUES (:userid, :name);SELECT LAST_INSERT_ID();");
    $stmt->bindValue(':userid', $userid, PDO::PARAM_STR);
    $stmt->bindValue(':name', 'test7', PDO::PARAM_STR);
    if ($stmt->execute())
    {
        $result = array();
        while ($row = $stmt->fetch()) {
            array_push($result, $row);
        }

        if (isset($result[0][0]))
        $categorynumber = $result[0][0];

    }   

    // close database and return null 
    $dbh = null;
    return $categorynumber;
}

What am I doing wrong here, so that I cannot retrieve the last id of the created entry? How can I return the last_insert_id(); and assign it to the $_SESSION['categorynumber']??

Quick Duck

I have managed to achieve what I wanted with the following line:

$categorynumber = $dbh->lastInsertId();

full example:

    function create_category($userid)
{

    // connect to database with PDO
    $dsn = 'mysql:host='.DB_HOST.';dbname='.DB_DATABASE;
    $dbh = new PDO($dsn, DB_USER, DB_PASSWORD);

    // insert category and get the category_id
    $categorynumber = 0;
    $stmt = $dbh->prepare("INSERT INTO categories (userid, name) VALUES (:userid, :name);");
    $stmt->bindValue(':userid', $userid, PDO::PARAM_STR);
    $stmt->bindValue(':name', 'test7', PDO::PARAM_STR);
    $stmt->execute();
    $categorynumber = $dbh->lastInsertId();

    // close database and return null 
    $dbh = null;
    return $categorynumber;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Both sql LAST_INSERT_ID() and PHP insert_id return 0

From Dev

Draggable with Vue.js, not being able to return to original position

From Dev

Draggable with Vue.js, not being able to return to original position

From Dev

Not being able to grab element by ID using $(this)

From Dev

Insert Returning last ID with PDO

From Dev

Not being able to Insert data into MYSQL tables using PHP

From Dev

Not being able to catch an exception

From Dev

Not being able to catch an exception

From Dev

Not being able to autowire a bean

From Dev

Not being able to use promise

From Dev

How to return a non-zero exit code on a custom Django command while being able to test it?

From Dev

php pdo query fails to return result despite prepared statement being correct

From Dev

php pdo query fails to return result despite prepared statement being correct

From Dev

Not able to install pdo_mysql

From Dev

Jquery Not being able to disable event

From Dev

Not being able to call the right data

From Dev

Jquery Not being able to disable event

From Dev

Not being able to set a value to a combobox

From Dev

Not able to return appropriate value

From Dev

PHP PDO, Return value object after insert query

From Dev

PDO Return All Rows

From Dev

Not being able to access id element to build object for JS list for Ajax post.

From Dev

PDO last insert id returns null or no method found

From Dev

Get last insert id return null

From Dev

Return the id of the last MySQL insert in PHP

From Dev

how to select last_insert_id?

From Dev

How efficient is Last_insert_id?

From Dev

MYSQL last_insert_id() and concurrency

From Dev

LAST_INSERT_ID after restarting MySQL

Related Related

HotTag

Archive