Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql_insert_id returns 0 after SELECT query

Status
Not open for further replies.

Rutsagig

IS-IT--Management
Jun 24, 2019
9
ES
I have and old PHP5 web thats use mysql_connect (I know it's deprecated). I changed it to a new server and it starts failing.
After checking it, I found the problem is the mysql_insert_id function.
AFAIK, It's supossed to get the last autoincrement generated ID from SQL insert query, but if I use SELECT queries after the INSERT ones, it returns 0.

Here is an example:
PHP:
mysql_connect("localhost","user","pass");
mysql_select_db("test");
mysql_query("INSERT INTO table VALUES('', 'TEST')");
echo "<br>LAST ID: " . mysql_insert_id();
mysql_query("SELECT * FROM table");
echo "<br>LAST ID: " . mysql_insert_id();
In old server, both values are the same, but in new server second value is 0. If I delete the select, values are OK.
Is there any tweak I could do to PHP or MySQL settings in order to correct this? I want to avoid changing the code.

New server is:
Ubuntu 18.04
Apache
10.1.47-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04
PHP 5.6.40-29+ubuntu18.04.1+deb.sury.org+1

Old server is:
LiteSpeed V7.8 CloudLinux 1.2
MariaDB 10.3.28
PHP Version 5.6.40
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top