pg_query什么都不返回

时间:2012-03-26 22:30:14

标签: php postgresql osx-lion

关于PostgreSQL的新手问题。我正在将我在Linux服务器上托管的MySQL / PHP应用程序迁移到MacOSX Lion Server环境中的PostgreSQL / PHP。这是我第一次体验Postgres。我正在测试的第一个查询不起作用,因为它什么都不返回(甚至不是错误消息,无论我添加哪个检查代码)。我做错了什么?我已经阅读了网上的文章,包括php官方网站上的文档,但所有评论,个人方法以及版本之间的差异,无论是使用Postgres还是PHP,都让它非常混乱,我最终还是不明白我应该写的我的查询和fetch_array。感谢您的任何建议。

这是我原始MySQL应用程序中的代码:

// 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的改编:

// 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 ();

2 个答案:

答案 0 :(得分:2)

postgresql的php代码不会调用connexion(),所以它永远不会连接,与mysql代码不同。

答案 1 :(得分:0)

如果查询失败,您可以尝试查询显示错误

$reqcount = pg_query ($link,"SELECT catname FROM people") or die(pg_last_error());

通过这种方式,您可以看到错误。

errors

<强>更新

删除连接中的@以查看错误,然后添加or die(pg_last_error()) 看错误

相关问题