PHP,连接数据库失败 [英] PHP, Failed connecting to database

查看:100
本文介绍了PHP,连接数据库失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试连接到数据库时,我都无法连接到数据库

I can't connect to my databases, whenever i try it's giving me this error

我尝试连接到默认数据库(例如mysql),并且效果很好,就像图片所示

I tried to connect to the default databases like mysql, and it worked fine just like the pic shows

我正在使用最新版本的wamp服务器,如果需要,请使用以下代码:

I'm using wamp server the latest version, here's code if needed :

$servername = "localhost";
$username = "root";
$password = "";
$my_db="mydb";

$link=mysqli_connect($servername, $username, $password, $my_db);
if (mysqli_connect_error()) {
    die("there is an error");
} else {
    echo "connected to ".$my_db;
}

推荐答案

Wampserver 3.2.0的新安装或升级

这可能会对其他人有所帮助

可能xamp也使用mariaDB作为默认值.

Probably xamp using mariaDB as default too.

Wamp服务器随附mariaDB和mysql,并在3306端口上默认安装mariaDB.

Wamp server comes with mariaDB and mysql, and instaling mariaDB as default on 3306 port.

要使mysql正常工作!

在安装时,它要求使用mariaDBMySql,mariaDB被默认选中,您无法更改它,请选中mysql选项并安装.

On instalation it asks to use mariaDB or MySql, mariaDB is checked as default and you cant change it, check mysql option and install.

安装完成后,两个都将在默认端口上运行mariaDB,在另一个端口上运行mysql.

when instalation done both will be runing mariaDB on default port and mysql on another port.

右键单击wamp图标,其运行应在右下角,转到工具,然后查看您的mysql运行端口.

Right click on wamp icon where its runing should be on right bottom corner, goto tools and see your mysql runing port.

并在数据库连接中包括以下内容:

And include in your database connection same as folowng :

$host = 'localhost';
$db   = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';
$port = '3308';

$dsn = "mysql:host=$host;dbname=$db;port=$port;charset=$charset";
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
try {
     $pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
}

Note :我正在使用pdo.

查看更多信息: https://sourceforge.net/projects/wampserver/

这篇关于PHP,连接数据库失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆