2011-12-16 148 views
8

任何想法都可以幫助,我已經在這裏一段時間了,只是無法弄清楚什麼是錯的。MySQL查詢在phpmyadmin中可用,但不能通過php

問題:查詢工作正常,直到我在多個單詞搜索中添加,您可以在下面看到粗體字。然而,我回應了SQL查詢,然後粘貼到phpmyadmin中,它完美的工作,但通過PHP結果0記錄。它沒有任何意義,我想不出什麼可能會造成的0

SELECT 
     DISTINCT c.id 
    FROM 
     carpets AS c 
     INNER JOIN carpet_relations AS r1 ON c.id = r1.carpet_id 
     INNER JOIN carpet_relations AS r2 ON c.id = r2.carpet_id 
     INNER JOIN carpet_relations AS r3 ON c.id = r3.carpet_id 
    WHERE 
     c.active = '1' 



     AND ((c.title LIKE '%north tabriz%') OR **(c.title LIKE '%north%') OR (c.title LIKE '%tabriz%')** OR (c.item_no LIKE '%north tabriz%') OR **(c.item_no LIKE '%north%') OR (c.item_no LIKE '%tabriz%')** OR (c.pattern LIKE '%north tabriz%') OR **(c.pattern LIKE '%north%') OR (c.pattern LIKE '%tabriz%')** OR (c.period LIKE '%north tabriz%') OR **(c.period LIKE '%north%') OR (c.period LIKE '%tabriz%')** OR (c.country LIKE '%north tabriz%') **OR (c.country LIKE '%north%') OR (c.country LIKE '%tabriz%')**) 

     AND (c.width_feet BETWEEN '0' AND '22') 
     AND (c.width_inches BETWEEN '0' AND '11') 
     AND (c.height_feet BETWEEN '0' AND '49') 
     AND (c.height_inches BETWEEN '0' AND '11') 
    ORDER BY 
     c.item_no 


id int(11) NO PRI  NULL auto_increment 
active int(11) NO  NULL  
title varchar(250) NO  NULL  
item_no  varchar(250) NO  NULL  
country  varchar(250) NO  NULL  
period varchar(250) NO  NULL  
pattern  varchar(250) NO  NULL  
price float NO  NULL  
web_special  float NO  NULL  
notes text NO  NULL  
width_feet int(11) NO  NULL  
width_inches int(11) NO  NULL  
height_feet  int(11) NO  NULL  
height_inches int(11) NO  NULL  
restrict int(11) NO  NULL  
views_amount int(11) NO  NULL  
last_modified datetime NO  NULL  
modified_by  int(11) NO  NULL 
+0

在您的上下文中是否還有其他更簡單的陳述? (如SELECT * FROM地毯) – djot 2011-12-16 15:53:53

回答

1

結果試試這個:

SELECT 
    DISTINCT c.id 
FROM 
    carpets AS c 
    INNER JOIN carpet_relations AS r1 ON c.id = r1.carpet_id 
    INNER JOIN carpet_relations AS r2 ON c.id = r2.carpet_id 
    INNER JOIN carpet_relations AS r3 ON c.id = r3.carpet_id 
WHERE 
    c.active = '1' 

    AND ((c.title LIKE '%north%') 
    OR (c.title LIKE '%tabriz%') 
    OR (c.item_no LIKE '%north%') 
    OR (c.item_no LIKE '%tabriz%') 
    OR (c.pattern LIKE '%north%') 
    OR (c.pattern LIKE '%tabriz%') 
    OR (c.period LIKE '%north%') 
    OR (c.period LIKE '%tabriz%') 
    OR (c.country LIKE '%north%') 
    OR (c.country LIKE '%tabriz%')) 

    AND (c.width_feet BETWEEN 0 AND 22) 
    AND (c.width_inches BETWEEN 0 AND 11) 
    AND (c.height_feet BETWEEN 0 AND 49) 
    AND (c.height_inches BETWEEN 0 AND 11) 
ORDER BY 
    c.item_no 

我北放棄了「LIKE「%大不里士% '「部分,因爲它們是多餘的,因爲如果它與」北大不列顛「單獨匹配,那麼」北部「和」大巴「也是如此。

另外,我用單引號將兩個子句之間的數字包圍起來。

無法複製表來測試它,因爲你只有1個表顯示,但希望這有助於。

0

這很可能不是查詢,而是PHP代碼中的錯誤。你可以發佈你的PHP代碼讓我們看看嗎?

3

密切關注您如何在PHP中使用引號。對於具有where子句的SQL查詢,由於where子句需要在單引號中聲明WHERE x ='value',因此可能會造成問題。因此,請確保您的整個SQL字符串使用雙引號。此外,單引號中的PHP變量將不會被評估,因此如果您將一個where子句放在PHP變量上,則需要在使用SQL語句之前將該變量包裝在單引號中。我希望所有這些都有道理,當我開始學習PHP/MySQL時,我在這個問題上停留了2天。

1

先嚐試簡單的查詢(測試連接) ,如果他們不工作,看看數據庫主機,用戶名,密碼(他們可能是錯誤地輸入)

一旦確認數據庫連接的,請嘗試使用多查詢在PHP函數,去除不必要的間距,運行在一個查詢for循環或多個步驟

希望這有助於

0

有時我得到這個問題,它發生時,纔會有與連接或或查詢任何問題。請打印您的查詢並查看它如何打印。在查詢中您必須使用$var

0

嘗試在你的php代碼中正確處理(「」)。如果你的查詢在phpMyAdmin中工作正常,那麼它應該在你的php代碼中工作。或者你的數據庫連接可能有一些錯誤。確保你正在選擇你想要的表的正確數據庫。

相關問題