ThingEngineer / PHP-MySQLi-Database-Class

Wrapper for a PHP MySQL class, which utilizes MySQLi and prepared statements.
Other
3.29k stars 1.35k forks source link

Get id of the last updated row ? #925

Open maan81 opened 3 years ago

maan81 commented 3 years ago

I was looking as this https://stackoverflow.com/questions/1388025/how-to-get-id-of-the-last-updated-row-in-mysql#answer-1751282 . How to execute it ?

I have done :

    $sql = 'SET @scanning_id := 0;'.
            'UPDATE mytable '.
            'SET status = "true", '.
            'id = (SELECT @scanning_id := id) '.
            'WHERE param = "asdf" LIMIT 1; '.
            'SELECT @scanning_id;';

    $id = $db->rawQuery($sql);

I get the error :

    PHP Fatal error:  Uncaught Exception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE mytable SET status = "true", id = (SELECT @scanning_id := id) WHERE hr' at line 1 query: SET @id := 0; UPDATE mytable SET status = "true", id = (SELECT @scanning_id := id) WHERE params = "asdf" LIMIT 1; SELECT @scanning_id; in PHP_MySQLi_Database/MysqliDb.php:2006
maan81 commented 3 years ago

Is it possible to run raw unprepared queries ?

kythuatwebso commented 3 years ago

Only method insert() can return last_id. method update() only return status update (check is: "$db->count" )