OussamaMater
OussamaMater

Reputation: 127

PHP, Failed connecting to database

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

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

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;
}

Upvotes: 0

Views: 866

Answers (1)

user1805543
user1805543

Reputation:

Wampserver 3.2.0 new instalation or upgrading

This might help others

Probably xamp using mariaDB as default too.

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

To make mysql work!

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

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

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 : I am using pdo.

See here for more : https://sourceforge.net/projects/wampserver/

Upvotes: 3

Related Questions