我试图从postgresql数据库中获取数据,我收到错误:pg_last_error()期望参数1是资源,在/path/to/query.php中给出的对象 所以数据作为对象而不是资源。任何想法如何解决这一问题?
SQL使用此代码:
foreach ($conn->query($sql1) as $row)
{
print $row["Site_ID"] . " ";
print $row["Site_name_1"] . "<br /> ";
}
但是当我使用pg_query代替时,问题就出现了 这是我的代码:
<?php include 'header.php'; ?>
<div class='container'>
<?php include 'menu.php'; ?>
<?php include 'PDO_connect.php'; ?>
<?php
$sql1='SELECT "Site_ID", "Site_name_1" FROM "Sites" ORDER BY "Sites"."Site_ID" ASC';
$result1 = pg_query($conn,$sql1);
if(!$result1) {
echo "There is an error!";
echo pg_last_error($conn);
}
?>
我的连接信息
<?php
try {
$dbuser = 'usr';
$dbpass = 'pwd';
$host = "localhost";
$dbname="db";
$conn = new PDO('pgsql:host=localhost;dbname=db', $dbuser, $dbpass);
}catch (PDOException $e) {
echo "Error : " . $e->getMessage() . "<br/>";
die();
}
?>
答案 0 :(得分:0)
正如评论中所说,我正在混合PDO和pg_connect,这解决了我的问题:
<?php
$servername = "localhost";
$username = "usr";
$password = "pwd";
$database = "db";
?>
<?php
$conn = pg_connect("host=localhost dbname=$database user=$username password=$password")or die("Can't connect to database".pg_last_error());
?>