Загрузка...

Troubleshooting PHP 7.4 with mysqli: Why It Only Runs in the Root Directory

Discover why PHP 7.4 with `mysqli` may only execute code from the root directory and learn how to fix it.
---
This video is based on the question https://stackoverflow.com/q/67337481/ asked by the user 'user3138025' ( https://stackoverflow.com/u/3138025/ ) and on the answer https://stackoverflow.com/a/67372748/ provided by the user 'user3138025' ( https://stackoverflow.com/u/3138025/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Why does PHP 7.4 with mysqli only run in root directory?

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Why Does PHP 7.4 with mysqli Only Run in the Root Directory?

If you've upgraded your website's PHP version from 5.4 to 7.4 and noticed that certain pages using mysqli statements aren't functioning as expected, you're not alone. Many users encounter issues where their PHP code simply fails to execute, while other parts of their pages, like HTML includes, work perfectly. This guide will explain why this issue occurs and how you can resolve it.

The Problem Explanation

After upgrading PHP, some parts of your code that utilize mysqli to connect to a database might stop working. For instance, you may have experienced a failure in a connection statement like this:

[[See Video to Reveal this Text or Code Snippet]]

While other parts of your page, such as including HTML files, work normally. Confusing, right? This issue can often lead to frustration, especially if your site was running well under the older PHP version.

As one user discovered, the issue may originate from the website's configuration files, particularly .htaccess and php.ini. When their hosting provider upgraded the PHP version, the changes made to the .htaccess file were the root of the problem.

The Solution: Editing the .htaccess File

Step 1: Identify the Problematic Configuration

After extensive troubleshooting, the user's support team identified that the following code was added to the .htaccess file during the upgrade:

[[See Video to Reveal this Text or Code Snippet]]

This line caused the environment to restrict the execution of PHP scripts to the root directory only (/public_html), preventing access to any scripts located in subordinate directories (like /public_html/my_app in this case).

Step 2: Comment Out the Problematic Section

The solution was straightforward: comment out the section of the .htaccess file that enforced this limitation. This was done by modifying the file to look like this:

[[See Video to Reveal this Text or Code Snippet]]

By commenting out these lines, the PHP code successfully executed from the subordinate directory, restoring the intended functionality of the site.

Step 3: Test Your Code

After making the changes:

Save the Changes: Make sure to save your edited .htaccess file.

Test Your Pages: Load the pages that were previously failing to see if they now work correctly.

Important Notes

Always back up your .htaccess file before making any changes.

If you're unsure about editing configuration files, consider consulting your web hosting support or an experienced developer.

Conclusion

If you're facing similar issues after a PHP upgrade, examine your .htaccess file for any newly added configurations that might restrict PHP execution in subordinate directories. Following the steps outlined can help you restore the functionality of your site without much hassle.

With a little troubleshooting, your PHP code can once again run seamlessly, regardless of its directory structure. If you have any experiences or additional tips regarding PHP upgrades and configurations, feel free to share your thoughts in the comments below!

Видео Troubleshooting PHP 7.4 with mysqli: Why It Only Runs in the Root Directory канала vlogize
Яндекс.Метрика

На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.

Об использовании CookiesПринять