Hi every one,

I am using this funtion

function get_last_inserted_id($table) {
    include "dbsetting/adm_vars_config.php";
    include "dbsetting/classdbconection.php";
    $dbacosis = new dbacosis();
    $sqlacosis  = $dbacosis->queryacosis("SELECT LAST_INSERT_ID() AS `last_id` FROM `pcosis_courses`;");
        if(!$sqlacosis or   mysql_num_rows($sqlacosis) == 0) {
            return false;
        } else  {
            $data = mysql_fetch_array($sqlacosis);
            return $data['last_id'];
        }
}

$id = get_last_inserted_id("courses");
echo $id;

but it reture zero. anybody have idea.

Recommended Answers

All 3 Replies

Member Avatar for diafol
$data = mysql_fetch_array($sqlacosis);
return $data['last_id'];

When you say 'zero' do you mean 'false' (first conditional branch result) or do you mean $data['last_id'] = 0 ?

Also the code above - are you certain that you're getting a mysql resource? Not sure about you mixing a db object and free range procedural...

Aren't they...

$sqlacosis->num_rows

and

$data = $sqlacosis->fetch_assoc()

Or something similar?

When you use LAST_INSERT_ID() you cannot select the table. Also the id must be an auto increment column, if for example you're using uuid_short() you will get an empty result. And the value is related to the session of the client, if the insert is done by client A, client B will not see the last inserted id:

If this does not help show your insert query and the table structure.

Member Avatar for diafol

ALternatively, you could, on an autoincrement PK, just select the last item by

... ORDER BY pkeyfield DESC LIMIT 1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.