我正在尝试从postgresql数据库中获取数据,我得到错误:pg_last_error((期望参数1是资源,在/path/to/quer..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();
}
?>
如我在评论中所说的,我正在混合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());
?>