-1

Recently my hosting site (HostGator) upgraded my site's PHP from 5.4 to 7.4. Pages that had php with mysqli statements in them failed, after working without incident in version 5.4. Code like:

<body>
<?php include("menu_primary.html");?>
Some text
<?php 
    $servername = "localhost";
    $username = "my_username";
    $password = "my_password";
    $database = "my_database";
    
    // Create connection
    $link = mysqli_connect($servername, $username, $password, $database);
        // Check connection
        if (!$link) {
            die("Connection failed: " . mysqli_connect_error());
        }   

would fail on the statement: mysqli_connect($servername, $username, $password, $database);
but the statement <?php include("menu_primary.html");?> would execute fine. After a lot of trial and error, I got the idea to move the entire PHP file from the folder /public_html/my_app to the folder /public_html . Then everything worked fine again.
Those two files were edited as part of the upgrade from PHP 5.4 to PHP 7.4.
Question: Is there something I have to do in the /public_htmnl/.htaccess or the /public_html/php.ini files to allow embedded PHP to execute in a directory subordinate to /public_html ? Thanks for looking at this.

user3138025
  • 795
  • 4
  • 17
  • 46
  • 2
    There's nothing obviously wrong with that code and should work the same in 7.4, no matter where the file is located. Please elaborate on how it fails (since it can mean many different things). What actually happens? Do you see any errors on the screen? Have you checked the error log? – M. Eriksson Apr 30 '21 at 16:50
  • Why did you edit .htaccess? Just upgrading PHP shouldn't require any changes in that file, since it's a config file for Apache (the web server). – M. Eriksson Apr 30 '21 at 16:58
  • Make sure you have enabled mysqli error reporting [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Apr 30 '21 at 17:00
  • The error produced is: Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /home2/susanna/public_html/new_parishioner/input1_test.php:55 Stack trace: #0 {main} thrown in /home2/susanna/public_html/new_parishioner/input1_test.php on line 55 Line 55 is the $link = statement – user3138025 Apr 30 '21 at 17:02
  • I did not edit the .htaccess nor the php.ini file. The hosting company did. It was a while before the error was noticed, so the backups were erased. However there's a line in the .htaccess file that seems to enable php 7.4: AddHandler application/x-httpd-ea-php74 .php .php7 .phtml – user3138025 Apr 30 '21 at 17:06
  • If you get that error if the file is in a sub folder but not in the root folder, I would suggest you contact your hosting company. That must be some weird configuration on their part, which we can know (and probably something they need to fix on their end). – M. Eriksson Apr 30 '21 at 17:06
  • I believe error reporting is enabled. The .htaccess file contains the line php_flag display_errors On – user3138025 Apr 30 '21 at 17:07
  • I have contacted HostGator, but don't have an answer yet. I posted here to see if someone is aware of a setting that can be set to enable PHP starting from a subordinate folder. – user3138025 Apr 30 '21 at 17:10
  • Try copying `/public_html/php.ini` to `/public_html/my_app/php.ini`. Perhaps the host no longer allows a directory to inherit the php config, and now requires it in the directory itself. If `my_app` is running a default php config then it probably has the mysqli extension disabled, which would cause that error. – MrCode Apr 30 '21 at 17:43
  • Thanks for the suggestion MrCode, but that had no effect. – user3138025 Apr 30 '21 at 19:23
  • removing php7.3 packages and keeping only php7.4 packages corrected the problem in debian11 – ShaileshKumarMPatel Oct 08 '21 at 21:11

1 Answers1

0

After many days I got an answer from my hosting company. The file .htaccess was the problem. When the site upgraded from 5.4 to 7.4 it added the following line:

suPHP_ConfigPath /home2/susanna

The public_html folder is subordinate to that path. Their escalated support commented out the entire section that was added:

#<IfModule mod_suphp.c>
# suPHP_ConfigPath /home2/susanna
 #<Files php.ini>
#   order allow,deny
#   deny from all
# </Files>
#</IfModule>

Now the PHP code (executing on php 7.4) can be called from a subordinate directory.

user3138025
  • 795
  • 4
  • 17
  • 46