標題:
PHP 透過 PDO 操作 pgsql
[打印本頁]
作者:
ray
時間:
2020-6-13 10:46
標題:
PHP 透過 PDO 操作 pgsql
本帖最後由 ray 於 2020-6-13 11:35 編輯
https://www.postgresqltutorial.com/postgresql-php/
Connect:
$conStr = "pgsql:host=[host];port=5432;dbname=[db];user=[user];password=[password]";
$pdo = new \PDO($conStr);
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
Insert:
$sql = 'INSERT INTO usertb(uname,pass) VALUES(:uname,:pass)';
$stmt = $pdo->prepare($sql);
// pass values to the statement
$stmt->bindValue(':uname', "hello");
$stmt->bindValue(':pass', "hello");
// execute the insert statement
$stmt->execute();
// return generated id
echo $pdo->lastInsertId();
Select:
$stmt = $pdo->query('SELECT uname, pass FROM usertb');
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC))
echo $row['uname'].",".$row['pass']."<br>";
作者:
ray
時間:
2020-6-13 11:54
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
This is firt test<br>
<?php
$conStr = "pgsql:host=ec2-34-198-243-120.compute-1.amazonaws.com;port=5432;dbname=d33tv102tftng6;user=zdnbsjhbujtaca;password=4e00d5400a3b2f211a31dae4b7b679212d0e7c72763659cd5337b6e8b1f4227e";
$pdo = new \PDO($conStr);
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
if(isset($_POST["uname"]) && isset($_POST["password"]) && $_POST["uname"] != "" && $_POST["password"] != "")
{
$sql = 'INSERT INTO usertb(uname,pass) VALUES(:uname,:pass)';
$stmt = $pdo->prepare($sql);
// pass values to the statement
$stmt->bindValue(':uname', $_POST["uname"]);
$stmt->bindValue(':pass', $_POST["password"] );
// execute the insert statement
$stmt->execute();
// return generated id
//echo $pdo->lastInsertId();
}
$stmt = $pdo->query('SELECT uname, pass FROM usertb');
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC))
echo $row['uname'].",".$row['pass']."<br>";
?>
<form action="" method="post">
Account:<input type=text name=uname><br>
Password:<input type=password name=password><br>
<input type=submit value='insert'>
</form>
</body>
</html>
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2