Home > Backend Development > PHP Tutorial > 请问这段多条件搜索代码为什么搜不出结果

请问这段多条件搜索代码为什么搜不出结果

WBOY
Release: 2016-06-13 13:39:01
Original
792 people have browsed it

请教这段多条件搜索代码为什么搜不出结果
数据库里有记录,但查询响应的记录时,却显示“数据库查询失败!
可能数据库中没有记录”,请教这是什么问题?

搜索代码:
 


  搜索:
 
 
 
 


查询代码:
$word = $_GET['word']; //获得关键字
$mode = $_GET['mode']; //获取搜索方式
if(!empty($word)) {
  echo '可以查询';
  echo '您查询的关键词是:“',$word,'”
';
$sql = "SELECT * FROM ".SqlTableName;
switch($mode)
{
case 'S_all': $sql = 'SELECT * FROM `'.SqlDataBase.'`.`'.SqlTableName."` 
WHERE `Name` LIKE '%$word%' OR `Birthday` LIKE '%$word%'
OR `QQ` LIKE '%$word%' OR `Mobile` LIKE '%$word%' 
OR `Email` LIKE '%$word%' OR `Address` LIKE '%$word%'";break;

case 'S_name': $sql = 'SELECT * FROM `'.SqlDataBase.'`.`'.SqlTableName."` 
WHERE `Name` LIKE '%$word%'"; break;
case 'S_birthday': $sql = 'SELECT * FROM `'.SqlDataBase.'`.`'.SqlTableName."` 
WHERE `Birthday` LIKE '%$word%'"; break;
case 'S_qq': $sql = 'SELECT * FROM `'.SqlDataBase.'`.`'.SqlTableName."` 
WHERE `QQ` LIKE '%$word%'"; break;
case 'S_mobile': $sql = 'SELECT * FROM `'.SqlDataBase.'`.`'.SqlTableName."` 
WHERE `Mobile` LIKE '%$word%'"; break;
case 'S_email': $sql = 'SELECT * FROM `'.SqlDataBase.'`.`'.SqlTableName."` 
WHERE `Email` LIKE '%$word%'"; break;
case 'S_address': $sql = 'SELECT * FROM `'.SqlDataBase.'`.`'.SqlTableName."`
  WHERE `Address` LIKE '%$word%''; break;
default: $sql = "SELECT * FROM `".SqlTableName."`";
}
require('conn.php'); //调用conn.php文件,执行数据库操作
  $result = mysqli_query($conn,$sql) or die('数据库查询失败!
可能数据库中没有记录'); //SQL语句在这里执行
if($result) 
{
echo $result;
}
}
else
{
  echo '',$word,'';
}
?>

------解决方案--------------------
$sql = 'SELECT * FROM `mycg`.`providers` WHERE `Address` LIKE '%$word%''
改成
$sql = “SELECT * FROM `mycg`.`providers` WHERE `Address` LIKE '%$word%'”
------解决方案--------------------
将 die('数据库查询失败!
可能数据库中没有记录');
改成 die("$sql
" . mysql_error());

这样在出错时就可知道为什么错了


switch 写的太乱,不便于检查正确性

------解决方案--------------------
先echo $sql; 查询语句到mysql 终端去执行一下看看是否能正常的返回结果.
------解决方案--------------------
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template