日期:2014-05-17  浏览次数:20705 次

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

搜索代码:
  <form id="form1" name="form1" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  搜索:
  <label>
  <select name="mode" id="S_mode">
  <option value="S_all" selected="selected">全部</option>
  <option value="S_name">供应商</option>
  <option value="S_birthday">生日</option>
  <option value="S_qq">QQ</option>
  <option value="S_mobile">手机</option>
  <option value="S_email">邮箱</option>
  <option value="S_address">地址</option>
  </select>
  </label>
  <label>
  <input name="word" type="text" id="S_word" />
  </label>
  <label>
  <input type="submit" name="Submit" value="搜索" />
  </label>
  </form>

查询代码:
<?php
$word = $_GET['word']; //获得关键字
$mode = $_GET['mode']; //获取搜索方式
if(!empty($word)) {
  echo '<title>可以查询</title>';
  echo '您查询的关键词是:“',$word,'”<br/>';
$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('数据库查询失败!<br/>可能数据库中没有记录'); //SQL语句在这里执行
if($result) 
{
echo $result;
}
}
else
{
  echo '<title>',$word,'</title>';
}
?>

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

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


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

------解决方案--------------------
先echo $sql; 查询语句到mysql 终端去执行一下看看是否能正常的返回结果.
------解决方案--------------------