1

I am trying to create a database for a PHP project using MySQL in phpmyadmin and also using MAMP on windows 10 and after I create the database and create a new user and try to check the database connection..it shows the below error..

Database connection failed: Can't connect to MySQL server on 'localhost' (10061) (2003)

I did check relevant questions in the forums and tried all the tricks there but did not work..

I am mostly a Wordpress developer and I am able to create a new database in wordpress like before but PHP is giving me issues..

Please check my codes..

config.php

 <?php

    // Database Constants
    defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
    defined('DB_USER')   ? null : define("DB_USER", "pop234");
    defined('DB_PASS')   ? null : define("DB_PASS", "4JfQuuQnzU6yfPdm");
    defined('DB_NAME')   ? null : define("DB_NAME", "demo");

    ?>

database.php

<?php

    require_once ('config.php');

    class MYSQLDatabase {

        private $connection;

      function __construct() {
        $this->open_connection();
      }

      public function open_connection() {
        $this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
        if(mysqli_connect_errno()) {
          die("Database connection failed: " . 
               mysqli_connect_error() . 
               " (" . mysqli_connect_errno() . ")"
          );
        }
      }
    }

    $database = new MySQLDatabase();
    $db =& $database;

    ?>

index.php

<?php

require_once('../includes/database.php');

if(isset($database)) { echo "true"; } else { echo "false"; }
echo "<br />";

?>

I have also tried using 127.0.0.1:8889 instead of localhost like i do in my WP programs if I get a database error..

Please assist and advise

Thanking you in anticipation..

<?php

// PHP Database Constants
defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
defined('DB_USER')   ? null : define("DB_USER", "popat234");
defined('DB_PASS')   ? null : define("DB_PASS", "4JfQuuQnzU6yfPdm");
defined('DB_NAME')   ? null : define("DB_NAME", "demo");

// Wordprsss Database Constants
/** The name of the database for WordPress */
define('DB_NAME', 'demo');

/** MySQL database username */
define('DB_USER', 'popat234');

/** MySQL database password */
define('DB_PASSWORD', '4JfQuuQnzU6yfPdm');

/** MySQL hostname */
define('DB_HOST', '127.0.0.1:8889');

?>
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Dhaval Chheda
  • 4,637
  • 5
  • 24
  • 44

2 Answers2

1

The DB_USER is different in the two configurations. config.php shows 'pop234' and wp-config.php uses 'popat234' which is apparently the correct one.

Make sure that your config.php contains:

 <?php

    // Database Constants
    defined('DB_SERVER') ? null : define("DB_SERVER", "127.0.0.1");
    defined('DB_PORT') ? null : define("DB_PORT", "8889");
    defined('DB_USER')   ? null : define("DB_USER", "popat234");
    defined('DB_PASS')   ? null : define("DB_PASS", "4JfQuuQnzU6yfPdm");
    defined('DB_NAME')   ? null : define("DB_NAME", "demo");

Also correct your mysqli_connect() line to:

    $this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME, DB_PORT);
Julie Pelletier
  • 1,740
  • 1
  • 10
  • 18
0

try to switch your port from 8889 to 3306, this resolve my problem. This maybe can occurr because the Windows block the port 8889.