2016-10-01 159 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>