需要从mysql帮助编译数组

时间:2008-10-22 21:22:43

标签: php mysql database arrays

这一直困扰着我,我无法理解它。我将使用食品类比试图简化我的问题。

1000名公众要求从13种足类中挑选各种足类。然后将这些选择存储在mysql数据库中,与其名称相对应。

e.g.    billy   mary   etc.   etc.

milk....semi. .skimmed...

bread...white...brown....

cheese..edam.....edam....

fruit...apple...orange...

veg....potato...sprout...

meat....beef.....beef....

sweet..bonbons..liquorice..

fish...trout....salmon...

crisp....s&v....plain....

biscuit..hovis..rich tea..

wine.....red.....red.....

beer....stella..carlsburg..

carb....coke.....pepsi....

然后要求1000个中的一个通过复选框选择从0到13的任何位置。

通过搜索数据库,有多少人选择了相同的品种?

显示在一个表格中,显示所有13个品种的名称和选择内容。

这有意义吗?我希望如此,因为它让我发疯。

6 个答案:

答案 0 :(得分:2)

假设你有一个简单的布局,那么你会有类似的东西(我将自己限制在三个类别中):

PersonId  What_Milk  What_Bread  What_Cheese
       1  Semi       Wheat       Swiss
       2  Skimmed    Rolls       French
       3  Soy        Brown       Smelly
       4  Low Fat    Wheat       Swiss

如果我理解正确,你的问题是:

当要求人4选择她的食物中的0 .. 3时,她可以选择“面包”和“奶酪”复选框,这意味着查询应该将人1作为匹配。正确?

SELECT
  PersonId,
  What_Milk,
  What_Bread,
  What_Cheese
FROM
  FoodPreference
WHERE
  PersonId != ?
  AND What_Milk   = IFNULL(NULLIF(?, ''), What_Milk)
  AND What_Bread  = IFNULL(NULLIF(?, ''), What_Bread)
  AND What_Cheese = IFNULL(NULLIF(?, ''), What_Cheese)

您的复选框值稍后将转到问号占位符所在的位置。 (我已经替换了以前使用IFNULL / NULLIF的CASE WHEN结构,它具有相同的效果但对PHP预处理语句更友好。

如果未选中复选框(从而将值固定为某些内容),则会将相应列与其自身进行比较。这意味着它的价值不会影响结果。如果其他列匹配,则将选择该行。

这也意味着如果用户选择了复选框,则会返回所有行。一个人选择的食物越多,匹配就越接近。

在PHP中,我建议您使用mysqli_prepare()从查询字符串创建预准备语句,并使用mysqli_stmt_bind_param()将实际值绑定到问号占位符。这比直接构建SQL字符串更安全。 PHP documentation has a whole lot of info on mysqli,请看一下。

答案 1 :(得分:1)

部分答案:

  • 通过排序和连接
  • 组成一系列线性选择
  • 比较然后变成一个简单的WHERE子句

因此,您首先要进行计算运行,将字符串放入某些字段,例如“milk | semi | skimmed”。

答案 2 :(得分:0)

SELECT  
    PersonId,  
    milk,  
    bread,  
    cheese
FROM  FoodPreference
WHERE  PersonId != :chosen_person_id  
    AND $milk= CASE WHEN :isset($_POST["milk"]))> '' THEN :isset($_POST["milk"]))   ELSE milk END  
    AND $bread= CASE WHEN :isset($_POST["bread"]))> '' THEN :isset($_POST["bread"]))  ELSE bread END  
    AND $cheese= CASE WHEN :isset($_POST["cheese"]))> '' THEN :isset($_POST["cheese"])) ELSE cheese END

我是在正确的轨道上吗?

答案 3 :(得分:0)

好的,我发布了一条回复,但Tomalak非常正确地指出了一个合乎逻辑的缺陷。我会再试一次:

将类别列为十三列的替代方法是将它们列为表格中的十三行,如下所示:

CREATE TABLE FoodPreference (
  PersonID   INT NOT NULL REFERENCES People,
  FoodCat    VARCHAR(10) NOT NULL REFERENCES FoodCategories,
  FoodChoice VARCHAR(10) NOT NULL,
  PRIMARY KEY (PersonID, FoodCat)
);
INSERT INTO FoodPreference VALUES
  (123, 'bread', 'white'),
  (123, 'milk', 'skim'),
  (123, 'cheese', 'edam'), ...
  (321, 'bread', 'brown'),
  (321, 'milk', 'whole'),
  (321, 'cheese', 'edam'), ...

然后你可以使用如下的查询,将所选人(p1)中的任何一行与来自另一个人(p2)的相同食物选择的行匹配,并从那里匹配该人的所有其他选择(p3) ):

SELECT DISTINCT p3.*
FROM FoodPreference AS p1
  JOIN FoodPreference AS p2 
    ON (p1.FoodCat = p2.FoodCat AND p1.FoodChoice = p2.FoodChoice 
      AND p1.PersonID != p2.PersonID)
  JOIN FoodPreference AS p3 
    ON (p2.PersonID = p3.PersonID AND p2.FoodCat != p3.FoodCat)
WHERE p1.PersonID = {(int)$chosen_person_id}
  AND p1.FoodCat IN ('milk', 'bread', 'cheese');

根据应用程序中的$_POST变量,WHERE子句中的'milk','bread','cheese'列表是您需要在PHP代码中构建的。如果选中了复选框,请在列表中包含该食品类别。

<?php
$food_cat_array = array("'none'");
$legal_food_cats = array('milk'=>1, 'bread'=>1, 'cheese'=>1, ...);
foreach (array_intersect_key($_POST, $legal_food_cats) as $key => $checked) {
  if ($checked) {
    $food_cat_array[] = "'$key'"; 
  }
}
$in_predicate = join(',', $food_cat_array);

答案 4 :(得分:0)

$sql = mysql_query("        
    SELECT  
    *
    FROM  ".$prefix."_users
    WHERE  username !='$username'  
    AND req_country   = IFNULL(NULLIF($bcountry, ''), req_country)  
    AND req_region  = IFNULL(NULLIF($bregion, ''), req_region)  
    AND req_type = IFNULL(NULLIF($btype, ''), req_type)
    AND req_beds = IFNULL(NULLIF($bbeds, ''), req_beds)
    AND req_value = IFNULL(NULLIF($bvalue, ''), req_value)
    AND country   = IFNULL(NULLIF($scountry, ''), country)  
    AND region  = IFNULL(NULLIF($sregion, ''), region)  
    AND type = IFNULL(NULLIF($stype, ''), type)
    AND beds = IFNULL(NULLIF($sbeds, ''), beds)
    AND value = IFNULL(NULLIF($svalue, ''), value)
    AND pool = IFNULL(NULLIF($spool, ''), 'Yes')
    AND garage = IFNULL(NULLIF($sgarage, ''), >0) 
            AND disabled = IFNULL(NULLIF($sdisabled, ''), 'Yes')");
$num = mysql_num_rows($sql);
echo "Total matches ($num): <br><br>";
        while($row = mysql_fetch_array($sql)){...etc

答案 5 :(得分:0)

case "display_results":

if ($bcountry = !isset($_POST["bcountry"])){
      $bcountry = "No";
      }else {
      $bcountry = "Yes";
      }
if ($bregion = !isset($_POST["bregion"])){
      $bregion = "No";
      }else {
      $bregion = "Yes";
      }
if ($btype = !isset($_POST["btype"])){
      $btype = "No";
      }else {
      $btype = "Yes";
      }
if ($bbeds = !isset($_POST["bbeds"])){
      $bbeds = "No";
      }else {
      $bbeds = "Yes";
      }
if ($bvalue = !isset($_POST["bvalue"])){
      $bvalue = "No";
      }else {
      $bvalue = "Yes";
      }
if ($scountry = !isset($_POST["scountry"])){
      $scountry = "No";
      }else {
      $scountry = "Yes";
      }
if ($sregion = !isset($_POST["sregion"])){
      $sregion = "No";
      }else {
      $sregion = "Yes";
      }
if ($stype = !isset($_POST["stype"])){
      $stype = "No";
      }else {
      $stype = "Yes";
      }
if ($sbeds = !isset($_POST["sbeds"])){
      $sbeds = "No";
      }else {
      $sbeds = "Yes";
      }
if ($svalue = !isset($_POST["svalue"])){
      $svalue = "No";
      }else {
      $svalue = "Yes";
      }
if ($spool = !isset($_POST["spool"])){
      $spool = "No";
      }else {
      $spool = "Yes";
      }
if ($sgarage = !isset($_POST["sgarage"])){
      $sgarage = "No";
      }else {
      $sgarage = "Yes";
      }
if ($sdisabled = !isset($_POST["sdisabled"])){
      $sdisabled = "No";
      }else {
      $sdisabled = "Yes";
      }

     $result = mysql_query("SELECT * FROM  ".$prefix."_users WHERE  username!='$username' 
AND (('$bcountry'='Yes' AND req_country= '$country') OR ('$bcountry'='No')) 
AND (('$bregion'='Yes' AND req_region= '$region') OR ('$bregion'='No'))
AND (('$btype'='Yes' AND req_type= '$type') OR ('$btype'='No'))
AND (('$bbeds'='Yes' AND req_beds= '$beds') OR ('$bbeds'='No'))
AND (('$bvalue'='Yes' AND req_value= '$value') OR ('$bvalue'='No'))
AND (('$scountry'='Yes' AND country= '$req_country') OR ('$scountry'='No'))
AND (('$sregion'='Yes' AND region= '$req_region') OR ('$sregion'='No'))
AND (('$stype'='Yes' AND type= '$req_type') OR ('$stype'='No'))
AND (('$sbeds'='Yes' AND beds= '$req_beds') OR ('$sbeds'='No'))
AND (('$svalue'='Yes' AND value= '$req_value') OR ('$svalue'='No'))
AND (('$spool'='Yes' AND pool= 'Yes') OR ('$spool'='No'))
AND (('$sgarage'='Yes' AND garage>=1 ) OR ('$sgarage'='No'))
AND (('$sdisabled'='Yes' AND disabled= 'Yes') OR ('$sdisabled'='No'))
")or die("MySQL ERROR: ".mysql_error());
$number = mysql_num_rows($result);
相关问题