Created
August 7, 2021 08:11
-
-
Save dimm999/e29fd87b18bc0c7e497a41e60d90c451 to your computer and use it in GitHub Desktop.
PHP PDO Connection and query eample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$host = 'localhost'; | |
$database = 'database'; | |
$user = 'login'; | |
$pass = 'password'; | |
$charset = 'utf8'; | |
$dsn = "mysql:host=$host;dbname=$database;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()); | |
} | |
$data = $pdo->query("SELECT id FROM stat where DATE(dt) = CURDATE()")->fetchAll(PDO::FETCH_ASSOC); | |
if(count($data) <= 0) { | |
$pst = $pdo->prepare("insert into stat(place, ip, dt) values(:place, :ip, :dt)"); | |
$pst->execute([ | |
':place' => 'domain.com', | |
':ip' => '127.0.0.1', | |
':dt' => date('Y-m-d H:i:s'), | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment