2016-10-01 4 views
0

私はPHPコードをhtmlに変換しようとしましたが、この時点では本当に燃え尽きました。 ここに私の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> 

少しヘルプここにしてください、ありがとうございます!!

EDIT:一部の人が私の質問を理解していないようです。私のPHPは正常に動作しているので、私はHTMLにPHPコードを変換したい。基本的に、私はテーブルからHTMLテーブルを使用して表示するデータベースから私のテーブルをしたい。

+0

何ができるのですか?何を聞いていますか? – Federkun

+0

コードに関する問題は何ですか?あなたは何を達成したいですか? – JYoThI

+2

mysql_ *は非推奨ですmysqli_ *またはPDOを使用してください – JYoThI

答えて

1

あなたは閉じていません。

そうにコードを変更します。実際に、あなたの問題はあなたが$localhostようには変数が存在しないHTMLバージョンのように言ったようですが、あなたのエラーコードの下などのMySQLを接続するためのパラメータを渡すことの変数のミスマッチを示し

<!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のコードではない。pls、chack code –

0

あなたのコード。

エラーコード:

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

ちょうどそれを試してみましたが、代わりにコードを表示します。 –

+0

extenstion filename.phpまたはfilename.htmlのファイル名は何ですか? @JessicaLim – JYoThI

0

ここではPDOを使用する別のバージョンだ - PHPのコネクタのはるかに優れたことにより、メンテナンス性や汎用性の面では。私は、MySQL、SQLite、SQLServerの下で動作する同じコードを持っています - 唯一の変更は接続文字列です。

すべての結果を一度に取り出すことに注意してください。私は配列を返すという事実を利用する。その配列の各要素は行です。各行はセルの配列です。これにより、結果セットを出力するために必要なコードが大幅に削減されます。それぞれのループには2つだけ必要です。それだけです。

<?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> 
関連する問題