I am trying to make a CRUD in PHP. Apache and MySQL is still running.
class DB
{
static private $connection;
const DB_TYPE = "mysql";
const DB_HOST = "localhost";
const DB_NAME = "crud";
const USER_NAME = "root";
const USER_PASSWORD = "";
static public function getConnection()
{
if (static::$connection == null) {
try {
static::$connection = new PDO(self::DB_TYPE . "host" . self::DB_HOST . "dbname" . self::DB_NAME . self::USER_NAME . self::USER_PASSWORD);
} catch (Exception $exception) {
throw new Exception("connection failed");
}
}
return static::$connection;
}
}
I am running on localhost:3306
phpMyAdmin is up and running
The output is in the picture
output:
2
Answers
I think you forgot spaces and signs in your connection arguments
That is your syntax :
Which give :
This is the syntax from the doc :
Your PDO invocation looks weird.
The construct for this is
new PDO($dsn, $user, $password);
The dsn has this format:
Your dsn section seems wrong, it should be more like this: