pg_query 抛出错误
pg_query throwing an error
我正在尝试从 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();
}
?>
正如我在混合 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());
?>
我正在尝试从 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();
}
?>
正如我在混合 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());
?>