gpt4 book ai didi

mysql - 将 mysql 查询转换为 postgresql 查询

转载 作者:行者123 更新时间:2023-11-29 14:46:47 25 4
gpt4 key购买 nike

我有一个在 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()。

最佳答案

尝试定义 $dbh 全局变量。

连接.php:

require("constants.php");
global $dbh;

func.php:

function getTierOne()
{
global $dbh;

关于mysql - 将 mysql 查询转换为 postgresql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6814544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com