2012-12-05 153 views
0

我正在爲SecondLife的一個小系統工作,它涉及到數據庫的大量使用&我遇到了一個問題,我不能爲我的生活解決問題。根據多列值選擇一行?

我想運行一個查詢,選擇基於兩個不同列值的整個行。我使用$ _GET ['']調用來確定我需要哪些細節,然後我需要從整行中提取所有數據並將其回顯到空白頁面。

現在,我首先得到它,但它是拉UUID創建的所有行並回顯它們,當我只需要一個特定的。然後我試着讓它與第二個標識符(爲了測試目的而被稱爲「字段」的列)一起工作,這幾乎是我丟失它的地方。

我只需要選擇一個有兩個確切值的行,並且我需要它是嚴格的,因此它只選擇一個既有兩個也不只有一個的行。

任何想法?

腳本封閉(不介意混亂或註釋掉的代碼,我一直以供參考;!我要清理腳本,一旦我得到它的工作)

 <?php 

// Get key from the parameter. 
$avatar_key = $_GET['key']; 

// Quest params. 
$questname = $_GET['questname']; 
$questid = $_GET['id']; 


// Connect to the database. $con holds connection info. 

$con = mysql_connect("localhost","user","pass"); 

// Error checking. 
if(!$con) 
{ 
    die('Could not connect: '.mysql_error()); 
} 


// Select the DB we want. 
mysql_select_db("yyyyy_slcom",$con); 

// Build the query. 
$query = "SELECT * FROM sldb_data WHERE sldb_data.uuid = '$avatar_key' AND sldb_data.field = '$questname'"; 
/*$query = "select * from (
          select uuid, field, value, progress, ROW_NUMBER() OVER (PARTITION BY uuid ORDER BY progress DESC) 
      ";*/ 

// Run the query, store the result in variable $result. 
$result = mysql_query($query); 

if(!result) 
{ 
die('Error: ' . mysql_error()); 
} 

// Check how many rows we got back with our query. 
$rows_returned = mysql_num_rows($result); 

echo $row['field']; 

// If we get anything back, 
if($rows_returned > 0) 
{ 




    //echo $row['field'] . ' was found.' . $questname; 
    /*if(!$row['field']) 
    { 
     echo 'You are not on this quest yet.'; 
    } 
    elseif(($row['field']) && ($row['value'] == "notstarted")) 
    { 
     echo 'This quest hasn\'t started yet.'; 
    } 
    elseif(($row['field']) && ($row['value'] == "started")) 
    { 
     echo 'You are on quest: "' .$row['field']. '"'; 
    }*/ 

    /* $fields = mysql_list_fields("tenaar_slcom","sldb_data"); 
    $columns = mysql_num_fields($fields); 
    for ($i = 0; $i < $columns; $i++) {$field_array[] = mysql_field_name($fields, $i);} 

    if(in_array($quest, $field_array)) 
    { 
     echo 'Your quest is ' . $row['field']; 
    } 
    elseif(!in_array($questname, $field_array)) 
    { 
     echo 'You are not on this quest yet.'; 
    } 
    elseif((in_array($questname, $field_array)) && ($row['value'] == "notstarted")) 
    { 
     echo 'You have not started quest "' .$row['field']. '" yet.'; 
    } */  

    // cycle through and print what we got. 
    //while($row = mysql_fetch_array($result)) 
    //{ 

     //echo 'Quest name: ' . $row['field'] . ' | Progress: ' .$row['value'] . '<br />'; 

    //} 
} 
/*else 
{ 
echo 'You are not on this quest yet.'; 
}*/ 

// Close the connection. 
mysql_close($con); 

?> 
+1

很難說沒有看到表,但你有沒有嘗試基於'$ questid'而不是'$ questname'拉? – nullrevolution

+0

+1 @nullrevolution。 U可能會向我們展示您的表格和預期輸出:$ – bonCodigo

+0

有什麼問題?你的數據庫查詢確實需要兩個值(作爲'和'條件)。 –

回答

2

你似乎丟失獲取行數和打印字段之間的一個步驟。您需要將查詢的結果實際存儲在$ row數組中。

$rows_returned = mysql_num_rows($result); 
$row = mysql_fetch_array($result); 
echo $row['field']; 
+0

不敢相信我設法錯過了這樣一個基本的步驟n.n謝謝! – Martin