PDO DB connection












0















I need some help with my DB connection.



I've got my config.php:



namespace AppCore;


class config
{
private $db;
private $host;
private $user;
private $password;
private $charset;
private $opt;
private $dsn;

public function __construct()
{
$this->db = 'example';
$this->host = '127.0.0.1';
$this->user = 'root';
$this->password = '';
$this->charset = 'utf8';
$this->dsn = "mysql:host=$this->host;DBName=$this->db;charset=$this->charset;";

$this->opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false
);
}

// getters
public function db()
{
return $this->db;
}

public function host()
{
return $this->host;
}

public function user()
{
return $this->user;
}

public function password()
{
return $this->password;
}

public function charset()
{
return $this->charset;
}

public function options()
{
return $this->opt;
}

public function dsn()
{
return $this->dsn;
}
}


I chose to use a config class.



Then I do also have a DBHandler class. This is where I make my PDO connection, and probally where it goes wrong.



public function __construct()
{
$this->config = new config();

try {
$this->PDO = new PDO($this->config->dsn(), $this->config->user(), $this->config->password(), $this->config->options());
} catch (PDOException $e) {
throw new PDOException($e->getMessage(), (int)$e->getCode());
}
}

public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new DBHandler();
}
return self::$instance;
}

public function connect()
{
return $this->PDO;
}


Then I also have a DBHelper what is basicly my CRUD class, here I write all my queries and this is the file I call in all my normal PHP files like "index.php".



Whenever I try to run a query I get an error, so I var_dumped my $db and got this error out of it:




Fatal error: Uncaught PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in D:AppCoreDBHelper.php:28 Stack trace: #0 D:AppCoreDBHelper.php(28): PDO->prepare('SELECT * FROM u...') #1 D:AppCoreDBHelper.php(55): AppCoreDBHelper->userQuery('SELECT * FROM u...') #2 D:Appindex.php(28): AppCoreDBHelper->select('users', '*', 'userName', NULL) #3 {main} thrown in D:AppCoreDBHelper.php on line 28




Can anyone tell me where it goes wrong?










share|improve this question

























  • Off topic: normally you would call getters more like ... getDb(), getDatabase() talking about the config class or if you use setters setDd(...), setDatabase(...) they you directly can see the function of the function

    – Raymond Nijland
    Nov 26 '18 at 20:22








  • 1





    The dsn DBName=$this->db try it with lowercase dbname=$this->db not sure if it matters

    – Raymond Nijland
    Nov 26 '18 at 20:25






  • 1





    @Raymond, you are right, DBName causes Invalid catalog name: 1046 No database selected. When the database does not exist, then I get error 1049.

    – digijay
    Nov 26 '18 at 21:34











  • @RaymondNijland Thanks for the help I do connect with the database now. Just have an SQL error.

    – yorrid08
    Nov 27 '18 at 9:37











  • It should be dbname, not DBName. Doc.

    – yivi
    Nov 27 '18 at 12:04
















0















I need some help with my DB connection.



I've got my config.php:



namespace AppCore;


class config
{
private $db;
private $host;
private $user;
private $password;
private $charset;
private $opt;
private $dsn;

public function __construct()
{
$this->db = 'example';
$this->host = '127.0.0.1';
$this->user = 'root';
$this->password = '';
$this->charset = 'utf8';
$this->dsn = "mysql:host=$this->host;DBName=$this->db;charset=$this->charset;";

$this->opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false
);
}

// getters
public function db()
{
return $this->db;
}

public function host()
{
return $this->host;
}

public function user()
{
return $this->user;
}

public function password()
{
return $this->password;
}

public function charset()
{
return $this->charset;
}

public function options()
{
return $this->opt;
}

public function dsn()
{
return $this->dsn;
}
}


I chose to use a config class.



Then I do also have a DBHandler class. This is where I make my PDO connection, and probally where it goes wrong.



public function __construct()
{
$this->config = new config();

try {
$this->PDO = new PDO($this->config->dsn(), $this->config->user(), $this->config->password(), $this->config->options());
} catch (PDOException $e) {
throw new PDOException($e->getMessage(), (int)$e->getCode());
}
}

public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new DBHandler();
}
return self::$instance;
}

public function connect()
{
return $this->PDO;
}


Then I also have a DBHelper what is basicly my CRUD class, here I write all my queries and this is the file I call in all my normal PHP files like "index.php".



Whenever I try to run a query I get an error, so I var_dumped my $db and got this error out of it:




Fatal error: Uncaught PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in D:AppCoreDBHelper.php:28 Stack trace: #0 D:AppCoreDBHelper.php(28): PDO->prepare('SELECT * FROM u...') #1 D:AppCoreDBHelper.php(55): AppCoreDBHelper->userQuery('SELECT * FROM u...') #2 D:Appindex.php(28): AppCoreDBHelper->select('users', '*', 'userName', NULL) #3 {main} thrown in D:AppCoreDBHelper.php on line 28




Can anyone tell me where it goes wrong?










share|improve this question

























  • Off topic: normally you would call getters more like ... getDb(), getDatabase() talking about the config class or if you use setters setDd(...), setDatabase(...) they you directly can see the function of the function

    – Raymond Nijland
    Nov 26 '18 at 20:22








  • 1





    The dsn DBName=$this->db try it with lowercase dbname=$this->db not sure if it matters

    – Raymond Nijland
    Nov 26 '18 at 20:25






  • 1





    @Raymond, you are right, DBName causes Invalid catalog name: 1046 No database selected. When the database does not exist, then I get error 1049.

    – digijay
    Nov 26 '18 at 21:34











  • @RaymondNijland Thanks for the help I do connect with the database now. Just have an SQL error.

    – yorrid08
    Nov 27 '18 at 9:37











  • It should be dbname, not DBName. Doc.

    – yivi
    Nov 27 '18 at 12:04














0












0








0








I need some help with my DB connection.



I've got my config.php:



namespace AppCore;


class config
{
private $db;
private $host;
private $user;
private $password;
private $charset;
private $opt;
private $dsn;

public function __construct()
{
$this->db = 'example';
$this->host = '127.0.0.1';
$this->user = 'root';
$this->password = '';
$this->charset = 'utf8';
$this->dsn = "mysql:host=$this->host;DBName=$this->db;charset=$this->charset;";

$this->opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false
);
}

// getters
public function db()
{
return $this->db;
}

public function host()
{
return $this->host;
}

public function user()
{
return $this->user;
}

public function password()
{
return $this->password;
}

public function charset()
{
return $this->charset;
}

public function options()
{
return $this->opt;
}

public function dsn()
{
return $this->dsn;
}
}


I chose to use a config class.



Then I do also have a DBHandler class. This is where I make my PDO connection, and probally where it goes wrong.



public function __construct()
{
$this->config = new config();

try {
$this->PDO = new PDO($this->config->dsn(), $this->config->user(), $this->config->password(), $this->config->options());
} catch (PDOException $e) {
throw new PDOException($e->getMessage(), (int)$e->getCode());
}
}

public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new DBHandler();
}
return self::$instance;
}

public function connect()
{
return $this->PDO;
}


Then I also have a DBHelper what is basicly my CRUD class, here I write all my queries and this is the file I call in all my normal PHP files like "index.php".



Whenever I try to run a query I get an error, so I var_dumped my $db and got this error out of it:




Fatal error: Uncaught PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in D:AppCoreDBHelper.php:28 Stack trace: #0 D:AppCoreDBHelper.php(28): PDO->prepare('SELECT * FROM u...') #1 D:AppCoreDBHelper.php(55): AppCoreDBHelper->userQuery('SELECT * FROM u...') #2 D:Appindex.php(28): AppCoreDBHelper->select('users', '*', 'userName', NULL) #3 {main} thrown in D:AppCoreDBHelper.php on line 28




Can anyone tell me where it goes wrong?










share|improve this question
















I need some help with my DB connection.



I've got my config.php:



namespace AppCore;


class config
{
private $db;
private $host;
private $user;
private $password;
private $charset;
private $opt;
private $dsn;

public function __construct()
{
$this->db = 'example';
$this->host = '127.0.0.1';
$this->user = 'root';
$this->password = '';
$this->charset = 'utf8';
$this->dsn = "mysql:host=$this->host;DBName=$this->db;charset=$this->charset;";

$this->opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false
);
}

// getters
public function db()
{
return $this->db;
}

public function host()
{
return $this->host;
}

public function user()
{
return $this->user;
}

public function password()
{
return $this->password;
}

public function charset()
{
return $this->charset;
}

public function options()
{
return $this->opt;
}

public function dsn()
{
return $this->dsn;
}
}


I chose to use a config class.



Then I do also have a DBHandler class. This is where I make my PDO connection, and probally where it goes wrong.



public function __construct()
{
$this->config = new config();

try {
$this->PDO = new PDO($this->config->dsn(), $this->config->user(), $this->config->password(), $this->config->options());
} catch (PDOException $e) {
throw new PDOException($e->getMessage(), (int)$e->getCode());
}
}

public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new DBHandler();
}
return self::$instance;
}

public function connect()
{
return $this->PDO;
}


Then I also have a DBHelper what is basicly my CRUD class, here I write all my queries and this is the file I call in all my normal PHP files like "index.php".



Whenever I try to run a query I get an error, so I var_dumped my $db and got this error out of it:




Fatal error: Uncaught PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in D:AppCoreDBHelper.php:28 Stack trace: #0 D:AppCoreDBHelper.php(28): PDO->prepare('SELECT * FROM u...') #1 D:AppCoreDBHelper.php(55): AppCoreDBHelper->userQuery('SELECT * FROM u...') #2 D:Appindex.php(28): AppCoreDBHelper->select('users', '*', 'userName', NULL) #3 {main} thrown in D:AppCoreDBHelper.php on line 28




Can anyone tell me where it goes wrong?







php mysql pdo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 18:00









tereško

52.5k2078136




52.5k2078136










asked Nov 26 '18 at 20:17









yorrid08yorrid08

93




93













  • Off topic: normally you would call getters more like ... getDb(), getDatabase() talking about the config class or if you use setters setDd(...), setDatabase(...) they you directly can see the function of the function

    – Raymond Nijland
    Nov 26 '18 at 20:22








  • 1





    The dsn DBName=$this->db try it with lowercase dbname=$this->db not sure if it matters

    – Raymond Nijland
    Nov 26 '18 at 20:25






  • 1





    @Raymond, you are right, DBName causes Invalid catalog name: 1046 No database selected. When the database does not exist, then I get error 1049.

    – digijay
    Nov 26 '18 at 21:34











  • @RaymondNijland Thanks for the help I do connect with the database now. Just have an SQL error.

    – yorrid08
    Nov 27 '18 at 9:37











  • It should be dbname, not DBName. Doc.

    – yivi
    Nov 27 '18 at 12:04



















  • Off topic: normally you would call getters more like ... getDb(), getDatabase() talking about the config class or if you use setters setDd(...), setDatabase(...) they you directly can see the function of the function

    – Raymond Nijland
    Nov 26 '18 at 20:22








  • 1





    The dsn DBName=$this->db try it with lowercase dbname=$this->db not sure if it matters

    – Raymond Nijland
    Nov 26 '18 at 20:25






  • 1





    @Raymond, you are right, DBName causes Invalid catalog name: 1046 No database selected. When the database does not exist, then I get error 1049.

    – digijay
    Nov 26 '18 at 21:34











  • @RaymondNijland Thanks for the help I do connect with the database now. Just have an SQL error.

    – yorrid08
    Nov 27 '18 at 9:37











  • It should be dbname, not DBName. Doc.

    – yivi
    Nov 27 '18 at 12:04

















Off topic: normally you would call getters more like ... getDb(), getDatabase() talking about the config class or if you use setters setDd(...), setDatabase(...) they you directly can see the function of the function

– Raymond Nijland
Nov 26 '18 at 20:22







Off topic: normally you would call getters more like ... getDb(), getDatabase() talking about the config class or if you use setters setDd(...), setDatabase(...) they you directly can see the function of the function

– Raymond Nijland
Nov 26 '18 at 20:22






1




1





The dsn DBName=$this->db try it with lowercase dbname=$this->db not sure if it matters

– Raymond Nijland
Nov 26 '18 at 20:25





The dsn DBName=$this->db try it with lowercase dbname=$this->db not sure if it matters

– Raymond Nijland
Nov 26 '18 at 20:25




1




1





@Raymond, you are right, DBName causes Invalid catalog name: 1046 No database selected. When the database does not exist, then I get error 1049.

– digijay
Nov 26 '18 at 21:34





@Raymond, you are right, DBName causes Invalid catalog name: 1046 No database selected. When the database does not exist, then I get error 1049.

– digijay
Nov 26 '18 at 21:34













@RaymondNijland Thanks for the help I do connect with the database now. Just have an SQL error.

– yorrid08
Nov 27 '18 at 9:37





@RaymondNijland Thanks for the help I do connect with the database now. Just have an SQL error.

– yorrid08
Nov 27 '18 at 9:37













It should be dbname, not DBName. Doc.

– yivi
Nov 27 '18 at 12:04





It should be dbname, not DBName. Doc.

– yivi
Nov 27 '18 at 12:04












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53488396%2fpdo-db-connection%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53488396%2fpdo-db-connection%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)