我有一个在mysql中完美运行的例子但是当我转换为postgresql时不起作用。
Mysql代码:(index.php):
<?php
require('includes/connection.php');
include('func.php');
?>
<html>
<head>
</head>
<body>
<p>
<form action="" method="post">
<select name="drop_1" id="drop_1">
<option value="" selected="selected" disabled="disabled">Select a Category</option>
<?php getTierOne(); ?>
</select>
</form>
</body>
</html>
Mysql代码(func.php):
function getTierOne()
{
$result = mysql_query("SELECT DISTINCT tier_one FROM three_drops")
or die(mysql_error());
while($tier = mysql_fetch_array( $result ))
{
echo '<option value="'.$tier['tier_one'].'">'.$tier['tier_one'].'</option>';
}
}
POstgreSql代码:(index.php):
<?php
require('includes/connection.php');
include('func.php');
?>
<html>
<head>
</head>
<body>
<p>
<form action="" method="post">
<select name="drop_1" id="drop_1">
<option value="" selected="selected" disabled="disabled">Select a Category</option>
<?php getTierOne(); ?>
</select>
</form>
</body>
</html>
PostgreSql代码(func.php):
function getTierOne()
{
$sth = $dbh->query("SELECT DISTINCT tier_one FROM three_drops");
while($tier = $sth->fetch())
{
echo '<option value="'.$tier['tier_one'].'">'.$tier['tier_one'].'</option>';
}
}
连接(Postgresql):
require("constants.php");
$dbh = new PDO('pgsql:host=' . DB_SERVER . ';dbname=' . DB_NAME, DB_USER, DB_PASS,
array (PDO::ATTR_PERSISTENT => true ));
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
连接正常,但是我收到错误:在非对象上调用成员函数query()。
答案 0 :(得分:1)
尝试定义$ dbh global。
connection.php:
require("constants.php");
global $dbh;
func.php:
function getTierOne()
{
global $dbh;