1

我知道这与这里的许多其他帖子相同,但我无法弄清楚!

我的代码如下所示:

$i=0;
$shelves = array();
$shelves['position'] = array();
$query = "select id, cat_id, book_title, writer, publisher, issue_year, copies, abstract from library where $table like '%$search_param%'";
$result = mysql_query($query);
while ( $data = mysql_fetch_assoc($result) ) {
   error_log($data['id']);
   $shelves['position'][$i]['id'] = $data['id'];
   $shelves['position'][$i]['cat_id'] = $data['cat_id'];
   $shelves['position'][$i]['book_title'] = $data['book_title'];
   $shelves['position'][$i]['writer'] = $data['writer'];
   $shelves['position'][$i]['publisher'] = $data['publisher'];
   $shelves['position'][$i]['issue_year'] = $data['issue_year'];
   $shelves['position'][$i]['copies'] = $data['copies'];
   $shelves['position'][$i]['abstract'] = $data['abstract'];
   ++$i;
}
error_log( count($shelves['position']) );

而且因为有像这样的其他帖子的音调,我尝试了他们的解决方案:

$query = sprintf("select id, cat_id, book_title, writer, publisher, issue_year, copies, abstract from library where %s like'%%%s%'",mysql_real_escape_string($table),mysql_real_escape_string($search_param) );

或类似的东西:

$query = "select id, cat_id, book_title, writer, publisher, issue_year, copies, abstract from library where $table like '%{$search_param}%'";

我也尝试在没有动态变量的情况下运行查询,而我得到了同样的结果。

$query = "select id, cat_id, book_title, writer, publisher, issue_year, copies, abstract from library where book_title like '%lord%'";

似乎没有任何效果。

我已经在 mysql 工作台上测试了我的查询,它就像一个魅力!

在所有三个查询中,我从来没有得到第一个 error_log 的日志,而第二个每次都对我大喊 0!

有人可以照亮路吗?

4

3 回答 3

2

好吧,这里唯一可疑的是您正在使用的字符集/排序规则,这可能会导致区分大小写的问题。尝试强制进行非敏感排序,看看会发生什么。

SET NAMES 'utf8' COLLATE 'utf8_general_ci';

一个 utf8_bin (或任何 *_bin )使比较区分大小写。如果将连接排序规则设置为不敏感的作品,那将解释您的脚本和 MySQL Workbench 之间的区别。

无论如何,我会将排序规则设置为不区分大小写以避免此类问题。

于 2013-07-11T04:14:53.553 回答
0
The query getting correct answer.. 

Try this:

$i=0; // before start while loop,you need to initialize i

while ( $data = mysql_fetch_assoc($result) ) {
   error_log($data['id']);
   $shelves['position'][$i]['id'] = $data['id'];
   $shelves['position'][$i]['cat_id'] = $data['cat_id'];
   $shelves['position'][$i]['book_title'] = $data['book_title'];
   $shelves['position'][$i]['writer'] = $data['writer'];
   $shelves['position'][$i]['publisher'] = $data['publisher'];
   $shelves['position'][$i]['issue_year'] = $data['issue_year'];
   $shelves['position'][$i]['copies'] = $data['copies'];
   $shelves['position'][$i]['abstract'] = $data['abstract'];
   ++$i;
}
于 2013-07-11T04:04:42.417 回答
0

试试这个可能会有所帮助。

$query ="select id,
                cat_id, 
                book_title,
                writer, 
                publisher, 
                issue_year, 
                copies, 
                abstract 
                from library 
                where ".$table." like "%".$search_param."%";

但它还没有经过测试。

于 2013-07-11T04:52:25.630 回答