I';我试图将MySQL中的数据显示到包含表的PHP页面上,但我可以';t get the info to


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数据库中的行,但我无法在html中显示PHP。这是我的代码:

    <?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时,它会显示信息,但我需要它在表中。

尝试这个

<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>

检查php设置文件中是否启用了php短标记。如果没有,你就不能使用<?,你必须使用<?php

示例:

<?php echo $usrtext ?>

同时while循环在显示表之前结束。在显示结束后结束while循环。

通过添加像这样重写代码

<?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>

$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>

您可以这样做。

...
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 } ?>

如果您的查询结果返回多行,那么您很可能正在寻找这样的东西:

<?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>

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

您的代码包含一些不好的东西,首先,您不应该使用<? ?>,我甚至不知道这在某个地方是否有效。使用<?= ?><?php echo ?>(但使用<?php,因为它更兼容

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

现在,你可以试试这个:

<?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>

这被称为内联编码