2012-03-26 108 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()) 看到錯誤