UNABLE TO CONNET TO MySQL DATABASE
Posted by: Chukwura (197.210.172.---)
Date: September 07, 2016 10:18AM

I am using:

Wampserer 3.0.4,

PHP 5.6.19,

Apache 2.4.18, Port 80,and

MySQL 5.7 11.

I was trying to open a connection to MySQL database using PDO with the following code but received this

error message: "Connection failed: SQLSTATE[HY000][1049] Unknown database 'myDB'. I have checked and

rechecked to find out my mistake to no avail, and should be grateful for any assistance to fish out the error

because I specified dbname here:

" $conn = new PDO("mysql:host=$servername;dbname=myDB", "root", "patony"winking smiley;"




<!DOCTYPE html>
<html lang="en-us">
<meta charset="utf-8">
<body>

<?php
$servername = "localhost";
$username = "root";
$password = "patony";


try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", "root", "patony"winking smiley;

//Set the PDO error mode to exception
$conn->Attribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>

</body>
</html>

Thanks

Chuks

Options: ReplyQuote
Re: UNABLE TO CONNET TO MySQL DATABASE
Posted by: RiggsFolly (Moderator)
Date: September 07, 2016 10:25AM

Hi

Have you created a database called myDB in MYSQL?

---------------------------------------------------------------------------------------------
(Windows 10 Pro 64bit) (Wampserver 3.3.4 64bit) Aestan Tray Menu 3.2.5.4
<Apache versions MULTIPE> <PHP versions MULTIPLE> <MySQL Versions MULTIPLE>
<MariaDB versions MULTIPLE> <phpMyAdmin versions MULTIPLE> <MySQL Workbench 8.0.23>

Read The Manuals Apache -- MySQL -- PHP -- phpMyAdmin
Get your Apache/MySQL/mariaDB/PHP ADDONs here from the WAMPServer alternate Repo
-X-X-X- Backup your databases regularly Here is How dont regret it later! Yes even when developing -X-X-X-

Options: ReplyQuote
Re: UNABLE TO CONNET TO MySQL DATABASE
Posted by: Chukwura (197.210.226.---)
Date: September 07, 2016 12:46PM

I thought that I have created the database called myDB in MySQL with "dbname=myDB" in the code.

Options: ReplyQuote
Re: UNABLE TO CONNET TO MySQL DATABASE
Posted by: RiggsFolly (Moderator)
Date: September 07, 2016 01:03PM

No, you have not. That is a request to connect to a database that shoudl already exist in MYSQL

---------------------------------------------------------------------------------------------
(Windows 10 Pro 64bit) (Wampserver 3.3.4 64bit) Aestan Tray Menu 3.2.5.4
<Apache versions MULTIPE> <PHP versions MULTIPLE> <MySQL Versions MULTIPLE>
<MariaDB versions MULTIPLE> <phpMyAdmin versions MULTIPLE> <MySQL Workbench 8.0.23>

Read The Manuals Apache -- MySQL -- PHP -- phpMyAdmin
Get your Apache/MySQL/mariaDB/PHP ADDONs here from the WAMPServer alternate Repo
-X-X-X- Backup your databases regularly Here is How dont regret it later! Yes even when developing -X-X-X-

Options: ReplyQuote


Sorry, only registered users may post in this forum.