PHP-mySQL“;例如;查询不返回任何内容
我知道这与这里的许多其他帖子是一样的,但我无法理解 我的代码如下所示:PHP-mySQL“;例如;查询不返回任何内容,php,mysql,Php,Mysql,我知道这与这里的许多其他帖子是一样的,但我无法理解 我的代码如下所示: $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_
$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 workbench上测试了我的查询,它工作得很好
在所有三个查询中,我从来没有得到第一个错误日志的日志,而第二个错误每次都对我大叫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;
}
SET NAMES 'utf8' COLLATE 'utf8_general_ci';
utf8_bin(或任何*_bin)使比较区分大小写。如果将连接排序规则设置为“不敏感”,则可以解释脚本和MySQL Workbench之间的差异
无论如何,我会将列排序规则设置为不区分大小写,以避免此类问题。试试这个,它可能会有所帮助
$query ="select id,
cat_id,
book_title,
writer,
publisher,
issue_year,
copies,
abstract
from library
where ".$table." like "%".$search_param."%";
但是它还没有测试。你得到了什么结果?让我们做一些简单的事情,首先
mysql\u query($query)或者die(mysql\u error()
这样我们就可以查看是否有任何错误。@swapnesh$table变量有一个类似于书的标题!@TomMcPadden在我的php脚本上我没有得到任何结果!@GeorgeSfitis所以它是空的?可能是数据库中没有任何内容与查询匹配吗?很抱歉,我得到了你提到的那一行,我只是忘记了在问题上加上它!太简单了好的..你尝试获取数组“while($data=mysql\u fetch\u array($result))”我的表排序规则是utf8-utf8\u bin。关于强制的事情,你能给我一个例子吗?我到现在还没有做过!谢谢。这是我的问题希望这能帮助其他人!