将PHP代码转换为HTML

时间:2016-10-01 09:14:58

标签: php html type-conversion

我一直很难将我的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表格显示。

4 个答案:

答案 0 :(得分:1)

你没有关闭;

所以将代码更改为:

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

答案 1 :(得分:0)

实际上你的问题就像你说的那样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>

答案 2 :(得分:0)

这是另一个使用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>

答案 3 :(得分:0)

下载HTTrack网站复印机并安装并运行该软件。运行脚本,然后将URL粘贴到Web复印机软件中。您将在所需文件夹中获得HTML输出