0

私はこのコードを持っています:

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ページを介してローカルでクエリを実行すると、行は返されますが、返されません。誰かを助けますか?

4

3 に答える 3

1

WHERE計算列では機能しないため、クエリ自体は機能しません

@concatローカルで変数が宣言されているため、おそらく機能します。クエリが実際に変数を割り当てる可能性があるため、特定の条件でクエリを2回実行した場合にも機能するように見えます。@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)
于 2012-09-17T10:39:47.607 に答える
0

これは、Sequel ProがクエリステートメントをMySqlクエリに変換し、MySqlが「where@concatLIKE...」の意味を理解していないためです。

PHPコードのwhere句を「whereCONCAT(firstname、''、surname)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']) ."%');");
于 2012-09-17T10:43:47.703 に答える
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']) ."%');");
于 2012-09-17T10:39:14.060 に答える