Welcome to WebmasterWorld Guest from 34.237.76.91

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Help with Fatal error after upgrade to php7

Upgrade to php7 fatal error

     
4:55 pm on Feb 18, 2018 (gmt 0)

Preferred Member from US 

10+ Year Member

joined:Nov 27, 2002
posts: 418
votes: 1


After an upgrade to PHP7 - error

1) I have this page which is a form to fill out and submit.

2) When it is filled out and submitted I get error [Fatal error: Uncaught Error: Call to undefined function mysql_insert_id() in /home/example/public_html/class/admin_class.php:137 Stack trace: #0 /home/example/public_html/become_dealer.php(94): admin->add_dealer('1234*', 'Test', 'ES1388704', 'Name', 'Surname', 'contact@test...', 'NNN-000-0000', '', '6170', '', 'City', '29', '89139', '1', '5', 'Friends') #1 {main} thrown in /home/example/public_html/class/admin_class.php on line 137]

3) I contacted my hosting company and they said
[Could be related to the php and sql upgrades out servers underwent. Although that was months ago
But here sounds like a small adjustment needs to happen in admin_class.php
There must be some kind of replacement for mysql_insert_id()
basically that function isn't php7 compatible from what I'm googling]

Any idea how to go about fixing this anyone?



[edited by: not2easy at 5:09 pm (utc) on Feb 18, 2018]
[edit reason] privatized [/edit]

6:23 pm on Feb 18, 2018 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 20, 2017
posts:334
votes: 73


Hi,

"mysql_xxx" functions are depreciated since June 2013. Instead you need to use the "mysqli_xxx" (with the "i") functions or the PDO_MySQL class.

So in your case, you need to use :

mysqli_insert_id()
or PDO::lastInsertId()

[php.net...]
[php.net...]
7:51 am on Feb 19, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Sept 25, 2005
posts:2091
votes: 370


See also: Migrating from PHP 5.6.x to PHP 7.0.x [php.net] (specifically: Backward incompatible changes [php.net]
8:31 am on Feb 19, 2018 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 2, 2016
posts: 343
votes: 59


Yes, you need to alter your code and change any reference from mysql to mysqli in the admin_class.php. The syntax is almost identical, it should not be that hard.
12:18 pm on Feb 19, 2018 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 20, 2017
posts:334
votes: 73


Just to add that each intermediate version of PHP also introduces depreciated functions and backward incompatibility.

So if you use PHP 7.0.x, 7.1.x or 7.2.x this is not exactly the same. You have the whole list here : [secure.php.net...]
6:20 pm on Feb 19, 2018 (gmt 0)

Preferred Member from US 

10+ Year Member

joined:Nov 27, 2002
posts: 418
votes: 1


Ok so the form submits and I get an email now after making the changes above (Changed , thank you. But...
I get this error now below... any ideas?

Changes I made..
LINE: 64
FROM: $uid=mysql_insert_id(); TO: $uid=mysqli_insert_id();

LINE: 137
FROM: $uid=mysql_insert_id(); TO: $uid=mysqli_insert_id();

----New Error---

Warning: mysqli_insert_id() expects exactly 1 parameter, 0 given in /home/eprovide/public_html/class/admin_class.php on line 137

Warning: Cannot modify header information - headers already sent by (output started at /home/eprovide/public_html/class/admin_class.php:137) in /home/eprovide/public_html/become_dealer.php on line 185

----END NEW ERROR---
6:25 pm on Feb 19, 2018 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 20, 2017
posts:334
votes: 73


The error message explains it. The function requires one parameter, which is the mysql link identifier, returned when you opened the link with your database server ( using mysqli_connect() or mysqli_init() for example ).

Documentation of the function : [secure.php.net...]
6:31 pm on Feb 19, 2018 (gmt 0)

Preferred Member from US 

10+ Year Member

joined:Nov 27, 2002
posts: 418
votes: 1


Ok I am kinda php dumb, thanks Peter, not sure what needs to be altered in the code now..

What I have there (showing in error messages)

become_dealer.php
Line 185 is
header("Location:dealer_thank_you.php?msg=2&dealer_code=".$dealer_code);

admin_class.php
Line 137 is
$uid=mysqli_insert_id();
9:57 pm on Feb 19, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Sept 25, 2005
posts:2091
votes: 370


This seems like a bad idea. If you don't know what you're doing in the code, better get someone to do the work before you break the site.

After all, mysqli_insert_id() is unlikely to be the only function you need to change.
10:18 pm on Feb 19, 2018 (gmt 0)

Preferred Member from US 

10+ Year Member

joined:Nov 27, 2002
posts: 418
votes: 1


Robzilla yah... I think it's almost there, form submits now. Just need to figure out this next issue.

Changes I made..
LINE: 64
FROM: $uid=mysql_insert_id(); TO: $uid=mysqli_insert_id();

LINE: 137
FROM: $uid=mysql_insert_id(); TO: $uid=mysqli_insert_id();

----Latest New Error---

Warning: mysqli_insert_id() expects exactly 1 parameter, 0 given in /home/eprovide/public_html/class/admin_class.php on line 137

Warning: Cannot modify header information - headers already sent by (output started at /home/eprovide/public_html/class/admin_class.php:137) in /home/eprovide/public_html/become_dealer.php on line 185

----END NEW ERROR---
10:41 am on Feb 20, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Sept 25, 2005
posts:2091
votes: 370


As Peter_S wrote, the mysqli_insert_id() function "requires one parameter, which is the mysql link identifier". Calling the function without a link identifier only worked with mysql_insert_id().

So if you're using mysqli_connect() to set up a link to a database, you refer to that connection when calling the mysqli_insert_id() function, as described in the PHP documentation. This way, PHP knows which database connection it has to get the newest inserted ID from.

However, chances are you still use mysql_connect() rather than mysqli_connect(), and the two can't work together, so mysqli_insert_id() likely won't work as intended.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members