$host = "localhost";//Ip of database, in this case my host machine
$user = "root"; //Username to use
$pass = "qwerty";//Password for that user
$dbname = "DB";//Name of the database
try {
$connection = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e)
{
echo $e->getMessage();
}
const DB_HOST = 'localhost';
const DB_NAME = 'DB_Name'; //Name of the database
const DB_USERNAME = 'username'; //Username to use
const DB_PASSWORD = 'Password'; //Password for that user
// Data Source Name
$dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME;
$options = [
PDO::ATTR_EMULATE_PREPARES => false, // turn off emulation mode for "real" prepared statements
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //turn on errors in the form of exceptions
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, //make the default fetch be an anonymous object with column names as properties
];
//Create PDO instance
try {
$pdo = new PDO($dsn, DB_USERNAME, DB_PASSWORD, $options);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$host = '127.0.0.1';
$db = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (PDOException $e) {
throw new PDOException($e->getMessage(), (int)$e->getCode());
}
Code Example |
---|
Php :: laravel date diff |
Php :: how-to-generate-an-xlsx-using-php |
Php :: laravel packages |
Php :: array_merge in php |
Php :: php get user county |
Php :: smarty php |
Php :: enable extensions in php.ini |
Php :: laravel copy row |
Php :: wordpress args |
Php :: php preg_quote |
Php :: laravel set date format |
Php :: saving an image from pc to php |
Php :: laravel 9 excel |
Php :: php call method from another class |
Php :: PHP strip_tags — Strip HTML and PHP tags from a string |
Php :: laravel db raw count where |
Php :: drop foreign key laravel |
Php :: php recortar string |
Php :: laravel get namespace |
Php :: how to determine if file is empty in php |
Php :: wordpress login user programmatically |
Php :: laravel pagination |
Php :: how to add two string in php |
Php :: laravel new line in session flash message |
Php :: laravel load relationship |
Php :: laravel relation with limit |
Php :: timezones php |
Php :: laravel migration mediumtext length |
Php :: encode zlib php |
Php :: wp large medium large |