Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP查询正确,但结果不匹配?_Php_Mysql - Fatal编程技术网

PHP查询正确,但结果不匹配?

PHP查询正确,但结果不匹配?,php,mysql,Php,Mysql,我只有两个数据库,它们都通过相同的select脚本运行。该脚本对其中一行非常有效,但另一行只显示数据库的前三行,并重复显示它们 由于我是PHP新手,我无法想象它为什么适用于一个数据库而不适用于另一个数据库。我四次检查了我所有的拼写,并重复了查询脚本以确保正确。更糟糕的是,浏览器没有给我一个错误作为线索。它只是显示了错误的信息 请帮忙。谢谢你 html这是表单…我不认为问题在这里…但我只是为了以防万一…我认为问题一定在product_list.php中 <!DOCTYPE html>

我只有两个数据库,它们都通过相同的select脚本运行。该脚本对其中一行非常有效,但另一行只显示数据库的前三行,并重复显示它们

由于我是PHP新手,我无法想象它为什么适用于一个数据库而不适用于另一个数据库。我四次检查了我所有的拼写,并重复了查询脚本以确保正确。更糟糕的是,浏览器没有给我一个错误作为线索。它只是显示了错误的信息

请帮忙。谢谢你

html这是表单…我不认为问题在这里…但我只是为了以防万一…我认为问题一定在product_list.php中

<!DOCTYPE html>
<html>
        <head><title>Databases</title></head>
        <body>
                <h1>Music Store Database</h1>
                <form method='POST' action='display.php'
                        <label>Select a table:</label>
                        <select name="tableName">
                                <option value="products">Products</option>
                                <option value="categories">Categories</option>
                        </select>
                        <p>To ADD: Enter the field values(s)  below for the record(s) you want to add.  NOTE:  Adding a record requires all appropriate field for the selected table.<p>
                        <p>To DELETE:  Enter the value for the field you are going to use to identify the desired record(s).  Then select that field from "delete record(s)" section below.</$
                                <label>ProductID</label>
                                <input type="text" name="productIDtx" value=""/></br>
                                <label>CategoryID</label>
                                <input type="text" name="categoryIDtx" value=""/></br>
                                <label>Product Code</label>
                                <input type="text" name="productCodetx" value=""/></br>
                                <label>Product Name</label>
                                <input type="text" name="productNametx" value=""/></br>
                                <label>List Price</label>
                                <input type="text" name="listPricetx" value=""/></br>
                                <label>Category Name</label>
                                <input type="text" name="categoryNametx" value=""/></br>
                        <p>Delete Record(s):  Select desired field below.  Don't forget to complete the information for the record in question above:</p>
                                <input type="radio" name="remove" value="productID"/>
                                <label>ProductID</label><br />
                                <input type="radio" name="remove" value="categoryID"/>
                                <label>CategoryID</label><br />
                                <input type="radio" name="remove" value="productCode"/>
                                <label>Product Code</label><br />
                                <input type="radio" name="remove" value="productName"/>
                                <label>Product Name</label><br />
                                <input type="radio" name="remove" value="listPrice"/>
                                <label>List Price</label><br />
                                <input type="radio" name="remove" value="categoryName"/>
                                <label>Category Name</label><br />
                        <p>To Retrieve Record(s): Select field(s) below you want to see from the list below.</p>
                                <input type="checkbox" name="productIDcb"/>
                                <label>ProductID</label><br />
                                <input type="checkbox" name="categoryIDcb"/>
                                <label>CategoryID</label><br />
                                <input type="checkbox" name="productCodecb"/>
                                <label>Product Code</label><br />
                                <input type="checkbox" name="productNamecb"/>
                                <label>Product Name</label><br />
                                <input type="checkbox" name="listPricecb"/>
                                <label>List Price</label><br />
                                <input type="checkbox" name="categoryNamecb"/>
                                <label>Category Name</label><br />
                        <p>Select the appropriate action based on your selection from above:</p>
                                <input type="radio" name="operation" value="retrieve"/>
                                <label>Retrieve Information</label><br />
                                <input type="radio" name="operation" value="addition"/>
                                <label>Add Information</label><br />
                                <input type="radio" name="operation" value="delete"/>
                                <label>Delete Information</label><br />
                        <p><input type="submit" value="Submit Request"/></p>
                </form>
       </body>
</html>
php这会从我的表单中获取信息并调用函数…仍然包括以防万一,但我想问题一定出在下一个文件中

<?php
  require('database.php');

  $productIDtx = $_POST['productIDtx'];
  $categoryIDtx = $_POST['categoryIDtx'];
  $productCodetx = $_POST['productCodetx'];
  $productNametx = $_POST['productNametx'];
  $listPricetx = $_POST['listPricetx'];
  $categoryNametx = $_POST['categoryNametx'];


  if(isset($_POST['tableName']))
   {
        $table = $_POST['tableName'];
   }
  else
   {
        echo("Must select a table.<br>");
   }//endif

  if(isset($_POST['operation']))
   {
        $operation = $_POST['operation'];
   }
  else
   {
        echo("Must select an action.<br>");
        exit();
   }//endif

   if(isset($_POST['remove']))
   {
        $remove = $_POST['remove'];
   }
   else
   {
        $remove = "";
   }//endif

   if(isset($_POST['productIDcb']))
   {
        $productIDcb = $_POST['productIDcb'];
   }
   else
   {
        $productIDcb = "";
   }//endif

   if(isset($_POST['categoryIDcb']))
   {
        $categoryIDcb = $_POST['categoryIDcb'];
   }
   else
   {
        $categoryIDcb = "";
   }//endif

   if(isset($_POST['productCodecb']))
   {
        $productCodecb = $_POST['productCodecb'];
   }
 else
   {
        $productCodecb = "";
   }//endif

   if(isset($_POST['productNamecb']))
   {
        $productNamecb = $_POST['productNamecb'];
   }
   else
   {
        $productNamecb = "";
   }//endif

   if(isset($_POST['listPricecb']))
   {
        $listPricecb = $_POST['listPricecb'];
   }
   else
   {
        $listPricecb = "";
   }//endif

   if(isset($_POST['categoryNamecb']))
   {
        $categoryNamecb = $_POST['categoryNamecb'];
   }
   else
   {
        $categoryNamecb = "";
   }//endif


  if($operation == 'retrieve')
        {
                include_once('product_list.php');
                show_products($productIDcb, $categoryIDcb, $productCodecb, $productNamecb, $listPricecb, $categoryNamecb);
        }
  elseif($operation == 'addition')
        {
                include_once('addprod.php');
                add($table, $productIDtx, $categoryIDtx, $productCodetx, $productNametx, $listPricetx, $categoryNametx);
        }
  elseif($operation == 'delete')
        {
                include_once('deleteprod.php');
                delete($table, $remove, $productIDtx, $categoryIDtx, $productCodetx, $productNametx, $listPricetx, $categoryNametx);
        }
  else
        {
                echo('<p>Select an action:  Retrieve, Add, or Delete. </p>');  //this code shouldn't ever happen because $operation is tested above but I put it in here in case an errors at this point in the code.
                exit();
        }//endif

?>
product_list.php我认为问题一定出在$rSET或之后,因为$theQuery的回音显示正确。但这对另一个数据库有效。所以我不知所措

<?php
  include('database.php');


  function show_products($productIDcb, $categoryIDcb, $productCodecb, $productNamecb, $listPricecb, $categoryNamecb)
  {
        global $db;
        $theQuery = 'select ';
        $list = "";
        if($productIDcb == "")
         {
                $theQuery == $theQuery;
         }
        else
         {
                $theQuery .= 'p.productID, ';
         }//endif

        if($categoryIDcb == "")
         {
                $theQuery == $theQuery;
         }//endif

        else
         {
                $theQuery .= 'c.categoryID, ';

         }//endif

        if($productCodecb == "")
         {
                $theQuery == $theQuery;
         }
        else
         {
                $theQuery .= 'p.productCode, ';

         }//endif

        if($productNamecb == "")
         {
                $theQuery == $theQuery;
         }
        else
         {
                 $theQuery .= 'p.productName, ';
         }//endif

        if($listPricecb == "")
         {
                $theQuery == $theQuery;
         }
        else
         {
                $theQuery .= 'p.listPrice, ';
         }//endif

        if($categoryNamecb == "")
         {
                $theQuery == $theQuery;
         }
        else
         {
                $theQuery .= 'c.categoryName, ';
         }//endif

        $theQuery .=" '' from (categories c, products p) where (c.categoryID = p.categoryID);";
        echo($theQuery);
        echo('<br>');

        //***I THINK THE ISSUE MUST BE SOMEWHERE AFTER THIS***

        $rSet = $db -> query($theQuery);
        foreach($rSet AS $results)
          {
              $list .=' '.$results[0];
                if(isset($results[1]))
                {
                   $list .=' '.$results[1];
                }
                if(isset($results[2]))
                {
                   $list .=' '.$results[2];
                }
                if(isset($results[3]))
                {
                   $list .=' '.$results[3];
                }
                if(isset($products[4]))
                {
                   $list .=' '.$results[4];
                }
              $list .="<br>";
          }//end foreach
        echo($list);
        echo('<br>');
        echo('<a href="index.html">Music Store Database</a>');

}//end function
?>
产品数据库查询这一个工作

从类别c、产品p中选择p.productID、c.categoryID、p.productCode、p.productName、p.listPrice,其中c.categoryID=p.categoryID

1 1斯特拉特挡泥板平铺脚轮

2 1 les_paul Gibson les paul

3 1吉布森sg

4 1 fg700s雅马哈fg700s

5 1 washburn washburn D10S

6 1罗德里格斯·罗德里格斯·卡巴莱罗11

7.2精度翼子板精度

8.2霍夫纳-霍夫纳图标

9 3路德维希路德维希5件套鼓,带钹

10 3塔玛塔玛5件套鼓,带钹

类别数据库查询查询看起来正确,但数据错误

从c类产品p中选择c.categoryID,c.categoryName,其中c.categoryID=p.categoryID

1把吉他

1把吉他

1把吉他

1把吉他

1把吉他

1把吉他

2贝斯

2贝斯

3个鼓

3个鼓

它应该列出以下内容

1把吉他

2贝斯

3个鼓

11试验

15试验

20试验

33测试33

40试验


注意:这些测试是在我测试add函数时进行的,以防您怀疑。

您的查询将导致笛卡尔乘积,它将返回重复的结果。请尝试使用内部联接或交叉联接


感谢所有在上面发表评论的人让我明白这一点。正如andrewsi指出的,SELECT DISTINCT代码确实解决了重复的问题,但是由于c.categoryID=p.categoryID,整个categories表不会显示。因此,我添加了和if-else语句来处理这两种情况,这也消除了对区分的需要。这是修复它的代码

if($theQuery == 'select c.categoryID, c.categoryName, ')
        {
                $theQuery = "";
                $theQuery = "select * from categories;";
        }
        else
        {
                $theQuery .=" '' from (categories c, products p) WHERE (c.categoryID = p.categoryID);";
        }

你能显示database.php文件的内容吗?你能回显你正在生成的SQL吗?它既适用于你想要的查询,也适用于其中一个失败的查询。我直接从mysql添加了一个数据库副本,但看起来相当混乱。让我看看能不能换个方法。我还将展示这两个语句。谢谢,请稍等。请打印\r$rSet并告诉我您得到了什么。@user3025217-我想您可能需要在查询中执行显式连接。如果您尝试在数据库中运行正确的查询,它是否也会产生相同的结果?如果您尝试选择categoryID,categoryName从类别c内部连接产品p到c.categoryID=p.categoryID,会怎么样?我下次会尝试。谢谢目前,我只是使用if语句来处理这种情况。
if($theQuery == 'select c.categoryID, c.categoryName, ')
        {
                $theQuery = "";
                $theQuery = "select * from categories;";
        }
        else
        {
                $theQuery .=" '' from (categories c, products p) WHERE (c.categoryID = p.categoryID);";
        }