【问题标题】:I'm trying to display data from MySQL onto a PHP page that contains a table but I can't get the info to display inside the table我正在尝试将 MySQL 中的数据显示到包含表的 PHP 页面上,但我无法获取要在表内显示的信息
【发布时间】:2018-06-05 21:01:21
【问题描述】:

我正在尝试在表中显示 mysql 数据库中的行,但我无法让 PHP 在 html 中显示。这是我的代码:

    <?php
    $connection = mysql_connect("localhost","root","")
or die("no connection");
$db_select=mysql_select_db("smqr",$connection)
or die("no connection to db");

    $query= ("SELECT * FROM seafood");
    $result=mysql_query($query)or die(mysql_error());

    while
    ($row =mysql_fetch_array($result)):
     $recipe=$row['recipe'];
     $usrtext=$row['usrtext']; 
     $usrtxt=$row['usrtxt'];


    endwhile;
     ?>

  <body bgcolor="#ffccff">
  <table align="center"  width="780" height="100%"  bgcolor="lightgrey" border="1">   
    <thead>
    </thead>
    <tbody>
    <tr>
    <th height="220">
    <img src="seafoods.jpg" width="100%" height="220" /></th></tr>
    <tr>
    <th>Recipe Name <p>
    <? echo "$recipe" ?></p></th></tr>
    <tr>
    <th>Ingrediants and Measurements
    <p><? echo $usrtext ?></p></th></tr>
    <tr>
    <th>Instructions
    <p> <? echo $usrtxt ?></p></th>
    </tr>

我认为我必须在 while 循环中回显表,我尝试回显表,但它不起作用,所以我尝试在我发布的 html 中添加 PHP。当我在结束前回显$recipe 时,它会显示信息,但我需要它在表格中。

【问题讨论】:

    标签: php mysql html-table


    【解决方案1】:

    试试这个

    <table align="center"  width="780" height="100%"  bgcolor="lightgrey" border="1">   
      <tr>
        <td>Recipe Name </td>
        <td>Ingrediants and Measurements</td>
        <td>Instructions</td>
      </tr>
      <?php
      $connection = mysql_connect("localhost","root","")or die("no connection");
      $db_select=mysql_select_db("smqr",$connection) or die("no connection to db");
    
        $query= ("SELECT * FROM seafood");
        $result=mysql_query($query)or die(mysql_error());
    
        while($row=mysql_fetch_array($result))
        {
         $recipe=$row['recipe'];
         $usrtext=$row['usrtext']; 
         $usrtxt=$row['usrtxt'];
         ?>
      <tr>
        <td><?=$recipe?></td>
        <td><?=$usrtext?></td>
        <td><?=$usrtxt?></td>
      </tr>
     <?php
        }// End while
     ?>
    </table>
    

    【讨论】:

      【解决方案2】:

      检查您的 php 设置文件中是否启用了 php 短标签。如果不是你不能使用&lt;? 你必须使用&lt;?php

      示例:

      <?php echo $usrtext ?>
      

      另外,while 循环在显示表之前结束。在显示结束后结束while循环。

      【讨论】:

        【解决方案3】:

        通过添加来重写你的代码

        <?php ?> instead of <? ?>
        
        
        
        <?php
            $connection = mysql_connect("localhost","root","")
        or die("no connection");
        $db_select=mysql_select_db("test",$connection)
        or die("no connection to db");
        
            $query= ("SELECT * FROM profile");
            $result=mysql_query($query)or die(mysql_error());
        
            while
            ($row =mysql_fetch_array($result)):
            $recipe=$row['recipe'];
         $usrtext=$row['usrtext']; 
         $usrtxt=$row['usrtxt'];
        
        
            endwhile;
             ?>
        
          <body bgcolor="#ffccff">
          <table align="center"  width="780" height="100%"  bgcolor="lightgrey" border="1">   
            <thead>
            </thead>
            <tbody>
            <tr>
            <th height="220">
            <img src="seafoods.jpg" width="100%" height="220" /></th></tr>
            <tr>
            <th>Recipe Name <p>
            <?php echo "$recipe" ?></p></th></tr>
            <tr>
            <th>Ingrediants and Measurements
            <p><?php echo $usrtext ?></p></th></tr>
            <tr>
            <th>Instructions
            <p> <?php echo $usrtxt ?></p></th>
            </tr>
        

        【讨论】:

          【解决方案4】:

          $recipe、$usrtext 和 $usrtxt 都定义在你的 while 循环的本地范围内,因此它们在 "endwhile;" 之后将不存在没有了

          <?
          //define variables outside the while loop
          $recipe;
          $usrtext;
          $usrtxt;
          while($row =mysql_fetch_array($result)):
             $recipe=$row['recipe'];
             $usrtext=$row['usrtext']; 
             $usrtxt=$row['usrtxt'];
          endwhile;
           ?>
          
          <body bgcolor="#ffccff">
          <table align="center"  width="780" height="100%"  bgcolor="lightgrey" border="1">   
            <thead>
            </thead>
            <tbody>
            <tr>
              <th height="220">
                <img src="seafoods.jpg" width="100%" height="220" />
              </th>
            </tr>
             <tr>
               <th>
                 Recipe Name 
                 <p> <? echo $recipe ?></p>
               </th>
             </tr>
             <tr>
             <th>
               Ingrediants and Measurements
               <p><? echo $usrtext ?></p>
             </th>
           </tr>
           <tr>
              <th>
                Instructions
                  <p> <? echo $usrtxt ?></p>
              </th>
           </tr>
          </table>
          </body>
          

          【讨论】:

            【解决方案5】:

            你可以这样做。

            ...
            
            while($row = mysql_fetch_array($result)) {
            ?>
            
              <table align="center" width="780" height="100%" bgcolor="lightgrey" border="1">   
                <thead>
                </thead>
                <tbody>
                  <tr>
                    <th height="220">
                      <img src="seafoods.jpg" width="100%" height="220" />
                    </th>
                  </tr>
                  <tr>
                    <th>Recipe Name <p>
                      <?php echo $row['recipe']; ?></p>
                    </th>
                  </tr>
                  <tr>
                    <th>Ingrediants and Measurements
                    <p><?php echo $row['usrtxt']; ?></p>
                    </th>
                  </tr>
                  <tr>
                    <th>Instructions
                    <p><?php echo $row['usrtxt']; ?></p>
                    </th>
                  </tr>
                </tbody>
              </table>
            
            <?php } ?>
            

            【讨论】:

            • 难道你不想使用 做一些事情
            • 我之前用过这个系统,现在用的是PDO ;)
            【解决方案6】:

            如果您的查询结果返回不止一行,那么您很可能正在寻找类似这样的内容:

            <?php
                $connection = mysql_connect("localhost", "root", "") or die("no connection");
                $db_select  = mysql_select_db("smqr", $connection) or die("no connection to db");
                $query      = "SELECT * FROM seafood";
                $result     = mysql_query($query) or die(mysql_error());
            ?>
            
            <body bgcolor="#ffccff">
                <table align="center" width="780" bgcolor="lightgrey" border="1">
                    <thead>
                        <tr>
                            <th>Recipe Name</th>
                            <th>Ingrediants and Measurements</th>
                            <th>Instructions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php while ($row = mysql_fetch_array($result)): ?>
                        <tr>
                            <td><?php echo $row['recipe']; ?></td>
                            <td><?php echo $row['usrtext']; ?></td>
                            <td><?php echo $row['usrtxt']; ?></td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </body>
            

            注意:如果上述代码适合您,请仔细遵循我对您的代码所做的更改。希望这将帮助您学习编写更好的代码。 :)

            【讨论】:

              【解决方案7】:

              您的代码包含一些不好的东西,首先,您不应该使用&lt;? ?&gt;,我什至不知道这在某处是否有效。使用&lt;?= ?&gt;&lt;?php echo ?&gt;(但请改用&lt;?php,因为它更兼容。

              其次,使用 PDO 库代替 MySQL 是个好主意,它更安全、更简单、更灵活、更兼容,请使用 this 教程。

              现在,你可以试试这个:

              <?php
                  $connection = mysql_connect("localhost","root","")
                      or die("no connection");
                  $db_select=mysql_select_db("smqr",$connection)
                      or die("no connection to db");
              
                  $query= ("SELECT * FROM seafood");
                  $result=mysql_query($query)or die(mysql_error());
              ?>
              <table>
              <?php 
                  while($row=mysql_fetch_array($result)): // everything between "while():" and "endwhile;" will be outputted in a cycle
              ?>
                  <tr>
                      <td>
                          <?php echo $row['recipe']; ?>
                      </td>
                      <td>
                          <?php echo $row['usrtext']; ?>
                      </td>
                      <td>
                          <?php echo  $row['usrtxt']; ?>
                      </td>
                  </tr>
              <?php endwhile; ?>
              </table>
              

              这称为内联编码

              【讨论】:

                猜你喜欢
                • 2018-04-21
                • 2021-01-22
                • 2018-08-08
                • 1970-01-01
                • 2020-01-03
                • 1970-01-01
                • 2021-02-10
                • 1970-01-01
                • 2022-06-25
                相关资源
                最近更新 更多