All jobs

Fix Call to Undefined Function mysql_connect() on PHP 7 and PHP 8

An agent moves each mysql_* call to mysqli with the connection passed explicitly, then exercises every page against a real MySQL-compatible server on PHP 8.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The mysql_* functions were deprecated in PHP 5.5 and removed in PHP 7.0. A script that calls them on a current host dies with Fatal error: Uncaught Error: Call to undefined function mysql_connect(). The replacement, mysqli, has a function for each old one, and the catch is in the arguments: the old API kept one hidden global connection, and mysqli wants the connection passed to almost every call, as the first argument. A search-and-replace that adds an i produces code that parses and then fails on every query. The agent does the conversion call by call and runs each page to prove it.

Goatella/Simple-PHP-Login is four small scripts and a one-line schema in SQL.txt. add.php and members.php open with mysql_connect(...) and mysql_select_db(...) and go on to use mysql_query, mysql_num_rows, mysql_fetch_array and mysql_error. The agent creates the connection once with mysqli_connect, giving the database name as the fourth argument, and passes that handle to each query. login.php shows what a hurried earlier fix looks like: it already calls mysqli_connect and mysqli_query, and every failure branch still says or die(mysql_error()). That function no longer exists, so the first real database error would be replaced by a second fatal error about a missing function, hiding the cause.

Moving to PHP 8 surfaces more than the database calls, and the agent runs php -l and then each page to find them. add.php calls get_magic_quotes_gpc(), which PHP 8.0 removed. login.php writes setcookie(ID_your_site, ...) and logout.php writes setcookie(ID_my_site, gone, $past) with bare words where strings were meant; PHP 8 throws Undefined constant for those. Since PHP 8.1 mysqli also throws exceptions by default, so the or die(...) pattern never runs. The agent replaces it with one error handler that logs the detail and shows the visitor a plain message.

Every query in these files is built by pasting request data into SQL, in members.php straight from a cookie. The agent has to touch each query call anyway, so it converts them to prepared statements with bound parameters and says so as a separate commit. It reports the things that are bugs or risks and outside this job: logout.php expires cookies named ID_my_site while login.php sets ID_your_site, so logging out clears nothing; passwords are stored as unsalted md5; and login.php connects to a database called forks while the other pages name a different placeholder. It changes none of the three, because each alters behaviour for existing users.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "This PHP project fails on PHP 7 or 8 with a fatal error because mysql_connect() and the other mysql_* functions no longer exist. List every call to a mysql_* function with file and line. Convert each to mysqli, procedural or object style to match the surrounding code: create one connection per request and pass it explicitly to every query, error and escape call; give the database name to mysqli_connect instead of calling select_db separately. Check for half-finished earlier conversions, such as mysqli calls whose error branch still calls mysql_error. Keep credentials where they are read from today. In a second commit, convert queries that concatenate request, cookie or session data into prepared statements with bound parameters, without changing what the query returns. Then install the newest PHP 8 CLI with the mysqli extension and a MariaDB server in the sandbox, create the schema from the repository's SQL file, run php -l on every file, start php -S and request every page with curl through its main paths (for a login script: register, log in, view the protected page, log out). Fix whatever else stops a page on PHP 8, such as removed functions and bare-word constants, and list each such fix. Replace or die(mysql_error()) with one error handler that logs the database error and shows a generic message. Do not change the password hashing scheme, the table layout or cookie names; report problems with them instead, with file and line. Include the commands, the curl transcripts and the PHP error log in the pull request description.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request with two commits. The first is the conversion: a table in the description maps each old call to its new one with file and line, and a second, shorter list covers what else PHP 8 rejected, such as the removed magic-quotes function and the unquoted cookie names. The second commit is the prepared statements, kept apart so a reviewer who wants the smallest possible fix can take the first alone.

The description includes the curl transcript of a full session against the local server: registering a user, logging in, loading the members page with the cookies set, and logging out, followed by the PHP error log for that session. A reviewer reads the log first. Any warning or notice left in it is listed with a reason. They then read the report section, where the logout cookie mismatch and the md5 passwords are described with the lines involved, and decide what to schedule next. The agent notes one fact that helps that decision: the password VARCHAR(60) column in SQL.txt is exactly wide enough for a bcrypt hash from password_hash.

Run it on every site moved to a PHP 8 host

This error is rarely a surprise for one project and often a surprise for twenty, on the day a hosting provider retires PHP 5.6 or 7.x. An agency with many small sites can start a run per repository from a script that sends the request above with each repository's URL in resources. Send a request_id with each call, a UUID you store next to the repository name, so a retry after a network error returns the run that already exists.

If the sites report errors to a tracker such as Sentry, a handler on your server can receive its webhook and start the run for the project it names. Sentry signs its own requests and cannot send the Authorization: Bearer header a Rush webhook trigger needs, so the handler is what calls Rush. It can also add the failing URL and stack trace to the end of input, so the agent starts from the page that broke.

The use case this job follows, with its automation and trigger: Fix a bug from an alert.

Why run it on Rush?

The conversion is only finished when each page has been requested against a live database on the new PHP version. That needs PHP 8 with mysqli, a MySQL-compatible server and an empty schema, which most developers no longer keep on a laptop for a ten-year-old login script. The agent installs them in its sandbox, loads SQL.txt, serves the files with PHP's built-in server and drives them with curl. Each request and response is kept in the session transcript, so the claim that login works rests on a recorded exchange.

The sandbox is also the right place to try hostile input. To show that the prepared statements changed nothing for normal use and closed the injection path, the agent sends a username containing a quote and a crafted cookie value before and after the second commit, and includes both responses. Running that against a shared staging database is a risk nobody needs to take; here the database exists for this run and holds nothing.

Frequently asked questions

Can I just replace mysql_ with mysqli_ everywhere?

No. mysqli_query, mysqli_error, mysqli_real_escape_string and others take the connection as their first argument, and mysqli_connect_error() replaces mysql_error() for a failed connection. A blind rename gives you code that parses and fails at the first query. mysqli_fetch_array and mysqli_num_rows do keep their shape.

Should it be mysqli or PDO?

For getting an old script running, mysqli, because its procedural functions map one to one onto the old calls and the diff stays readable. PDO is the better target when you also want to support another database or are restructuring the code. Say which you want in the prompt; the agent follows the request and keeps to one API across the project.

Is there a shim that defines the old functions again?

Several exist, and they get a site back up quickly. They also keep string-built queries and the hidden global connection, and they need maintaining with each PHP release. The agent uses a shim only if you ask for a stopgap, and then lists every call that still goes through it.

Will existing users still be able to log in afterwards?

Yes, as long as the hashing stays as it is, which is why the agent does not replace md5 in this pull request. Stored hashes, column names and cookie names are untouched. Moving to password_hash needs a rehash-on-login step and is described in the report as follow-up work.

More PHP jobs

More jobs like this: Fix a bug from an alert

Keep reading