-1

I went through this guide and the code provided does not work. I run Drupal core 10.0.0-alpha4 with PHP8.1 and whenever I run the below code I get HTTP ERROR 500, which is a PHP error, the question is how to adjust this code and make it work with php8.1?

<?php
$db = mysql_connect('localhost','user','password');
if(!$db) echo "cannot connect to the database";
mysql_select_db('user_drpl1');
$result=mysql_query('OPTIMIZE TABLE accesslog,cache,comments,node,users,watchdog;');
echo mysql_error();
?>

The error message I get when I run this script:

Stack trace:
#0 {main}
  thrown in /var/www/example.com/html/drupal/optimize.php on line 2" while reading response header from upstream, client: xx.xx.xxx, server: example.com, request: "GET /optimize.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php8.1-fpm.sock:", host: "example.com"
2022/05/28 17:54:57 [error] 897442#897442: *29717 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Call to undefined function mysql_connect() in /var/www/example.com/html/drupal/optimize.php:2
Abe
  • 327
  • 2
  • 15
  • _"I get HTTP ERROR 500, which is a PHP error"_ - No, a 500 error is a generic "internal server" error. It can be caused by PHP, but it can also be caused by configuration issues on the server itself. – M. Eriksson May 28 '22 at 16:15
  • 2
    You can't use the `mysql_*` extension in PHP 8.1. That extension was deprecated back in PHP 5.5 and **completely removed** in PHP 7. Use mysqli or PDO instead. – M. Eriksson May 28 '22 at 16:16
  • I checked the error log and updated my post with the error I get. I hope it helps. – Abe May 28 '22 at 16:16
  • 1
    Does this answer your question? [Why shouldn't I use mysql\_\* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – M. Eriksson May 28 '22 at 16:22

0 Answers0