簡體   English   中英

我正在嘗試將數據從MySQL顯示到包含表格的PHP頁面,但我無法在表格中顯示信息

[英]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循環中回顯表,我嘗試了echo表但是它沒有用,所以我試着在html中添加PHP,這就是我發布的內容。 當我在結束前回復$recipe它會顯示信息,但我需要在表格中。

檢查php設置文件中是否啟用了php短標簽。 如果不是你不能用<? 你必須使用<?php

例:

<?php echo $usrtext ?>

while循環也在顯示表之前結束。 在顯示結束后結束while循環。

嘗試這個

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

你的代碼包含一些不好的東西,首先,你不應該使用<? ?> <? ?> ,我甚至都不知道這是否有效。 使用<?= ?><?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>

這稱為內聯編碼

你可以這樣做。

...

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>

注意:如果上述代碼適合您,請仔細遵循我對您的代碼所做的更改。 希望這能幫助您學習編寫更好的代碼。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM