2012-10-19 108 views
-1

我有一個mysql查詢返回多列中的值的總和。查詢是正確的,當我將它包含在主頁面中時,一切正常。但是,當我在另一個 頁面中包含查詢並返回總和並在主頁面中打印它不工作時,該問題開始。php mysql查詢功能不工作

下面是主要的頁面調用:

require('totals.php'); 
$res_easyhint_total=easyhint_totals($currentpid); 

print $res_easyhint_total; 
//The above is contained in a while loop and current pid gets updated each time. 

功能頁面:

function easyhint_totals($currentpid){ 
require('connect.php'); 
$sql_easyhint_total = "SELECT sum(Coffee+Gift+Cools+Affection+Patience+Anger+EHignore) from whyangry.posts where Pid=$currentpid"; 
$res_easyhint_total=mysql_query($sql_easyhint_total,$con); 
$res_easyhint_total=mysql_fetch_array($res_easyhint_total); 
$res_easyhint_total=$res_easyhint_total[0]; 
return $res_easyhint_total; 
} 

我不明白是什麼錯誤,請幫助。

+0

定義不工作...它會拋出一個錯誤?意外的結果?如果是這樣,結果/預期結果是什麼? – sachleen

+0

它沒有給出任何結果。並且while循環卡在那裏。 –

+1

首先,你不應該在函數中包含文件......或者在循環中,如果這就是你正在做的。 – sachleen

回答

0

你是否在connect.php中定義了任何函數?如果不嘗試添加:

$res_easyhint_total=mysql_query($sql_easyhint_total,$con); 

if (mysql_errno() != 0) { 
    echo mysql_error(); 
} 

$res_easyhint_total=mysql_fetch_array($res_easyhint_total); 
0

您是否檢查過新頁面的結果本身?我的意思是說,如果你試圖從新頁面打印結果,就像你從主頁面嘗試一樣。然後還有一件事需要說明,即包含這兩個文件的路徑。嘗試從新頁面傳遞任何其他變量到主頁面,並檢查新文件是否包含正確。

如果您能夠從主頁上的新頁面訪問其他變量,並且它只是不返回該函數的結果。嘗試在主頁面上添加connect.php並檢查它。

0

您之前是否包含了文件「connect.php」?

if([email protected]_once('connect.php')) { 
    // include connect.php 
} 
0

檢查您的連接字符串返回正確的鏈接標識,並檢查日誌是否也有像警告MySQL的任何錯誤或警告:請求mysql_query()[2002]沒有這樣的文件或目錄(試圖連接通過unix:///var/run/mysql/mysql.sock)。在這種情況下,嘗試設置正確的套接字文件位置。