Search code examples
mysqlxamppmysql-select-db

Xampp not connecting to mysql databse error please check the database name


I attached index.php file. I get error on line 37 as:

"Database connection error: Please check the database name you provided".

I checked database name its correct and tried 2 different database names but I get same error. Can someone tell me whats wrong in this code?

This is my index.php file.

if (( isset( $_POST ) && $_POST['submit'] == 'submit' )) {
    $SS_license = trim( $_POST['license'] );
    $db_host = trim( $_POST['host'] );
    $db_name = trim( $_POST['database_name'] );
    $db_pass = trim( $_POST['database_password'] );
    $db_user = trim( $_POST['database_user'] );
    $flag = 0;

    if (( 'Database connection error: Please check the host name, user name and password you provided' || $con = mysql_connect( $db_host, $db_user, $db_pass ) )) {
        if (mysql_select_db( $db_name, $con )) {
            $message .= 'Successfully connected to database<br>';
            $flag = 1;
        }
        else {
            $message .= 'Database connection error: Please check the database name you provided<br>';

        }
    }
    else {
        $message .= 'Database connection error: Please check the host name, user name and password you provided<br>';
    }


    if ($flag == 1) {
        $fp = fopen( BaseUrl . DS . 'includes' . DS . 'db_inc.php', 'w' );
        $string = '<?php';

please tell me where to edit this code so I can connect to databse.


Solution

  • you should try using below code, this works fine for me.


    <?php    $servername = "localhost";$username = "your_username";$password = "your_password";$dbname = "your_DBname"; $conn = mysqli_connect($servername, $username, $password, $dbname);if (!$conn) {die("Connection failed: " . mysqli_connect_error()); } ?>