skip to Main Content

I tried connecting my PHP file to MySQli database but when i ran the code it displays enter image description here

But when i logon to phpmyadmin it works fine no errors i can login to my account with no problems

Credentials in the database:
enter image description here

Connection Code:

<?php

$con = mysqli_connect('localhost','sotomango', '1234', 'mysql');

    if(!$con) {

        echo 'noooo';

    }

?>

I double checked the database usernames, passwords, and privileges. Everything seems in place and correct, but the php file wont allow me to connect to the database.

2

Answers


  1. Chosen as BEST ANSWER

    For The People Wondering

    I solved this by just adding a port to the host name like localhost:3308


  2. @Dharman had a nice post but I couldnt find it.

    Try this for debug :

        $con = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
    
        if (!$con) {
            echo "Error: Unable to connect to MySQL." . PHP_EOL;
            echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
            echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
            exit;
        }
      echo "Success: A proper connection to MySQL was made! The my_db database is 
      great." . PHP_EOL;
      echo "Host information: " . mysqli_get_host_info($con) . PHP_EOL;
    
    mysqli_close($con);
    

    mysqli_connect

    if its a new instalation or new upgrade, You need to include your port into connection, mariadb comes as default port so, when you try to connect mysql its redirecting to mariadb.

    you need to include your correct port into connection it might be 3306, 3307 or 3308, and use ip instead of localhost.

    your final codes should look like this :

    $host = '127.0.0.1';
    $db   = 'test';
    $user = 'root';
    $pass = '';
    $port = '3308';
    $charset = 'utf8mb4';
    
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    try {
        $mysqli = mysqli_connect($host, $user, $pass, $port, $db);
        mysqli_set_charset($mysqli, $charset);
    } catch (mysqli_sql_exception $e) {
         throw new mysqli_sql_exception($e->getMessage(), $e->getCode());
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search