簡體   English   中英

將php代碼轉換為html

[英]converting php code to html

我一直很難嘗試將我的php代碼轉換為html已有5個小時了,在這一點上,我真的很疲憊:X。 這是我的PHP代碼

 <?php
  $con=mysqli_connect("localhost","dbuser","pw","dbname");
 // Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

 $result = mysqli_query($con,"SELECT * FROM tablea");

 echo "<table border='1'  >
 <tr>
 <th>id</th>
 <th>Subject_Code</th>
 <th>date</th>
 <th>name</th>
 <th>description</th>
 <th>Other_Details</th>
 </tr>";

 while($row = mysqli_fetch_array($result))
   {
   echo "<tr>";
   echo "<td>" . $row['id'] . "</td>";
   echo "<td>" . $row['Subject_Code'] . "</td>";
   echo "<td>" . $row['date'] . "</td>";
   echo "<td>" . $row['name'] . "</td>";
   echo "<td width='600' class='table_width'>" . $row['description'] . "</td>";
   echo "<td width='600' class='table_width' align='center'>" . $row['Other_Details'] . "</td>";
   echo "</tr>";
   }
 echo "</table>";

 mysqli_close($con);
 ?> 

這是我到目前為止對HTML所做的

<!doctype html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>database connections</title>
    </head>
    <body>
      <?php
      $username = "dbuser";
      $password = "pw";
      $host = "localhost";
      $database= "dbname";

      $connector = mysql_connect($localhost,$dbuser,$pw,dbname)
          or die("Unable to connect");
        echo "Connections are made successfully::";
      $selected = mysql_select_db("test_db", $connector)
        or die("Unable to connect");

      //execute the SQL query and return records
      $result = mysql_query("SELECT * FROM tablea ");
      ?>
      <table border="2">
      <thead>
        <tr>
          <th>id</th>
          <th>Subject_Code</th>
          <th>date</th>
          <th>name</th>
          <th>description</th>
          <td>Other_Details</td>
        </tr>
      </thead>
      <tbody>

        <?php
    while ($row = mysql_fetch_array($result)) {
        ?>
        <tr>
            <td><?php echo $row['id']; ?></td> 
            <td><?php echo $row['Subject_Code']; ?></td> 
            <td><?php echo $row['date']; ?></td> 
            <td><?php echo $row['name']; ?></td>
            <td><?php echo $row['description']; ?></td>
            <td><?php echo $row['Other_Details']; ?></td>
        </tr>

     <?php mysql_close($connector); ?>
    </body>
    </html>

小幫助請在這里,謝謝!

編輯:似乎有些人不理解我的問題。 我的php工作正常,因此我想將php代碼轉換為html。 基本上,我希望數據庫中的表使用HTML表顯示。

你不關門;

因此將代碼更改為:

    <!doctype html>
        <html lang="en">
        <head>
          <meta charset="UTF-8">
          <title>database connections</title>
        </head>
        <body>
          <?php

/////////////////////////////////// change \\\
          $username = "dbuser";
          $password = "pw";
          $host = "localhost";
          $database= "dbname";

          $connector = mysql_connect($localhost,$username,$password)
              or die("Unable to connect");
            echo "Connections are made successfully::";
          $selected = mysql_select_db($database, $connector)
            or die("Unable to connect");

    /////////////////////////////////// end change \\\

          //execute the SQL query and return records
          $result = mysql_query("SELECT * FROM tablea ");
          ?>
          <table border="2">
          <thead>
            <tr>
              <th>id</th>
              <th>Subject_Code</th>
              <th>date</th>
              <th>name</th>
              <th>description</th>
              <td>Other_Details</td>
            </tr>
          </thead>
          <tbody>

            <?php
        while ($row = mysql_fetch_array($result)) :
            ?>
            <tr>
                <td><?php echo $row['id']; ?></td> 
                <td><?php echo $row['Subject_Code']; ?></td> 
                <td><?php echo $row['date']; ?></td> 
                <td><?php echo $row['name']; ?></td>
                <td><?php echo $row['description']; ?></td>
                <td><?php echo $row['Other_Details']; ?></td>
            </tr>
         <?php endwhile;?>
         <?php mysql_close($connector); ?>
        </body>
        </html>

實際上,您的問題是,就像您所說的html版本一樣,沒有像$localhost這樣的變量,但是您傳遞參數來連接mysql等。錯誤代碼下面顯示了代碼的變量不匹配。

錯誤代碼 :

<?php
  $username = "dbuser";
  $password = "pw";
  $host = "localhost";
  $database= "dbname";

  $connector = mysql_connect($localhost,$dbuser,$pw,dbname);
                             ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^

  //here there is no variable like what you passing to connect mysql that's problem here 
  ?>

解決方案:

         <!doctype html>
        <html lang="en">
            <head>
          <meta charset="UTF-8">
          <title>database connections</title>
        </head>
        <body>

    <?php
      $con=mysqli_connect("localhost","dbuser","pw","dbname");
     // Check connection
     if(mysqli_connect_errno())
       {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
       }

     $result = mysqli_query($con,"SELECT * FROM tablea");

     echo "<table border='1'  >
     <tr>
     <th>id</th>
     <th>Subject_Code</th>
     <th>date</th>
     <th>name</th>
     <th>description</th>
     <th>Other_Details</th>
     </tr>";

      while($row = mysqli_fetch_array($result))
       {
            echo "<tr>";
           echo "<td>" . $row['id'] . "</td>";
           echo "<td>" . $row['Subject_Code'] . "</td>";
           echo "<td>" . $row['date'] . "</td>";
           echo "<td>" . $row['name'] . "</td>";
           echo "<td width='600' class='table_width'>" . $row['description'] . "</td>";
           echo "<td width='600' class='table_width' align='center'>" . $row['Other_Details'] . "</td>";
           echo "</tr>";
       }
     echo "</table>";

     mysqli_close($con);

     ?>

       </body>
        </html>

這是另一個利用PDO的版本-就可維護性和多功能性而言,到目前為止是php連接器的優越者。 我在MySQL,SQLite和SQLServer下使用相同的代碼-唯一更改的是連接字符串。

您會注意到我立即提取所有結果。 我還利用了我們返回數組的事實。 該數組的每個元素都是一行。 每行是一個單元格數組。 這大大減少了輸出結果集所需的代碼。 我們只需要兩個forEach循環即可。

<?php
    $host = 'localhost';
    $userName = 'dbuser';
    $password = 'pw';
    $nameOfDb = 'dbname';
    $nameOfTable = 'tablea';

    $pdo = new PDO('mysql:host='.$host, $userName, $password);
    $pdo->query("use " . $nameOfDb);

    // desired results requested explicitly, so when we get an array for the row's data, the elements will be in the same order
    // - not required if the order of the columns in the database matches the desired display order. (we could just use 'select *' then)
    //$query = $pdo->prepare('select * from '.$nameOfTable);
    $query = $pdo->prepare('select id, Subject_Code, date, name, description, Other_Details from '. $nameOfTable);

    $query->execute();
    $query->setFetchMode(PDO::FETCH_ASSOC);
    $rows = $query->fetchAll();
?><!doctype html>
<html>
<head>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>id</th><th>Subject_Code</th><th>date</th><th>name</th><th>description</th><th>Other_Details</th>
            </tr>
        </thead>
        <tbody><?php
                forEach($rows as $curRow)
                {
                    echo '<tr>';

                        // use this one if you want to display the columns in the same order they are returned in the query results
                        // AND you'd like to display all columns in the result
                        forEach($curRow as $curCell)
                            echo '<td>'.$curCell.'</td>';

                        // otherwise, you can request the desired elements by name
                        //
                        //  echo '<td>' . $curCell['id'] . '</td>'
                        //  echo '<td>' . $curCell['Subject_Code'] . '</td>'

                    echo '</tr>';
                }
            ?></tbody>
    </table>
</body>

下載HTTrack網站復印機並安裝並運行該軟件。 運行腳本,然后將URL粘貼到Web復印機軟件中。 您將在所需文件夾中獲得HTML輸出

暫無
暫無

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

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