Question Updating PHP 5.x code to be PHP 7.3+ compatible?

Skillz

Golden Member
Feb 14, 2014
1,133
1,153
136
I recently acquired a fairly large and complex web site that's coded in PHP mostly (for the front end) that I need to update to work in a 7.3 or higher PHP environment. What are my options with doing this with no coding experience?

The web site is a stats gathering web site for distributed computing web sites. https://stats.free-dc.org/stats.php?page=index

There are a lot of
  • mysql_query()
  • mysql_num_rows()
  • mysql_fetch_array()

extension/functions or whatever it's called. ChatGPT says its mysql_* extensions. This code needs to be converted to mysqli_* extensions/functions and while I have tried to use ChatGPT to just convert the code for me it makes a lot of wrong assumptions and seems to add stuff that doesn't belong. Such as adding code for connecting to the MySQL (MariaDB) database with the username, dbname, password, etc.. that wasn't in the original code as the connection was already established from whatever file called that file. I'm over my head here, but hopefully what I am saying makes sense.

The problem is there are a LOT of php files on this web site and it seems they all need to be updated. I can't really trust ChatGPT to do this as when it does make mistakes or adds stuff that doesn't belong or omits stuff that does belong I'm not smart enough to see this to tell it to correct it. I got lucky when I noticed the connection code that appeared in the script and asked ChatGPT why it was there lol
 

Sgraffite

Member
Jul 4, 2001
183
121
116
I understand what you are saying, but I am not sure of an easy way with no coding experience. One option is gaining coding experience, trial and error is how I learned PHP. Another is finding someone to help. Another is paying someone to help.

How to trial and error:
Breaking down the problem will really help as the sheer amount of files may be intimidating. If you can isolate a spot to do some trial and error so you are effecting a fairly minimal part of the site, that can get you progressing forward. Maybe that is a login page, or some one-off page that just reads some simple data from one table. Things can be entangled in the source code for weird reasons so it may not be so simple in that regard, but maybe it is. PHP is interpreted you may want to put yourself in a position to be able to change a file, save, and reload the page and get instant feedback on if you broke something or not. Maybe there is a better way these days but that is how I did it in the early 2000s with Notepad++.

If you are able to isolate a simple page get the change to work you should understand kind of what to do for similar cases.

Knowing what to change:
You'll have to change the includes obviously to use mysqli library. Find equivalentish calls to run queries if they are different and maybe some other stuff. I have not use mysqli in a long time, I remember it supported question marks for SQL params before, but hopefully it also supports named parameters as they would be preferred. If you care about avoiding SQL injection you would want to stick to those and convert any "parameters" that are being concatenated directly to SQL query strings. Basically get an idea of the convention that will be used as it will likely be similar between different pages that ask for data.

ChatGPT in my experience really struggles with context and as you move through a discussion it will slowly/randomly forget things that are important, so if you don't know what to look for you may not notice how it is steering your wrong. You end up maybe with a concept of what to do but sprinkled with issues.

Haven't done PHP since 2011 or so, so I apologize if things have changed so much this doesn't make sense.
 
  • Like
Reactions: MangoX

Red Squirrel

No Lifer
May 24, 2003
70,166
13,573
126
www.anyf.ca
It's a bit of a pain as lot of stuff has changed between those versions. The biggest thing is going from mysql to mysqli library, so the way things are done is a bit different and the parameters are different too.

Depending on how the app is written though it ideally has a wrapper class that does all the query stuff and the mysql calls are within that class, so it may not be that hard. But really, you pretty much need to look at error output and fix stuff as you go. So first step is to ensure you have a dev server you can work on so you're not doing this live, and enable all error output, even warnings, and just plug away and fix stuff. You will want to test every aspect of the app to see if more errors get triggered. Also newer versions of Mysql are strict about wanting a default value for certain data types like most numerical types and I believe char as well. So you may also need to modify the table structures to specify a default.

There are some other ways of doing stuff that have be deprecated which may require to rewrite some code too, I don't recall from top of my head as the mysql stuff is what is typically the biggest one I've run into.
 
  • Like
Reactions: Ken g6

Skillz

Golden Member
Feb 14, 2014
1,133
1,153
136
Well I switched to Claude AI (Paid version) and have been letting it convert php files. So far I have some working pages on the test site that pulls the data and displays them. I just have a long way to go before I get them all done and even with the paid version of Claude I still get limited after doing ~10 - 20 file checks and have to wait for the limit to reset ~4 hours later before I can start again. But it is promising.

As for the database, I just have it connecting to the current database on the live database server to pull the data from it. I do have a new server I intend to migrate the database server to along with an updated operating system and everything else updated as well. (MariaDB, Perl, PHP, etc...) Just so much that needs to be done.
 
  • Like
Reactions: Sgraffite