简体   繁体   English

使用PHP中的MySQL将单个mysqli_fetch_array()查询填充多个下拉列表

[英]Populating multiple dropdowns with a single mysqli_fetch_array() query to MySQL in PHP

I am having trouble populating multiple drop-downs .I need to do it with a single query to db and is it possible to do it within a single while loop? 我在填充多个下拉列表时遇到问题。我需要对db执行一次查询,是否可以在单个while循环中执行此操作? Here is the code.Only the first dropdown(Player) is getting populated.The database table(playerDB) has 2 columns-Player and Game. 这是代码。只有第一个下拉列表(播放器)正在填充。数据库表(playerDB)有2列 - 播放器和游戏。 and the respective dropdowns need to be populated 并且需要填充相应的下拉列表

 <form name="form1" action="" method="post">
    <fieldset>
      <legend>Selecting report</legend>
       <p>
          <?php
             $connect = mysqli_connect('localhost','root','','mydatabase');

             if(mysqli_connect_errno($connect))
             {
                echo 'Failed to connect';
             }
            else{
              echo '';
            }

            if (!$res=mysqli_query($connect, "SELECT * from playerDB")){
                  echo ("Error description: "  .mysqli_error($connect));
            }
         ?>

         <label>Select Player</label>
         <select>
           <?php
             while($row=mysqli_fetch_array($res))
             {
           ?>
            <option> <?php echo $row["Player"]; ?> </option           
           <?php
             }
          ?>
        </select>

          <label>Select Game</label>
          <select id = "myGameList">          
             <?php
             while($row=mysqli_fetch_array($res))
             {
           ?>
            <option> <?php echo $row["Game"]; ?> </option>                
           <?php
             }
           ?>
          </select>

Any help would be appreciated,Thanks! 任何帮助将不胜感激,谢谢!

1. Instead of using while() to populate select-boxes,try to save records in array and then use that array as many time as you want. 1.不要使用while()来填充选择框,而是尝试将记录保存在数组中,然后根据需要多次使用该数组。

2. Also try to fetch only those columns what you actually needed further. 2.还尝试仅获取您实际需要的那些列。 It will make query lighter as well as records array lighter too. 它会使查询更轻,记录数组也更轻。

Do like below:- 如下所示: -

<?php
     $connect = mysqli_connect('localhost','root','','mydatabase');

     if(mysqli_connect_errno($connect))
     {
        echo 'Failed to connect';
     }
    else{
        if (!$res=mysqli_query($connect, "SELECT Player,Game from playerDB")){ // get that much column only which you want not all
              echo ("Error description: "  .mysqli_error($connect));
        }
    }
    $data = []; //create array 
    while($row=mysqli_fetch_array($res))
    {
        $data['Player'][] = $row["Player"]; //assign values to array
        $data['Game'][] = $row["Game"];//assign values to array
    }
?>


<form name="form1" action="" method="post">
    <fieldset>
        <legend>Selecting report</legend>
        <label>Select Player</label>
        <select>
        <?php
            foreach ($data['Player'] as $player){//use array as many times you want 
        ?>
            <option> <?php echo $player; ?> </option           
        <?php
            }
        ?>
        </select>
        <label>Select Game</label>
        <select id = "myGameList">          
        <?php
            foreach ($data['Game'] as $game){//use array as many times you want 
        ?>
            <option> <?php echo $game; ?> </option>                
        <?php
            }
        ?>
</select>

You can use the mysqli_fetch_all function. 您可以使用mysqli_fetch_all函数。

$connect = mysqli_connect('localhost','root','','mydatabase');

if(mysqli_connect_errno($connect))
{
    echo 'Failed to connect';
}

if (!$res=mysqli_query($connect, "SELECT * from playerDB")){
    echo ("Error description: "  .mysqli_error($connect));
}

$rows = mysqli_fetch_all($res);

Now you can use the $rows in your foreach 现在,您可以使用foreach$rows

foreach ($rows as $row) {
    echo $row['Player'];
}

foreach ($rows as $row) {
    echo $row['Game'];
}

You are already using a single query (which is good). 您已经在使用单个查询(这很好)。 The reason why only one of your drop-downs is getting populated is because mysqli_fetch_array always moves the result pointer of ($res) forward and it doesn't get reset automatically. 之所以只有一个下拉列表被填充,是因为mysqli_fetch_array始终向前移动($ res)的结果指针并且它不会自动重置。 Take a look at the code below. 看看下面的代码。 This should solve your problem and also clarify some of your doubts. 这应该可以解决您的问题并澄清您的一些疑虑。

<form name="form1" action="" method="post">
    <fieldset>
        <legend>Selecting report</legend>
        <p>
        <?php
            $connect = mysqli_connect('localhost','root','','mydatabase');

            if(mysqli_connect_errno($connect))
            {
                echo 'Failed to connect';
            }
            else{
                echo '';
            }

            // Single DB query. This is good. 
            // You could also use "SELECT Player, Game FROM playerDB"
            // This is generally recommended over * because in real-life databases the number of columns in the table can be huge.
            // You should only pick the columns that you need.
            if (!$res=mysqli_query($connect, "SELECT * from playerDB")){
                echo ("Error description: "  .mysqli_error($connect));
            }

            $player_options = "";
            $game_options = "";

            // Single loop per row. Update the HTML into PHP variables here and then latur re-use these option variables to generate your dropdown(s)
            while($row=mysqli_fetch_array($res))
            {
                $player_options .= "<option>".$row["Player"]."</option>";
                $game_options .= "<option>".$row["Game"]."</option>";
            }
         ?>

            <label>Select Player</label>
            <select>
                <?php echo $player_options; ?>
            </select>

            <label>Select Game</label>
            <select id = "myGameList">          
                <?php echo $game_options; ?>
            </select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM