Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.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/4/oop/2.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中限制内部SQL查询_Php_Mysql_Xml_Xmlwriter - Fatal编程技术网

不知道如何在PHP中限制内部SQL查询

不知道如何在PHP中限制内部SQL查询,php,mysql,xml,xmlwriter,Php,Mysql,Xml,Xmlwriter,到目前为止,大量的S/O搜索非常有用。。。下面的方法几乎可以奏效。我的问题是,第二个SQL查询返回每个“配方”的所有“配料”——而配方1有配料A、C、F,配方2有配料A、G、H。如何限制第二个查询只返回当前循环中配方的配料?我将非常感谢PHP/SQL向导的帮助,帮助我克服困难 具有多对多关系的数据库结构: 配方、名称 记录接收,输入 配料ingid,配料假设recid是配方的id。 试试这个: <?php $servername = "localhost"; $username = "ro

到目前为止,大量的S/O搜索非常有用。。。下面的方法几乎可以奏效。我的问题是,第二个SQL查询返回每个“配方”的所有“配料”——而配方1有配料A、C、F,配方2有配料A、G、H。如何限制第二个查询只返回当前循环中配方的配料?我将非常感谢PHP/SQL向导的帮助,帮助我克服困难

具有多对多关系的数据库结构: 配方、名称 记录接收,输入
配料ingid,配料假设recid是配方的id。 试试这个:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "recipe";
try {
  $db = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
}
catch(PDOException $e) {
   echo "Error: " . $e->getMessage();
}
$xml = new XMLWriter();
$xml->openURI('stackexch.xml');
$xml->setIndent(2);
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement('recipes');
$recipe = $db->query("SELECT * FROM recipe");
foreach ($recipe as $row) {
  $xml->startElement('recipe');
  $xml->startElement('title');
  $xml->writeRaw($row['title']);
  $xml->endElement();
  $ingredient = $db->query("SELECT ingredient FROM recipe, rec_ing, ingredient WHERE recipe.recid=rec_ing.recid AND ingredient.ingid=rec_ing.ingid AND recipe.recid = " . $row['recid']);
  foreach ($ingredient as $subrow) {
    $xml->startElement('ingredient');
    $xml->writeRaw($subrow['ingredient']);
    $xml->endElement();
    }
  $xml->endElement();
}
$xml->endElement();
$xml->endDocument();
$xml->flush();
?>
由于您已经从配方表中进行了选择,因此无需再次从中进行选择。 您可能可以这样简化代码:

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "recipe";
    try {
      $db = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    }
    catch(PDOException $e) {
       echo "Error: " . $e->getMessage();
    }
    $xml = new XMLWriter();
    $xml->openURI('stackexch.xml');
    $xml->setIndent(2);
    $xml->startDocument('1.0', 'UTF-8');
    $xml->startElement('recipes');
    $recipe = $db->query("SELECT * FROM recipe");
    foreach ($recipe as $row) {
      $xml->startElement('recipe');
      $xml->startElement('title');
      $xml->writeRaw($row['title']);
      $xml->endElement();
      $ingredient = $db->query("SELECT ingredient FROM rec_ing, ingredient WHERE ingredient.ingid=rec_ing.ingid AND rec_ing.recid = " . $row['recid']);
      foreach ($ingredient as $subrow) {
        $xml->startElement('ingredient');
        $xml->writeRaw($subrow['ingredient']);
        $xml->endElement();
        }
      $xml->endElement();
    }
    $xml->endElement();
    $xml->endDocument();
    $xml->flush();
    ?>

通常,我不建议将变量连接到查询中,但在这种情况下是安全的,因为您已经从数据库中选择了id。

这会消耗资源:

SELECT * FROM recipe
SELECT r.title, i.ingredient 
FROM recipe r
INNER JOIN rec_ing ON r.recid=rec_ing.recid 
INNER JOIN ingredient i ON i.ingid=rec_ing.ingid
但是您只使用[title],那么为什么要拉所有列呢

这将针对上面返回的每一行执行:

SELECT ingredient 
FROM recipe, rec_ing, ingredient 
WHERE recipe.recid=rec_ing.recid AND ingredient.ingid=rec_ing.ingid
但您可以在一个查询中同时执行上述两项操作,因此消耗的资源更少:

SELECT * FROM recipe
SELECT r.title, i.ingredient 
FROM recipe r
INNER JOIN rec_ing ON r.recid=rec_ing.recid 
INNER JOIN ingredient i ON i.ingid=rec_ing.ingid
还要注意的是,超过25年前,ANSI规范化了一组您应该尝试采用的连接。2个提示:

拒绝在from子句中的表名之间使用逗号,以及 如果一个条件有两个表引用,则该条件属于联接,例如recipe.recid=rec_ing.recid请参见?在equals的每一侧都有一个表引用,这是一个连接条件 最后一个建议:SQL查询不必是PHP代码中的一行

欢迎来到Stackoverflow。 您需要添加 RECIPE.recid='.$row['recid']在where子句中

然而,从我的角度来看,您可以只使用一个查询来简化逻辑。我建议使用内部联接来包含来自多个表的信息

SELECT *
FROM recipe AS r
INNER JOIN rec_ing AS ri ON ri.recid=r.recid
INNER JOIN INGREDIENT AS i ON i.ingid=ri.ingid;

Jonhasacat,你是我今天的英雄。简单而精彩的解决方案。丹克,谢谢你,埃米利奥!我的SQL已经生锈了,但这看起来很棒。我明天会试试,然后再报告或者要求更多的澄清。非常感谢。我是一个新成员,但我已经潜伏了一段时间-这是一个了不起的社区,我希望能向前支付。真棒,感谢这些伟大的提示。我明天会玩,学习,然后汇报。非常感谢!