Wireless Army
This is a blog / tips and tricks website for web developers and security researchers.
follow us in feedly


PDO setup help
by admin
 at 2023-04-12 10:59:21.

$host    = DATABASE_HOST;
$dbase   = DATABASE_NAME;
$user    = DATABASE_USER;
$pass    = DATABASE_PASS;
$charset = DATABASE_CHARSET;

// set the connection parameters
$dsn = "mysql:host=$host;dbname=$dbase;charset=$charset";
$opt = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];

// get the connection
$this->pdb = new PDO($dsn, $user, $pass, $opt);
Then you can use the statements below to read and write the database:

// WRITE STATEMENT THAT RETURNS ROW ID
$stmt = $this->pdb->prepare($sql);
$stmt->execute($fields);
$retval = $this->pdb->lastInsertId();

// READ STATEMENTS

$stmt = $this->pdb->prepare($sql);
$stmt->execute($fields);

// fetches one field
$result = $stmt->fetch(PDO::FETCH_COLUMN); 

// fetches multiple fields
$result = $stmt->fetch(PDO::FETCH_ASSOC);