richarddunnebsc Posted December 2, 2021 Share Posted December 2, 2021 The Database connection class Db (Config.php) class Db extends PDO { private $host = "localhost"; private $user = "root"; private $pass = ""; private $dbName = "Users"; private $charset = "utf8mb4"; public function __construct() { $dsn = "mysql:host={$this->host};dbName={$this->dbName};charset={$this->charset}"; parent::__construct($dsn, $this->user, $this->pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); } } When I try to do an insert in my Data class (Data.php) try { $pdo = new Db(); if ($_SERVER["REQUEST_METHOD"] == "POST") { $email = filter_var($_POST["Email"],FILTER_VALIDATE_EMAIL); } $sql = "Insert into Users (Email,Name) values(:Email,:Name)"; $stmt= $pdo->prepare($sql); $stmt->execute(array( ':Email' => $_POST["Email"],':Name' => $_POST["Name"] )); if($stmt) { return true; } } catch (PDOException $e) { die('Error: '.$e->getMessage()); } I get this error No Database selected. If I do an isset() on $pdo are creating the object variable it returns true, which would indicate a connection was successful. What am I missing here? Quote Link to comment https://forums.phpfreaks.com/topic/314263-error-sqlstate3d000-invalid-catalog-name-1046-no-database-selected/ Share on other sites More sharing options...
benanamen Posted December 2, 2021 Share Posted December 2, 2021 (edited) @gizmola and I both gave you code that you have not implemented. You should spend some time going through this PDO tutorial. Making a PDO connection is one of the simplest things you would ever need to do. https://phpdelusions.net/pdo This is all that is required to make a PDO connection. Anything you do beyond this, you should know exactly WHY you are doing more. $con = new PDO("mysql:host=localhost;dbname=test", 'root', ''); Edited December 2, 2021 by benanamen 2 Quote Link to comment https://forums.phpfreaks.com/topic/314263-error-sqlstate3d000-invalid-catalog-name-1046-no-database-selected/#findComment-1592376 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.