2012-09-17 89 views
0

我有這樣的代碼:MySQL查詢在網站無法正常工作,工作在MySQL程序

mysql_query(" 
    SELECT id as tc_id, firstname, surname, position, @concat := CONCAT(firstname,' ',surname) 
    FROM tc 
    WHERE (@concat LIKE '%". mysql_real_escape_string($_REQUEST['search_term']) ."%');"); 

SEARCH_TERM只是指文本輸入的值。

當我在本地sequel pro中運行SELECT語句 - 我的mysql程序 - 它完美的工作,選擇適當的行。當我通過我的PHP頁面在本地運行查詢時,它不返回行,即使它應該。幫助任何人?

+0

您能剖析mySql實例以查看哪些命令到達了它? (在MS SQL中稱爲「分析」,不確定mySql抱歉)。 – Sepster

+0

根本沒有連接或者沒有連接到您認爲自己的數據庫。 – duffymo

+2

這是什麼意思它返回「無行」?您是否執行了聲明或僅聲明瞭聲明?喲取得任何行?請看看我的答案[這裏](http://stackoverflow.com/questions/12367798/mysql-function-not-working/12367856#12367856) – DonCallisto

回答

1

WHERE不計算列無效,因此您的查詢,本身將無法​​正常工作

它可能是有效的,因爲你在本地有一個@concat變量聲明。如果您在特定條件下運行查詢兩次,它也會出現,因爲您的查詢可能實際上分配了一個@concat變量。

你想要的是

SELECT id as tc_id, firstname, surname, position, 
    CONCAT(firstname,' ',surname) AS concat 
FROM tc HAVING concat LIKE '%<YOUR SEARCH TERM>%'; 

由於測試:

-- Declare a minimum table to match the query 
CREATE TABLE tc (id integer, firstname varchar(20), surname varchar(20), position integer); 

INSERT INTO tc (firstname, surname) VALUES ('alfa', 'bravo'); 

-- Your query... 

SELECT id as tc_id, firstname, surname, position, 
    @concat := CONCAT(firstname,' ',surname) FROM tc WHERE (@concat LIKE '%alfa%'); 

-- ...returns nothing 

Empty set (0.00 sec) 

-- The proper query works. 

SELECT id as tc_id, firstname, surname, position, CONCAT(firstname,' ',surname) AS concat FROM tc HAVING concat LIKE '%alfa%'; 
+-------+-----------+---------+----------+------------+ 
| tc_id | firstname | surname | position | concat  | 
+-------+-----------+---------+----------+------------+ 
| NULL | alfa  | bravo |  NULL | alfa bravo | 
+-------+-----------+---------+----------+------------+ 
1 row in set (0.00 sec) 

-- But if I declare a @concat variable  

SELECT @concat := 'alfa'; 
+-------------------+ 
| @concat := 'alfa' | 
+-------------------+ 
| alfa    | 
+-------------------+ 
1 row in set (0.00 sec) 

-- Then your query SEEMS to work. 

mysql> SELECT id as tc_id, firstname, surname, position, @concat := CONCAT(firstname,' ',surname) FROM tc WHERE (@concat LIKE '%alfa%'); 
+-------+-----------+---------+----------+------------------------------------------+ 
| tc_id | firstname | surname | position | @concat := CONCAT(firstname,' ',surname) | 
+-------+-----------+---------+----------+------------------------------------------+ 
| NULL | alfa  | bravo |  NULL | alfa bravo        | 
+-------+-----------+---------+----------+------------------------------------------+ 
1 row in set (0.00 sec) 

-- "SEEMS" because the select query isn't actually working: 

UPDATE tc SET firstname = 'delta'; 
Query OK, 1 row affected (0.28 sec) 
Rows matched: 1 Changed: 1 Warnings: 0 

-- Having renamed the only row to "delta", a search for "alpha" should fail, 
-- but since @concat still holds 'alpha', then the query matches ALL rows: 

mysql> SELECT id as tc_id, firstname, surname, position, @concat := CONCAT(firstname,' ',surname) FROM tc WHERE (@concat LIKE '%alfa%'); 
+-------+-----------+---------+----------+------------------------------------------+ 
| tc_id | firstname | surname | position | @concat := CONCAT(firstname,' ',surname) | 
+-------+-----------+---------+----------+------------------------------------------+ 
| NULL | delta  | bravo |  NULL | delta bravo        | 
+-------+-----------+---------+----------+------------------------------------------+ 
1 row in set (0.00 sec) 
+0

這確實是問題。謝謝lserni。 – user1570747

0

試試這個:

mysql_query(" 
    SELECT id as tc_id, firstname, surname, position, CONCAT(firstname,' ',surname) as concatt 
    FROM tc 
    WHERE (CONCAT(firstname,' ',surname) LIKE '%". mysql_real_escape_string($_REQUEST['search_term']) ."%');"); 
0

這是因爲你的續集專業翻譯查詢語句成爲MySQL查詢,而MySQL不明白什麼意思約「,其中@concat喜歡...」。

試着改變你的where子句中的PHP代碼變得「裏CONCAT(名字,」 '姓)LIKE' %......」

波紋管的完整代碼:

mysql_query("SELECT id as tc_id, firstname, surname, position, CONCAT(firstname,' ',surname) FROM tc WHERE (CONCAT(firstname,' ',surname) LIKE '%". mysql_real_escape_string($_REQUEST['search_term']) ."%');");