首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在php mysql中没有显示任何结果之间

在php mysql中没有显示任何结果之间
EN

Stack Overflow用户
提问于 2014-07-11 21:51:55
回答 1查看 87关注 0票数 0

在我的程序中,有一系列的字段,我需要根据输入的范围从查询中获得结果。

在下面的代码中,有多个输入,它们是颜色、清晰度、形状、切割。在下面的php代码中,我从用户那里获取范围,然后使用mysql查询来根据查询查找所有可能的结果。

例如,在carat字段中,包含从1到25的值,如果用户输入2到10,则数据库应显示从2到10的所有字段。其他字段也是如此。

字段颜色由D、E、F、G、H、I、J、K等值组成。清晰度由FL、IF、VVS1、VVS2、VS1、VS2、SL1、SL2等值组成。

我希望我的查询找到给定范围内的所有结果,然后显示结果。我的问题是,尽管数据库中有确切的数据,但没有显示任何内容。

数据库的所有字段都是varchar,只有carat是数值型。

代码语言:javascript
复制
<?php
$hostname_localhost ="localhost";
$database_localhost ="testdb";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);

 mysql_select_db($database_localhost, $localhost);

$carat1 = $_POST['carat1'];
$carat2 = $_POST['carat2'];
$clarity1 = $_POST['clarity1'];
$clarity2 = $_POST['clarity2'];
$color1 = $_POST['color1'];
$color2 = $_POST['color2'];
$cut1 = $_POST['cut1'];
$cut2 = $_POST['cut2'];
$shape1 = $_POST['shape1'];
$shape2 = $_POST['shape2'];
$stones = $_POST['stones'];


$query_search ="Select * from search1 where carats Between '$carat1' and '$carat2' and 
color Between'$color1' and  '$color2' and cut Between '$cut1' and '$cut2' and shape Between '$shape1' and  '$shape2' and stone ='$stones' ";

$query_exec = mysql_query($query_search) or die(mysql_error());


    while($row=mysql_fetch_assoc($query_exec))
            $json_output[]=$row;
      print(json_encode($json_output));

    mysql_close();

//$rows = mysql_num_rows($query_exec);
//echo $rows;


 //while($row=mysql_fetch_assoc($query_exec)) { $json_output[]=$row; } echo json_encode($json_output);
?>

如果我将查询改为,那么它会给出结果,但它不是范围。

代码语言:javascript
复制
$query_search ="Select * from search1 where carats Between '$carat1' and '$carat2' and 
color = '$color1' or color =  '$color2' and cut = '$cut1' or cut = '$cut2' and shape = '$shape1' or shape =  '$shape2' and stone ='$stones' ";
EN

回答 1

Stack Overflow用户

发布于 2014-07-11 22:19:09

我看到的一个问题是,carat字段可能是db上的一个数字(因为您说它是一个数值)。如果是这样,那么在您的查询中应该包含以下内容:

代码语言:javascript
复制
... where carats Between $carat1 and $carat2 ...

不带引号。

还可以尝试使用括号修复查询:

代码语言:javascript
复制
$query_search ="Select * from search1 where (carats Between '$carat1' and '$carat2') and (color Between'$color1' and  '$color2') and (cut Between '$cut1' and '$cut2') and (shape Between '$shape1' and '$shape2') and stone ='$stones' ";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24699388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档