【问题标题】:converting php code to html将php代码转换为html
【发布时间】:2016-10-01 09:14:57
【问题描述】:

5 个小时以来,我一直在努力尝试将我的 php 代码转换为 html,而此时,我真的筋疲力尽了 :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 表显示。

【问题讨论】:

  • 帮忙做什么?你在问什么?
  • 你的代码有什么问题。你想达到什么目标?
  • mysql_* 已弃用尝试使用 mysqli_* 或 PDO
  • 我的 php 工作正常,所以我想将 php 代码转换为 html。基本上,我希望我的数据库中的表使用 HTML 表显示。
  • 有什么错误吗?

标签: php html type-conversion


【解决方案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>

【讨论】:

  • 更改连接数据库为 not mach php code.pls,chack code
【解决方案2】:

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

【讨论】:

  • 刚刚尝试过,它只是显示代码,甚至没有表格:X。
  • 扩展名为 filename.php 或 filename.html 的文件名是什么? @JessicaLim
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    下载 HTTrack Website Copier 并安装并运行该软件。运行您的脚本并将您的 URL 粘贴到网络复印机软件中。您将在所需文件夹中获得 HTML 输出

    【讨论】:

      猜你喜欢
      • 2013-10-24
      • 2023-03-23
      • 2018-07-03
      • 2013-08-11
      • 2021-10-03
      • 2015-07-16
      • 2010-12-07
      • 1970-01-01
      • 2011-03-14
      相关资源
      最近更新 更多