2012-01-27 73 views
-1

我遇到的問題是當我回顯或打印下列變量時,我收到的數據只是我表格中列出的最後一項業務的數據。查詢爲什麼只返回一組數據?

目前無論上市我單擊我得到的最後一次業務返回相同的一組數據。

正如您在下面的代碼中所看到的,我將通過點擊列表中的business_name傳遞給我的查詢以查找相關業務配置文件信息。

$business_name = mysql_real_escape_string($_GET['business_name']); 

$query = "SELECT 
business_id, 
category, 
years_recommended, 
profile_size, 
business_name, 
established, 
employees, 
service, 
strengths, 
ideal_for, 
reassurance 

FROM 
business_data 

WHERE 
business_name = '$business_name' 

AND 
profile_size = 'A' 

OR 
profile_size = 'B' 

OR 
profile_size = 'C' 

OR 
profile_size = 'D' 

OR 
profile_size = 'E'"; 

$result = mysql_query($query, $dbc) 
or die (mysql_error($dbc)); 

while($row = mysql_fetch_array($result)) { 

$business_id = $row["business_id"]; 
$profile_size = $row["profile_size"]; 
$category = $row["category"]; 
$years = $row["years_recommended"]; 
$established = $row["established"]; 
$employees = $row["employees"]; 
$service = $row["service"]; 
$strengths = $row["strengths"]; 
$ideal_for = $row["ideal_for"]; 
$reassurance = $row["reassurance"]; 
} 

echo... 

如果您需要更多信息,請讓我知道。

我的代碼有什麼問題嗎?

非常感謝提前。

+0

降票有點苛刻。它不像我已經付出了努力。 – 2012-01-27 15:51:43

回答

7

您的echo調用位於獲取循環之外,因此即使其他人已返回,您也只能看到最後一個結果。

while($row = mysql_fetch_array($result)) { 

    $business_id = $row["business_id"]; 
    $profile_size = $row["profile_size"]; 
    $category = $row["category"]; 
    $years = $row["years_recommended"]; 
    $established = $row["established"]; 
    $employees = $row["employees"]; 
    $service = $row["service"]; 
    $strengths = $row["strengths"]; 
    $ideal_for = $row["ideal_for"]; 
    $reassurance = $row["reassurance"]; 

    // Echo **inside** the loop 
    echo... 
} 

如果你願意,你可以根據需要存儲在一個大陣,然後可以在腳本中任何後續使用所有的結果,多次:

// Array for all results 
$results = array(); 
while($row = mysql_fetch_array($result)) { 
    // Append each row fetched onto the big array 
    $results[] = $row; 
} 
// Now use it as needed: 
foreach ($results as $r) { 
    echo $r['profile_size']; 
    print_r($r); 
} 
+0

謝謝你的迴應。有什麼理由爲什麼我得到一個語法錯誤在科莫多編輯例如。當我在回聲後移動花括號時出現紅色下劃線。 – 2012-01-27 15:49:23

+0

@RichardBell如果你的'echo'調用沒有任何東西,而且它只是'echo..',那是一個語法錯誤。如果你使用真正的語句'echo $ row ['profile_size'];'應該沒有錯誤。 – 2012-01-27 15:51:20

+0

好的。實現了echo $ row ['profile_size'];'變量包含所有的配置文件大小,而不僅僅是目標配置文件。我想我需要進一步考慮這一點。應該只有一個結果'business_name'匹配'profile_size'。感謝您的輸入。 – 2012-01-27 16:06:40

1

您的迴音應在循環內