MySQL查询中的PHP变量

时间:2012-10-20 07:03:43

标签: php mysql

我的代码无效

  <body>
<?php $outerSql = mysql_query("select * from gree_menu"); ?>
<ul>
  <?php
    while($outerRow = mysql_fetch_array($outerSql)) {
      $outerMenu = $outerRow['menu_name'];
      $outerId = $outerRow['menu_id'];
    ?>
      <li>
        <?=$outerMenu; ?>
        <?php $innerSql = mysql_query("SELECT sp.* FROM gree_menu gm INNER JOIN silicon_prod sp ON gm.menu_id = sp.menu_parent_id WHERE gm.menu_id = {$outerID}");?>

        <ul>
          <?php

            while($innerRow = mysql_fetch_array($innerSql)) {
              $innerMenu = $innerRow['prod_name'];
          ?>
            <li><?= $innerMenu;?></li>
            <?php
            }
            ?>
        </ul>
      </li>
    <?php 
    } 
    ?>

</ul>

导致我麻烦的行是

 <?php $innerSql = mysql_query("SELECT sp.* FROM gree_menu gm INNER JOIN silicon_prod sp ON gm.menu_id = sp.menu_parent_id WHERE gm.menu_id = {$outerID}");?>

如果我将查询作为

 <?php $innerSql = mysql_query("SELECT sp.* FROM gree_menu gm INNER JOIN silicon_prod sp ON gm.menu_id = sp.menu_parent_id WHERE gm.menu_id = 7");?>

它工作正常。但我希望它是动态的。 menu_id的有效值为7,8,9

请帮忙

5 个答案:

答案 0 :(得分:2)

试试这个

<?php $innerSql = mysql_query("SELECT sp.* FROM gree_menu gm INNER JOIN silicon_prod sp ON gm.menu_id = sp.menu_parent_id WHERE gm.menu_id =".{$outerID});?>

答案 1 :(得分:2)

你在@IDhi Beckert提到的outerID中输错了,而不是outerId。

$outerId = $outerRow['menu_id'];

您在查询中使用了$outerID。所以我不认为你的查询有错误。纠正错字并重试。

如果您想更改查询,请尝试以下

$innerSql = mysql_query("SELECT sp.* FROM gree_menu gm 
          INNER JOIN silicon_prod sp ON gm.menu_id = sp.menu_parent_id
          WHERE gm.menu_id = ".$outerId);

答案 2 :(得分:0)

你可以试试这个

 <?php $innerSql = mysql_query("SELECT sp.* FROM gree_menu gm INNER JOIN silicon_prod sp ON gm.menu_id = sp.menu_parent_id WHERE gm.menu_id = ".$outerID."");?>

答案 3 :(得分:0)

<?php $innerSql = mysql_query("SELECT sp.* FROM gree_menu gm INNER JOIN silicon_prod sp ON gm.menu_id = sp.menu_parent_id WHERE gm.menu_id = ".$outerID);?>

答案 4 :(得分:0)

mysql_query("SELECT sp.* FROM gree_menu gm 
INNER JOIN silicon_prod sp ON gm.menu_id = sp.menu_parent_id 
WHERE gm.menu_id =$outerID");