SQLSTATE[HY000] [1049] Unknown database 'datalib'
Solved
chabinot
Posted messages
391
Status
Membre
-
chabinot Posted messages 391 Status Membre -
chabinot Posted messages 391 Status Membre -
Hello,
I'm trying to connect to a database that indeed exists via PDO, but I'm getting the following error: 'SQLSTATE[HY000] [1049] Unknown database 'datalib''.
Here is my code:
I'm using a Database class, of which here is the code:
The database datalib does indeed exist. I must mention that I'm using MariaDB instead of MySQL.
Please let me know where I went wrong.
Best regards
I'm trying to connect to a database that indeed exists via PDO, but I'm getting the following error: 'SQLSTATE[HY000] [1049] Unknown database 'datalib''.
Here is my code:
<?php // Autoload require './vendor/autoload.php'; //include 'config.ini'; // Connection to the database $bdd = new Database("localhost", "datalib", "root", ""); var_dump($bdd); I'm using a Database class, of which here is the code:
<?php class Database { private $host; private $dbname; private $user; private $pwd; private $bdd; private $error; public function __construct($host, $dbname, $user, $pwd) { $this->host = $host; $this->dbname = $dbname; $this->user = $user; $this->pwd = $pwd; // Set [/contents/698-odbc-open-database-connectivity DSN] $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname; // Set options $options = [ PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' ]; // Create a new PDO instance try { $this->bdd = new PDO($dsn, $this->user, $this->pwd, $options); } // Capture errors catch (PDOException $e) { $this->error = $e->getMessage(); } } public function query($query) { $this->stmt = $this->dbh->prepare($query); } public function bind($param, $value, $type = null) { if (is_null($type)) { switch (true) { case is_int($value): $type = PDO::PARAM_INT; break; case is_bool($value): $type = PDO::PARAM_BOOL; break; case is_null($value): $type = PDO::PARAM_NULL; break; default: $type = PDO::PARAM_STR; } } $this->stmt->bindValue($param, $value, $type); } public function execute() { return $this->stmt->execute(); } public function result() { $this->execute(); return $this->stmt->fetchAll(PDO::FETCH_OBJ); } public function single() { $this->execute(); return $this->stmt->fetch(PDO::FETCH_OBJ); } public function rowCount() { return $this->stmt->rowCount(); } public function lastInsertId() { return $this->bdd->lastInsertId(); } public function beginTransaction() { return $this->bdd->beginTransaction(); } public function endTransaction() { return $this->bdd->commit(); } public function cancelTransaction() { return $this->bdd->rollBack(); } public function debugDumpParams() { return $this->stmt->debugDumpParams(); } } The database datalib does indeed exist. I must mention that I'm using MariaDB instead of MySQL.
Please let me know where I went wrong.
Best regards
3 réponses
Hello,
I found that, since I use MariaDB, I just needed to add the port 3306 to the DSN.
$dsn = 'mysql:host=' . $this->host . ';port=3306;dbname=' . $this->dbname;
Thank you all again.
Best regards
I found that, since I use MariaDB, I just needed to add the port 3306 to the DSN.
$dsn = 'mysql:host=' . $this->host . ';port=3306;dbname=' . $this->dbname;
Thank you all again.
Best regards
Hello,
Aside from the fact that you may have misspelled the name of your database.. I don’t see..
Could you do a SHOW DATABASE and paste the result here?
https://mariadb.com/kb/en/show-databases/
Make sure your files are also properly encoded in UTF8 WITHOUT BOM, to avoid hidden characters that could mess things up
By the way:
- rowCount => To be used ONLY for DELETE / INSERT / UPDATE queries but never for SELECT queries (instead you can do a COUNT on fetchAll)
- You should place the prepare and execute instructions in a TRY/CATCH block to catch any potential errors.
--
Best regards,
Jordane
Aside from the fact that you may have misspelled the name of your database.. I don’t see..
Could you do a SHOW DATABASE and paste the result here?
https://mariadb.com/kb/en/show-databases/
Make sure your files are also properly encoded in UTF8 WITHOUT BOM, to avoid hidden characters that could mess things up
By the way:
- rowCount => To be used ONLY for DELETE / INSERT / UPDATE queries but never for SELECT queries (instead you can do a COUNT on fetchAll)
- You should place the prepare and execute instructions in a TRY/CATCH block to catch any potential errors.
--
Best regards,
Jordane