0

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.

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Cd_Codes
  • 13
  • 3

2 Answers2

0

@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());
}
  • actually you could do `$con = mysqli_connect("localhost:3308","sotomango", "1234",'mysql');` – Cd_Codes Mar 12 '20 at 13:36
  • Yes I could and then your comon sence or dharman would say no dlk :) glad I helped. –  Mar 12 '20 at 13:37
0

For The People Wondering

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

Cd_Codes
  • 13
  • 3
  • Because the ***default*** MySQL Port is `3306`, your port of `3308` is **REQUIRED** because it is not the default. This is why this is your issue and this should be outlined for others reading this question. Glad you found the answer. – Martin Mar 12 '20 at 13:38