Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php pdo connection

$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();                         
}
Comment

php pdo

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();
}
Comment

php pdo example

$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());
}
Comment

pdo example

<?php

$dsn = "mysql:host=localhost;dbname=mydb";
$user = "user12";
$passwd = "12user";

$pdo = new PDO($dsn, $user, $passwd);

Comment

PREVIOUS NEXT
Code Example
Php :: api symfony 4 @ApiResource 
Php :: php access key stdClass object 
Php :: model class not found in laravel 
Php :: php validate credit card expiration date 
Php :: xampp php 5.6 download 64 bit 
Php :: wordpress popular posts query 
Php :: php execute a background process 
Php :: php flatten array 
Php :: wocommerce product image 
Php :: wordpress options 
Php :: add custom shortcode in contact form 7 
Php :: php while loop 
Php :: laravel uuid not showing in query 
Php :: remove square brackets from string php 
Php :: comment split une chaine de caratere en php 
Php :: php error handling 
Php :: use php artisan command through controller 
Php :: static class methods php 
Php :: use session in laravel 
Php :: how to run php in js 
Php :: remove time from date in carbon 
Php :: resize image using intervention laravel and save 
Php :: post format wordpress 
Php :: Parse error: syntax error, unexpected token "{" in C:xampphtdocsloginsystem1welcome.php on line 3 
Php :: variables in php 
Php :: Laravel permission to Vuejs 
Php :: curl download progress bar php 
Php :: macrotime phph 
Php :: theme mod disalow wp 
Php :: php encrypt password 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =