Php mysql http error 500


You've missed to user the $link variable when you were cheking for errors and running the query. The $link variable / connection have to be used everytime you're running something with mysqli_*, since it's the connection.
The mysqli_select_db were also made wrong, you have to define the connection first, then the name of the database.

Question

I’m trying to connect to my MySQL database with php, and I’m getting a http 500 error, and the following in the error log.

[Sat Aug 11 07:33:08.899904 2018] [php7:error] [pid 18763] [client 75.97.225.218:57986] PHP Fatal error:  Uncaught PDOException: SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' in /var/www/html/createaccount.php:14\nStack trace:\n#0 /var/www/html/createaccount.php(14): PDO->__construct('mysql:host=127....', 'root', 'password123', Array)\n#1 {main}\n  thrown in /var/www/html/createaccount.php on line 14

This is the code for createaccount.php

 PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
$pdo = new PDO($dsn, $user, $pass, $opt);
?>


And I can log in with mysql -u root -p and then entering the password password123. So I know it’s the password, or at least I think so, why else would it let me log in.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

The dreaded 500 internal server error. It always seems to come at the most inopportune time and you’re suddenly left scrambling to figure out how to get your WordPress site back online. Trust us, we’ve all been there. Other errors that behave similarly that you might have also seen include the frightening error establishing a database connection and the dreaded white screen of death. But from the moment your site goes down, you’re losing visitors and customers. Not to mention it simply looks bad for your brand.

Today we’re going to dive into the 500 internal server error and walk you through some ways to get your site back online quickly. Read more below about what causes this error and what you can do to prevent it in the future.

  • What is a 500 internal server error?
  • How to fix the 500 internal server error

500 Internal Server Error (Most Common Causes):

500 Internal server error in WordPress can be caused by many things. If you’re experiencing one, there’s a high chance one (or more) of the following elements is causing the issue:

  • Browser Cache.
  • Incorrect database login credentials.
  • Corrupted database.
  • Corrupted files in your WordPress installation.
  • Issues with your database server.
  • Corrupted WordPress core files.
  • Corrupted .htaccess file and PHP memory limit.
  • Issues with third-party plugins and themes.
  • PHP timing out or fatal PHP errors with third-party plugins.
  • Wrong file and folder permissions.
  • Exhausted PHP memory limit on your server
  • Corrupted or broken .htaccess file.
  • Errors in CGI and Perl script.

Check Out Our Ultimate Guide to Fixing the 500 Internal Server Error

What is a 500 Internal Server Error?

The Internet Engineering Task Force (IETF) defines the 500 Internal Server Error as:

The 500 (Internal Server Error) status code indicates that the server encountered an unexpected condition that prevented it from fulfilling the request.

When you visit a website your browser sends a request over to the server where the site is hosted. The server takes this request, processes it, and sends back the requested resources (PHP, HTML, CSS, etc.) along with an HTTP header. The HTTP also includes what they call an HTTP status code. A status code is a way to notify you about the status of the request. It could be a 200 status code which means “Everything is OK” or a 500 status code which means something has gone wrong.

There are a lot of different types of 500 status error codes (500, 501, 502, 503, 504, etc.) and they all mean something different. In this case, a 500 internal server error indicates that the server encountered an unexpected condition that prevented it from fulfilling the request (RFC 7231, section 6.6.1).

Php mysql http error 500
500 internal server error in WordPress

500 Internal Server Error Variations

Due to the various web servers, operating systems, and browsers, a 500 internal server error can present itself in a number of different ways. But they are all communicating the same thing. Below are just a couple of the many different variations you might see on the web:

    • “500 Internal Server Error”
    • “HTTP 500”
    • “Internal Server Error”
    • “HTTP 500 – Internal Server Error”
    • “500 Error”
    • “HTTP Error 500”
    • “500 – Internal Server Error”
    • “500 Internal Server Error. Sorry something went wrong.”
    • “500. That’s an error. There was an error. Please try again later. That’s all we know.”
    • “The website cannot display the page – HTTP 500.”
    • “Is currently unable to handle this request. HTTP ERROR 500.”

You might also see this message accompanying it:

The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log.

Php mysql http error 500
Internal Server Error

Other times, you might simply see a blank white screen. When dealing with 500 internal server errors, this is actually quite common in browsers like Firefox and Safari.

Php mysql http error 500
500 internal server error in Firefox

Bigger brands might even have their own custom 500 internal server error messages, such as this one from Airbnb.

Php mysql http error 500
Airbnb 500 internal server error

Here is another creative 500 server error example from the folks over at readme.

Php mysql http error 500
readme 500 internal server error

Even the mighty YouTube isn’t safe from 500 internal server errors.

Php mysql http error 500
500 internal server error on YouTube

If it’s an IIS 7.0 (Windows) or higher server, they have additional HTTP status codes to more closely indicate the cause of the 500 error:

  • 500.0 – Module or ISAPI error occurred.
  • 500.11 – Application is shutting down on the web server.
  • 500.12 – Application is busy restarting on the web server.
  • 500.13 – Web server is too busy.
  • 500.15 – Direct requests for global.asax are not allowed.
  • 500.19 – Configuration data is invalid.
  • 500.21 – Module not recognized.
  • 500.22 – An ASP.NET httpModules configuration does not apply in Managed Pipeline mode.
  • 500.23 – An ASP.NET httpHandlers configuration does not apply in Managed Pipeline mode.
  • 500.24 – An ASP.NET impersonation configuration does not apply in Managed Pipeline mode.
  • 500.50 – A rewrite error occurred during RQ_BEGIN_REQUEST notification handling. A configuration or inbound rule execution error occurred.
  • 500.51 – A rewrite error occurred during GL_PRE_BEGIN_REQUEST notification handling. A global configuration or global rule execution error occurred.
  • 500.52 – A rewrite error occurred during RQ_SEND_RESPONSE notification handling. An outbound rule execution occurred.
  • 500.53 – A rewrite error occurred during RQ_RELEASE_REQUEST_STATE notification handling. An outbound rule execution error occurred. The rule is configured to be executed before the output user cache gets updated.
    500.100 – Internal ASP error.

500 Errors Impact on SEO

Unlike 503 errors, which are used for WordPress maintenance mode and tell Google to check back at a later time, a 500 error can have a negative impact on SEO if not fixed right away. If your site is only down for say 10 minutes and it’s being crawled consistently a lot of times the crawler will simply get the page delivered from cache. Or Google might not even have a chance to re-crawl it before it’s back up. In this scenario, you’re completely fine.

However, if the site is down for an extended period of time, say 6+ hours, then Google might see the 500 error as a site level issue that needs to be addressed. This could impact your rankings. If you’re worried about repeat 500 errors you should figure out why they are happening to begin with. Some of the solutions below can help.

How to Fix the 500 Internal Server Error

Where should you start troubleshooting when you see a 500 internal server error on your WordPress site? Sometimes you might not even know where to begin. Typically 500 errors are on the server itself, but from our experience, these errors originate from two things, the first is user error (client-side issue), and the second is that there is a problem with the server. So we’ll dive into a little of both.

This is never not annoying 😖 pic.twitter.com/pPKxbkvI9K

— Dare Obasanjo (@Carnage4Life) September 26, 2019

Check out these common causes and ways to fix the 500 internal server error and get back up and running in no time.

1. Try Reloading the Page

This might seem a little obvious to some, but one of the easiest and first things you should try when encountering a 500 internal server error is to simply wait a minute or so and reload the page (F5 or Ctrl + F5). It could be that the host or server is simply overloaded and the site will come right back. While you’re waiting, you could also quickly try a different browser to rule that out as an issue.

Another thing you can do is to paste the website into downforeveryoneorjustme.com. This website will tell you if the site is down or if it’s a problem on your side. A tool like this checks the HTTP status code that is returned from the server. If it’s anything other than a 200 “Everything is OK” then it will return a down indication.

Php mysql http error 500
downforeveryoneorjustme

We’ve also noticed that sometimes this can occur immediately after you update a plugin or theme on your WordPress site. Typically this is on hosts that aren’t set up properly. What happens is they experience a temporary timeout right afterward. However, things usually resolve themselves in a couple of seconds and therefore refreshing is all you need to do.

2. Clear Your Browser Cache

Clearing your browser cache is always another good troubleshooting step before diving into deeper debugging on your site. Below are instructions on how to clear cache in the various browsers:

  • How to Force Refresh a Single Page for All Browsers
  • How to Clear Browser Cache for Google Chrome
  • How to Clear Browser Cache for Mozilla Firefox
  • How to Clear Browser Cache for Safari
  • How to Clear Browser Cache for Internet Explorer
  • How to Clear Browser Cache for Microsoft Edge
  • How to Clear Browser Cache for Opera

3. Check Your Server Logs

You should also take advantage of your error logs. If you’re a Kinsta client, you can easily see errors in the log viewer in the MyKinsta dashboard. This can help you quickly narrow down the issue, especially if it’s resulting from a plugin on your site.

Php mysql http error 500
Check error logs for 500 internal server errors

If your host doesn’t have a logging tool, you can also enable WordPress debugging mode by adding the following code to your wp-config.php file to enable logging:

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );

The logs are typically located in the /wp-content directory. Others, like here at Kinsta might have a dedicated folder called “logs”.

Php mysql http error 500
WordPress error logs folder (SFTP)

You can also check the log files in Apache and Nginx, which are commonly located here:

  • Apache: /var/log/apache2/error.log
  • Nginx: /var/log/nginx/error.log

If you’re a Kinsta client you can also take advantage of our analytics tool to get a breakdown of the total number of 500 errors and see how often and when they are occurring. This can help you troubleshoot if this is an ongoing issue, or perhaps something that has resolved itself.

Php mysql http error 500
Response analysis 500 error breakdown

If the 500 error is displaying because of a fatal PHP error, you can also try enabling PHP error reporting. Simply add the following code to the file throwing the error. Typically you can narrow down the file in the console tab of Google Chrome DevTools.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

And you might need to also modify your php.ini file with the following:

display_errors = on

4. Error Establishing a Database Connection

500 internal server errors can also occur from a database connection error. Depending upon your browser you might see different errors. But both will generate a 500 HTTP status code regardless in your server logs.

Below is an example of what an “error establishing a database connection” message looks like your browser. The entire page is blank because no data can be retrieved to render the page, as the connection is not working properly. Not only does this break the front-end of your site, but it will also prevent you from accessing your WordPress dashboard.

Php mysql http error 500
Example of error establishing a database connection

So why exactly does this happen? Well, here are a few common reasons below.

  • The most common issue is that your database login credentials are incorrect. Your WordPress site uses separate login information to connect to its MySQL database.
  • Your WordPress database is corrupted. With so many moving parts with themes, plugins, and users constantly deleting and installing them, sometimes databases get corrupted. This can be due to a missing or individually corrupted table, or perhaps some information was deleted by accident.
  • You may have corrupt files in your WordPress installation. This can even happen sometimes due to hackers.
  • Issues with your database server. A number of things could be wrong on the web hosts end, such as the database being overloaded from a traffic spike or unresponsive from too many concurrent connections. This is actually quite common with shared hosts as they are utilizing the same resources for a lot of users on the same servers.

Check out our in-depth post on how to fix the error establishing a database connection in WordPress.

5. Check Your Plugins and Themes

Third-party plugins and themes can easily cause 500 internal server errors. We’ve seen all types cause them here at Kinsta, from slider plugins to ad rotator plugins. A lot of times you should see the error immediately after installing something new or running an update. This is one reason why we always recommend utilizing a staging environment for updates or at least running updates one by one. Otherwise, if you encounter a 500 internal server error you’re suddenly scrambling to figure out which one caused it.

A few ways you can troubleshoot this is by deactivating all your plugins. Remember, you won’t lose any data if you simply deactivate a plugin. If you can still access your admin, a quick way to do this is to browse to “Plugins” and select “Deactivate” from the bulk actions menu. This will disable all of your plugins.

Php mysql http error 500
Deactivate all plugins

If this fixes the issue you’ll need to find the culprit. Start activating them one by one, reloading the site after each activation. When you see the 500 internal server error return, you’ve found the misbehaving plugin. You can then reach out to the plugin developer for help or post a support ticket in the WordPress repository.

If you can’t login to WordPress admin you can FTP into your server and rename your plugins folder to something like plugins_old. Then check your site again. If it works, then you will need to test each plugin one by one. Rename your plugin folder back to “plugins” and then rename each plugin folder inside of if it, one by one, until you find it. You could also try to replicate this on a staging site first.

Php mysql http error 500
Rename plugin folder

Always makes sure your plugins, themes, and WordPress core are up to date. And check to ensure you are running a supported version of PHP. If it turns out to be a conflict with bad code in a plugin, you might need to bring in a WordPress developer to fix the issue.

6. Reinstall WordPress Core

Sometimes WordPress core files can get corrupted, especially on older sites. It’s actually quite easy to re-upload just the core of WordPress without impacting your plugins or themes. We have an in-depth guide with 5 different ways to reinstall WordPress. And of course, make sure to take a backup before proceeding. Skip to one of the sections below:

  • How to reinstall WordPress from the WordPress dashboard while preserving existing content
  • How to manually reinstall WordPress via FTP while preserving existing content
  • How to manually reinstall WordPress via WP-CLI while preserving existing content

7. Permissions Error

A permissions error with a file or folder on your server can also cause a 500 internal server error to occur. Here are some typical recommendations for permissions when it comes to file and folder permissions in WordPress:

  • All files should be 644 (-rw-r–r–) or 640.
  • All directories should be 755 (drwxr-xr-x) or 750.
  • No directories should ever be given 777, even upload directories.
  • Hardening: wp-config.php could also be set to 440 or 400 to prevent other users on the server from reading it.

See the WordPress Codex article on changing file permissions for a more in-depth explanation.

You can easily see your file permissions with an FTP client (as seen below). You could also reach out to your WordPress host support team and ask them to quickly GREP file permissions on your folders and files to ensure they’re setup properly.

Php mysql http error 500
File permissions SFTP

8. PHP Memory Limit

A 500 internal server error could also be caused by exhausting the PHP memory limit on your server. You could try increasing the limit. Follow the instructions below on how to change this limit in cPanel, Apache, your php.ini file, and wp-config.php file.

Increase PHP Memory Limit in cPanel

If you’re running on a host that uses cPanel, you can easily change this from the UI. Under Software click on “Select PHP Version.”

Php mysql http error 500
Select PHP version

Click on “Switch to PHP Options.”

Php mysql http error 500
Switch to PHP options

You can then click on the memory_limit attribute and change its value. Then click on “Save.”

Php mysql http error 500
Increase PHP memory limit in cPanel

Increase PHP Memory Limit in Apache

The .htaccess file is a special hidden file that contains various settings you can use to modify the server behavior, right down to a directory specific level. First login to your site via FTP or SSH, take a look at your root directory and see if there is a .htaccess file there.

Php mysql http error 500
.htaccess file

If there is you can edit that file to add the necessary code for increasing the PHP memory limit. Most likely it is set at 64M or below, you can try increasing this value.

php_value memory_limit 128M

Increase PHP Memory Limit in php.ini File

If the above doesn’t work for you might try editing your php.ini file. Log in to your site via FTP or SSH, go to your site’s root directory and open or create a php.ini file.

Php mysql http error 500
php.ini file

If the file was already there, search for the three settings and modify them if necessary. If you just created the file, or the settings are nowhere to be found you can paste the code below. You can modify of course the values to meet your needs.

memory_limit = 128M

Some shared hosts might also require that you add the suPHP directive in your .htaccess file for the above php.ini file settings to work. To do this, edit your .htaccess file, also located at the root of your site, and add the following code towards the top of the file:

 
suPHP_ConfigPath /home/yourusername/public_html

If the above didn’t work for you, it could be that your host has the global settings locked down and instead have it configured to utilize .user.ini files. To edit your .user.ini file, login to your site via FTP or SSH, go to your site’s root directory and open or create a .user.ini file. You can then paste in the following code:

memory_limit = 128M

Increase PHP Memory Limit in wp-config.php

The last option is not one we are fans of, but if all else fails you can give it a go. First, log in to your site via FTP or SSH, and locate your wp-config.php file, which is typically in the root of your site.

Php mysql http error 500
wp-config.php file

Add the following code to the top of your wp-config.php file:

define('WP_MEMORY_LIMIT', '128M');

You can also ask your host if you’re running into memory limit issues. We utilize the Kinsta APM tool and other troubleshooting methods here at Kinsta to help clients narrow down what plugin, query, or script might be exhausting the limit. You can also use your own custom New Relic key from your own license.

Php mysql http error 500
Debugging with New Relic

9. Problem With Your .htaccess File

Kinsta only uses Nginx, but if you’re using a WordPress host that is running Apache, it could very well be that your .htaccess file has a problem or has become corrupted. Follow the steps below to recreate a new one from scratch.

First, log in to your site via FTP or SSH, and rename your .htaccess file to .htaccess_old.

Php mysql http error 500
Rename .htaccess file

Normally to recreate this file you can simply re-save your permalinks in WordPress. However, if you’re in the middle of a 500 internal server error you most likely can’t access your WordPress admin, so this isn’t an option. Therefore you can create a new .htaccess file and input the following contents. Then upload it to your server.

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

See the WordPress Codex for more examples, such as a default .htaccess file for multisite.

10. Coding or Syntax Errors in Your CGI/Perl Script

500 errors being caused by errors in CGI and Perl is a lot less common than it used to be. Although it’s still worth mentioning, especially for those using cPanel where there are a lot of one-click CGI scripts still being used. As AEM on Stack Overflow says:

CGI has been replaced by a vast variety of web programming technologies, including PHP, various Apache extensions like mod_perl, Java of various flavors and frameworks including Java EE, Struts, Spring, etc, Python-based frameworks like Django, Ruby on Rails and many other Ruby frameworks, and various Microsoft technologies.

Here are a few tips when working with CGI scripts:

  • When editing, always used a plain text editor, such as Atom, Sublime, or Notepad++. This ensures they remain in ASCII format.
  • Ensure correct permissions of chmod 755 are used on CGI scripts and directories.
  • Upload your CGI scripts in ASCII mode (which you can select in your FTP editor) into the cgi-bin directory on your server.
  • Confirm that the Perl modules you require for your script are installed and supported.

11. Server Issue (Check With Your Host)

Finally, because 500 internal server errors can also occur from PHP timing out or fatal PHP errors with third-party plugins, you can always check with your WordPress host. Sometimes these errors can be difficult to troubleshoot without an expert. Here are just a few common examples of some errors that trigger 500 HTTP status codes on the server that might have you scratching your head.

PHP message: PHP Fatal error: Uncaught Error: Call to undefined function mysql_error()...
PHP message: PHP Fatal error: Uncaught Error: Cannot use object of type WP_Error as array in /www/folder/web/shared/content/plugins/plugin/functions.php:525

We monitor all client’s sites here at Kinsta and are automatically notified when these types of errors occur. This allows us to be pro-active and start fixing the issue right away. We also utilize LXD managed hosts and orchestrated LXC software containers for each site. This means that every WordPress site is housed in its own isolated container, which has all of the software resources required to run it (Linux, Nginx, PHP, MySQL). The resources are 100% private and are not shared with anyone else or even your own sites.

PHP timeouts could also occur from the lack of PHP workers, although typically these cause 504 errors, not 500 errors. These determine how many simultaneous requests your site can handle at a given time. To put it simply, each uncached request for your website is handled by a PHP Worker.

When PHP workers are already busy on a site, they start to build up a queue. Once you’ve reached your limit of PHP workers, the queue starts to push out older requests which could result in 500 errors or incomplete requests. Read our in-depth article about PHP workers.

Monitor Your Site

If you’re worried about these types of errors happening on your site in the future, you can also utilize a tool like updown.io to monitor and notify you immediately if they occur. It periodically sends an HTTP HEAD request to the URL of your choice. You can simply use your homepage. The tool allows you to set check frequencies of:

  • 15 seconds
  • 30 seconds
  • 1 minute
  • 2 minutes
  • 5 minutes
  • 10 minutes

It will send you an email if and when your site goes down. Here is an example below.

Php mysql http error 500
Email notification of 500 error

This can be especially useful if you’re trying to debug a faulty plugin or are on a shared host, who tend to overcrowd their servers. This can give you proof of how often your site might actually be doing down (even during the middle of the night). That’s why we always recommend going with a managed WordPress host. Make sure to check out our post that explores the top 9 reasons to choose managed WordPress hosting.

Summary

500 internal server errors are always frustrating, but hopefully, now you know a few additional ways to troubleshoot them to quickly get your site back up and running. Remember, typically these types of errors are caused by third-party plugins, fatal PHP errors, database connection issues, problems with your .htaccess file or PHP memory limits, and sometimes PHP timeouts.

Was there anything we missed? Perhaps you have another tip on troubleshooting 500 internal server errors. If so, let us know below in the comments.


Save time, costs and maximize site performance with:

  • Instant help from WordPress hosting experts, 24/7.
  • Cloudflare Enterprise integration.
  • Global audience reach with 35 data centers worldwide.
  • Optimization with our built-in Application Performance Monitoring.

All of that and much more, in one plan with no long-term contracts, assisted migrations, and a 30-day-money-back-guarantee. Check out our plans or talk to sales to find the plan that’s right for you.

How do I fix a HTTP 500 error?

How to Fix the 500 Internal Server Error.
Reload the web page. ... .
Clear your browser's cache. ... .
Delete your browser's cookies. ... .
Troubleshoot as a 504 Gateway Timeout error instead. ... .
Contacting the website is another option. ... .
Come back later..

What causes 500 errors PHP?

If your PHP script makes external network connections, the connections may time out. If too many connections are attempted and time out, this will cause a "500 Internal Server Error." To prevent these time outs and errors, you'll want to make sure that PHP scripts be coded with some timeout rules.

Can T currently handle this request HTTP error 500 PHP?

The HTTP 500 message states that the server can't handle the request because of an unexpected condition. The problem may lie in browser cache, third-party themes and plugins, or PHP memory limit. It could also be due to a broken . htaccess file.

What is the error HTTP Status 500?

The HTTP status code 500 is a generic error response. It means that the server encountered an unexpected condition that prevented it from fulfilling the request. This error is usually returned by the server when no other error code is suitable.