连接数据库测试
1、新建测试文件test.php
<?php
$servername = "172.16.19.117";
$username = "vbadmin";
$password = "Vbase@admin";
$dbname = "vastbase";
$port = "5432";
try {
$conn = new PDO("pgsql:host=$servername;port=$port;dbname=$dbname", $username, $password);
// 设置 PDO 错误模式,用于抛出异常
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO hzy.user (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')";
// 使用 exec() ,没有结果返回
$conn->exec($sql);
$rs = $conn->query("select * from hzy.user");
foreach($rs as $row) {
print_r($row);
}
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
2、执行测试
进入php可执行文件所在目录执行 php test.php
注意:如果执行报错Unable to load dynamic library 'pdo_pgsql',则根据报错提示 tried:后面的路径将PHP解压目录下的ext拷贝到对应目录下再次运行