2012-03-26 57 views
0

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

这里是从原来的MySQL应用程序我的代码:

// below is the "connexion.php" file 
function connexion() 
{ 
    [email protected]_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(); 
+0

在您迁移时,为什么不在使用PDO时使用PDO?请放弃习惯或使用'@'来抑制错误。 – PeeHaa 2012-03-26 22:39:39

+0

缺乏时间,我尽可能少地修改,因此只是查询的东西。感谢您对@的建议。 – 2012-03-26 22:58:05

回答

2

PostgreSQL的不叫Connexion的PHP代码()所以它永远不会连接,不像mysql代码。

0

你可以试试你的查询,如果失败,则显示错误

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

这样你可以看到你的错误。

errors

UPDATE:

在您的连接删除@看到了错误,并添加or die(pg_last_error()) 看到错误