PHP file wont connect to the sql database, it says error #1045 but the values on the code are right [duplicate]

丶灬走出姿态 提交于 2020-04-03 08:43:21

问题


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

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

Credentials in the database:

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.


回答1:


@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());
}



回答2:


For The People Wondering

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



来源:https://stackoverflow.com/questions/60654151/php-file-wont-connect-to-the-sql-database-it-says-error-1045-but-the-values-on

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!