2016-02-24 73 views
2

我在這裏有一個非常非常奇怪的問題。我有數據在php中的數組。我想要爲數組查詢數據庫並獲取結果的每個項目。它只顯示我具體的結果,而不是一切。陣列打印數據正確,但查詢只顯示一半

我的代碼:

foreach($my as $k=> $v){ 
    //echo "Key: ". $k . " Value: " . $v . "<br/>"; 
    $sql2 = "SELECT column10 FROM `table` WHERE column1 = '$v' "; 
    $res2 = mysql_command($sql2); 
    echo $sql2 . "<br/>"; 
    $rowA = mysql_fetch_assoc($res2); 
    //echo "<strong>Alternative: </strong>" . $v. "<strong> Auto Alternative: </strong>" . $rowA['column10'] . "<br/>"; 

} 
echo '</table>'; 
echo "<pre>"; 
print_r($my); 
echo "</pre>"; 

在瀏覽器中的結果,如果我回應查詢和密鑰:和值是這樣的:

SELECT column10 FROM `table` WHERE column1 = 'Villetta La Canoa' 
SELECT column10 FROM `table` WHERE column1 = ' Casa Immerso nel Verde' 
SELECT column10 FROM `table` WHERE column1 = ' La Rosetta' 
SELECT column10 FROM `table` WHERE column1 = 'Agriturismo La Nonna' 
SELECT column10 FROM `table` WHERE column1 = ' Villetta Cassiopeia' 
SELECT column10 FROM `table` WHERE column1 = ' La Rosetta' 
SELECT column10 FROM `table` WHERE column1 = 'Ca Gianca 2' 
SELECT column10 FROM `table` WHERE column1 = ' Villetta Teresa' 
SELECT column10 FROM `table` WHERE column1 = ' Appartamento Pinamare' 
SELECT column10 FROM `table` WHERE column1 = ' Casa del Principe' 
SELECT column10 FROM `table` WHERE column1 = 'Ca Gianca 2' 
SELECT column10 FROM `table` WHERE column1 = ' Villetta Teresa' 
SELECT column10 FROM `table` WHERE column1 = ' Appartamento Pinamare' 
SELECT column10 FROM `table` WHERE column1 = ' Casa del Principe' 
SELECT column10 FROM `table` WHERE column1 = 'Ca Gianca 2' 
SELECT column10 FROM `table` WHERE column1 = ' Villetta Teresa' 
SELECT column10 FROM `table` WHERE column1 = ' Appartamento Pinamare' 
SELECT column10 FROM `table` WHERE column1 = ' Casa del Principe' 

的關鍵價值是:

Key: 0 Value: Villetta La Canoa 
Key: 1 Value: Casa Immerso nel Verde 
Key: 2 Value: La Rosetta 
Key: 3 Value: Agriturismo La Nonna 
Key: 4 Value: Villetta Cassiopeia 
Key: 5 Value: La Rosetta 
Key: 6 Value: Ca Gianca 2 
Key: 7 Value: Villetta Teresa 
Key: 8 Value: Appartamento Pinamare 
Key: 9 Value: Casa del Principe 
Key: 10 Value: Ca Gianca 2 
Key: 11 Value: Villetta Teresa 
Key: 12 Value: Appartamento Pinamare 
Key: 13 Value: Casa del Principe 
Key: 14 Value: Ca Gianca 2 
Key: 15 Value: Villetta Teresa 
Key: 16 Value: Appartamento Pinamare 
Key: 17 Value: Casa del Principe 

我得到的是:

Alternative: Villetta La Canoa Auto Alternative: Villa Ronchi, Casa Ciserai, Villino Torretta, Casa Bianca 
Alternative: Casa Immerso nel Verde Auto Alternative: 
Alternative: La Rosetta Auto Alternative: 
Alternative: Agriturismo La Nonna Auto Alternative: Agriturismo Antico Granaio, Casa Ciserai, Villa Ronchi, La Rosetta 
Alternative: Villetta Cassiopeia Auto Alternative: 
Alternative: La Rosetta Auto Alternative: 
Alternative: Ca Gianca 2 Auto Alternative: Ca Gianca 1, La Vigna 2, Villetta Teresa 
Alternative: Villetta Teresa Auto Alternative: 
Alternative: Appartamento Pinamare Auto Alternative: 
Alternative: Casa del Principe Auto Alternative: 
Alternative: Ca Gianca 2 Auto Alternative: Ca Gianca 1, La Vigna 2, Villetta Teresa 
Alternative: Villetta Teresa Auto Alternative: 
Alternative: Appartamento Pinamare Auto Alternative: 
Alternative: Casa del Principe Auto Alternative: 
Alternative: Ca Gianca 2 Auto Alternative: Ca Gianca 1, La Vigna 2, Villetta Teresa 
Alternative: Villetta Teresa Auto Alternative: 
Alternative: Appartamento Pinamare Auto Alternative: 
Alternative: Casa del Principe Auto Alternative: 

任何人都可以解釋什麼是錯的?

+0

您好,首先提示的全部cople的,'mysql_ *'功能過時,你應該改變爲'mysqli'或''PDO,你應該給DB列更多descripive名稱,它可以是一個噩夢保持或理解該代碼:) – Asur

+0

它顯示你'SELECT column10',而不是'SELECT *'? – devpro

+2

'mysql_command'到底是什麼? –

回答

4

從我的角度來看,您可能需要修剪$ v以確保字符串的開頭和結尾處沒有空格。

foreach($my as $k=> $v){ 
    $v = trim($v); 

此外,您應該嘗試'LIKE'運算符而不是'='。也許把'%'加到$ v。

$sql2 = "SELECT column10 FROM `table` WHERE column1 LIKE '%{$v}%'"; 
+0

我會在2分鐘內接受..感謝很多。 – BRG

+0

不要介意這個意見,但這只是一個解決方法,請你的眼睛看到你的代碼修復。問題是,爲什麼那裏有額外的空間呢?如果你不應該存儲額外的空間,那麼你爲什麼要存儲它,然後在最後修剪它?有一天會導致意想不到的結果。修剪格式不正確的字符串只是爲了讓事情發展而已,這不是一個好的設計解決方案。修復錯誤地在數據庫中存儲額外空間的代碼。 –

+0

@BRG Hanky說這是對你的代碼的快速解決方案。聽取他的建議,並嘗試從源頭中刪除空間。 – besciualex

3

什麼是mysql_command?我敢肯定它的一個錯字,你的意思mysql_query(唉甚至認爲: - /)

而且你不明白,因爲像這樣的

SELECT column10 FROM `table` WHERE column1 = ' Casa Immerso nel Verde' 
              ^

一些字符串後面的額外空間用於一些結果你樣本數據證實了這一點。你沒有選擇的唯一結果就是那些有額外空間的結果。