2016-10-01 99 views
0

我一直在努力将我的php代码转换为html 5个小时,在这一点上,我真的被烧毁了:X。 这里是我的PHP代码将php代码转换为html

<?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表格显示。

+0

帮忙做什么?你在问什么? – Federkun

+0

你的代码有什么问题。你想要达到什么? – JYoThI

+2

mysql_ *已弃用尝试使用mysqli_ *或PDO – JYoThI

回答

1

你不关闭while;

所以更改代码:

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

雅它仍然没有显示任何内容:X –

+0

可以向我发送错误代码? –

+0

更改连接数据库为非马克php代码。请仔细阅读下面的代码: –

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

刚刚尝试过,它只是显示代码,而不是表格:X。 –

+0

什么是扩展名为filename.php或filename.html的文件名? @JessicaLim – JYoThI

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>