问题描述
关于 PostgreSQL 的新手问题.我正在将我在 Linux 服务器上创建的 MySQL/PHP 应用程序迁移到 MacOSX Lion Server 环境中的 PostgreSQL/PHP.这是我第一次使用 Postgres.我正在测试的第一个查询不起作用,因为它什么都不返回(甚至没有错误消息,无论我添加哪个检查代码).我做错了什么?我已经阅读了网络上的文章,包括 php 官方网站上的文档,但是所有的评论、个人方法和版本与版本之间的差异,无论是使用 Postgres 还是 PHP,都使它非常混乱,我最终不明白我应该写什么我的查询和 fetch_array.感谢您的任何建议.
Newbie question about PostgreSQL. I'm migrating a MySQL/PHP app I've created, hosted on a Linux server, to PostgreSQL/PHP on a MacOSX Lion Server environment. It's my first experience with Postgres. The first query I'm testing doesn't work as it returns nothing (not even an error message, whichever check code I add). What did I do wrong? I've read articles on the web including the doc on php official website but all comments, personal methods and differences from version to version, either with Postgres or PHP, make it very confusing and I eventually don't understand exactly show I should write my query and fetch_array. Thanks for any suggestions.
这是我的原始 MySQL 应用程序代码:
Here is my code from the original MySQL application:
// below is the "connexion.php" file
function connexion ()
{
$link=@mysql_connect ("localhost","username","pwd");
if ($link && mysql_select_db ("database"))
return ($link);
return (FALSE);
}
// below is the "index.php" file
require ("connexion.php");
connexion() or exit();
$reqcount = mysql_query ("SELECT * FROM people");
$result = mysql_num_rows($reqcount);
echo "Total : ".$result." people";
mysql_free_result ($reqcount);
mysql_query("set names 'utf8'");
$reqcat = mysql_query ("SELECT catname FROM categories ORDER BY catname");
while ($fieldcat = mysql_fetch_array($reqcat))
{
$name = $fieldcat[catname];
echo $name."<br>";
}
mysql_free_result ($reqcat);
mysql_close ();
这是 PostgreSQL 改编版:
And here is the PostgreSQL adaptation:
// connexion.php
function connexion ()
{
$link=pg_connect("host=localhost port=5432 dbname=database user=username password=pwd connect_timeout=5 options='--client_encoding=UTF8'");
return ($link);
}
// index.php
require ("connexion.php");
$reqcount = pg_query ($link,"SELECT * FROM people");
$result = pg_num_rows($reqcount);
echo "Total : ".$result." people";
pg_free_result ($reqcount);
$reqcat = pg_query ($link,"SELECT catname FROM categories ORDER BY catname");
while ($fieldcat = pg_fetch_array($reqcat))
{
$name = $fieldcat[catname];
echo $name."<br>";
}
pg_free_result ($reqcat);
pg_close ();
推荐答案
postgresql 的 php 代码不调用 connexion() 所以它永远不会连接,不像 mysql 代码.
The php code for postgresql doesn't call connexion() so it never connects, unlike the mysql code.
这篇关于pg_query 不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!