MYSQLI on PHPStorm Build In Web Server

PHPStorm has a build-in web server running at default port 63342.  It works fine in general browser files such as HTML. however, I did run into a problem connecting with database with MYSQLI using the build-in web server.  Code like:

$servername = "127.0.0.1:63342";
$username = "someuser";
$password = "whatever";

// Create connection
$conn = new mysqli($servername, $username, $password, "db_manager_name", 3306);

will result in unknown host error.

Here’s how I get around it:

    • I had something that was running on port 80, the default port for Apache.  So first, I had to stop it.
    • As of PHP 5.4.0, the CLI SAPI provides a built-in web server. Here the link to the documentation. I start a new server in the shell:
      php -S localhost:80 -t root_document_directory/
    • Then I go back to my connection code then set server name to “127.0.0.1”.  Note, “localhost” wouldn’t work.
    • MYSQLI connected with no problem.

    BTW, an example of the url for the build-in web server looks like the following:

    http://localhost:63342/general/index.html

    Note: accessing PHP files on it will result in a 502 error. Use the SAPI build-in web server will solve this problem.