2013-12-16 37 views
4

我在我的profileTable中有一列叫做「關聯」...我試圖查詢關聯關係的關聯關係。在PHP/MySQL查詢中創建關聯數組

$sql = mysqli_query($con,"SELECT * FROM profileTable WHERE Keyword_ID LIKE 
'%".$getKeyID."%' ORDER BY Associations <> 'School of Engineering 
and Computer Science', Associations AND LaName ASC LIMIT $start,$end"); 

我能夠通過教育索引配置文件,但不是通過Association。

while ($row = mysqli_fetch_array($sql)) { 
     $key = $row['Keyword_Name']; 
     $keyID = $row['Keyword_ID']; 
     $fname = $row['FirName']; 
     $lname = $row['LaName']; 
     $mname = $row['MName']; 
     $suffix = $row['Suffix']; 
     $title = $row['Title']; 
     $title2 = $row['Title2']; 
     $title3 = $row['Title3']; 
     $education = $row['Education']; 
     $education2 = $row['Education2']; 
     $education3 = $row['Education3']; 
     $dept = $row['Dept']; 
     $phone1 = $row['PH1']; 
     $phone2 = $row['PH2']; 
     $email = $row['Email']; 
     $photo = $row['Photo']; 
     $bio = $row['BioLK']; 
     $website = $row['Website']; 
     $assocs = $row['Associations']; 

$actions=array('School of Engineering and Computer Science'=>'Computer Science 
     and Engineering'); 

我需要涉及與$鍵(字)的關聯,所以如果某個關鍵字用戶點擊「計算機」,它將涉及到「工程和計算機科學學院」和索引那些在「學校首先是「工程與計算機科學」。

這工作正常與其他列像「教育」,但似乎無法讓它與我的關聯數組一起工作。有任何想法嗎?

例如sqlfiddle:http://sqlfiddle.com/#!2/c1802/7/0

+4

問題:爲什麼你將這一行解壓縮爲一百萬個變量? –

+0

?? @ Mike'Pomax'Kamermans解釋... – MizAkita

+1

不,這是完整的問題。你有一個包含所有數據的數據容器,爲什麼要將它解壓縮到20個變量,而不是在需要使用它的代碼段中使用關聯數據數組?這隻會讓你的代碼變得不必要的巨大,沒有任何真正的好處(例如$ title1與$ data [「title1」]一樣清晰) –

回答

1

以下查詢列出根據需要排序的輸出。

SELECT * FROM mediaContacts ORDER BY associations DESC,lname ASC 

Fiddle

由於associations具有School of Engineering and Computer Science作爲最後一個字母記錄這一過程。

如果School of Engineering and Computer Science不是最後一個字母記錄,則失敗並需要其他方法。

+0

謝謝@davidstrachan這解決了我的查詢......並按照預期的方式根據我點擊的關鍵字隔離了我的關聯。再次感謝! – MizAkita

0

你可以嘗試以下

變化

while ($row = mysqli_fetch_array($sql)) {

while ($row = mysql_fetch_assoc($sql)) {

我t將返回一個與取出的行對應的關聯數組,並向前移動內部數據指針。

+0

我也這麼認爲,但它並沒有返回任何結果。使用fetch_array返回結果,只是不首先爲「關聯」建立索引。嗯... – MizAkita

+0

我已經添加了一個sqlfiddle在:http://sqlfiddle.com/#!2/c1802/7/0,如果你更改「assoc」回到「數組」它的工作... – MizAkita