<?php declare(strict_types=1); ini_set('display_errors', 'On'); error_reporting(-1); $driver = 'pgsql'; // 使用pgsql驱动 $host = 'localhost'; $dbname = 'testdb'; $schema = 'private'; $username = '账号'; $password = $password ?? '密码'; $options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; $pdo = new PDO("$driver:host=$host;dbname=$dbname", $username, $password, $options); $query = "SELECT \"name\", \"gender\", \"birth\" FROM \"$schema\".\"prefix_user\" WHERE \"uid\" = :uid LIMIT 1"; echo $query . PHP_EOL; // SELECT "name", "gender", "birth" FROM "private"."prefix_user" WHERE "uid" = :uid LIMIT 1 $sth = $pdo->prepare($query); $params = ['uid' => 3]; foreach ($params as $field => $value) { $sth->bindValue(":$field", $value); } $sth->execute(); $fetchAll = $sth->fetchAll(); $row = $fetchAll[0] ?? []; if ($row) { $name = $row['name']; $gender = $row['gender']; $birth = $row['birth']; echo "俺叫{$name}({$gender}),出生于{$birth}年。"; // 俺叫张三(男),出生于2003年。 }
Copyright © 2024 码农人生. All Rights Reserved