请问这段多条件搜索代码为什么搜不出结果
程序员文章站
2022-04-26 13:37:11
...
请教这段多条件搜索代码为什么搜不出结果
数据库里有记录,但查询响应的记录时,却显示“数据库查询失败!
可能数据库中没有记录”,请教这是什么问题?
搜索代码:
查询代码:
$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 终端去执行一下看看是否能正常的返回结果.
------解决方案--------------------
数据库里有记录,但查询响应的记录时,却显示“数据库查询失败!
可能数据库中没有记录”,请教这是什么问题?
搜索代码:
查询代码:
$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 '
}
?>
------解决方案--------------------
$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 终端去执行一下看看是否能正常的返回结果.
------解决方案--------------------
相关文章
相关视频
上一篇: PHP获取随机数字和字母的方法大全