2016-04-30 1 views
0

こんにちは私はhtmlとphpとmysqlを持つ新しいプログラマです。実際には、mysqlの一意のテーブルからすべてのデータを選択し、html5ページに表示したいと思います。 私はsms.htmlを作成し、私はChromeでそれを実行したときに、これは私がここでNormally I need to display a table with 5 th means 5 rowsmysqltableからすべてのデータを選択してhtmlに表示する方法

を見たものであるが、私のコード

<body> 
    <?php 
     echo "<table style='border: solid 1px black; border-collapse: collapse; 
     th, td { 
     padding: 5px; 
     text-align: left; 
     }'>"; 
     echo "<tr> 
     <th>date/th> 
     <th>Etat de TX1</th> 
     <th>Etat de TX2</th> 
     <th>Sur antenne</th> 
     <th>ALARME</th>  </tr>"; 

     class TableRows extends RecursiveIteratorIterator { 
     function __construct($it) { 
     parent::__construct($it, self::LEAVES_ONLY); 
     } 

     function current() { 
     return "<td style='width: 150px; border: 1px solid black;'>" .  
     parent::current(). "</td>"; 
      } 

     function beginChildren() { 
     echo "<tr>"; 
     } 

     function endChildren() { 
     echo "</tr>" . "\n"; 
     } 
    } 

     $servername = "localhost"; 
     $username = "root"; 
     $password = ""; 
     $dbname = "OACA"; 

     try { 
     $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, 

    $password); 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    $stmt = $conn->prepare("SELECT * FROM etat"); 
    $stmt->execute(); 

    // set the resulting array to associative 
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 

     foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
     echo $v; 
     } 
     } 
     catch(PDOException $e) { 
     echo "Error: " . $e->getMessage(); 
    } 
    $conn = null; 
    echo "</table>"; 
    ?> 
    </body> 
+0

これは少なくともないhttp://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-から、重複しませんページ –

+0

テーブルが小さすぎるためにテーブルのサイズを大きくする方法 –

答えて

0

は、あなたがこのような何かを行うことができませんでしたでしょうか?

<?php 
`$result = null; 

    $servername = "localhost"; 
    $username = "root"; 
    $password = ""; 
    $dbname = "OACA"; 

    try { 
     $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); 
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

     $stmt = $conn->prepare("SELECT * FROM etat"); 
     $stmt->execute(); 
     $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
     $count = $stmt -> rowCount(); 
     $all = $stmt->fetchALL(); 
     } 
    catch(PDOException $e) { 
     echo "Error: " . $e->getMessage(); 
    } 

?> 

<div>  
     <table style='border: solid 1px black; border-collapse: collapse; 
     th, td { 
     padding: 5px; 
     text-align: left; 
     }'> 
     <thead> 
      <tr> 
       <th>date/th> 
       <th>Etat de TX1</th> 
       <th>Etat de TX2</th> 
       <th>Sur antenne</th> 
       <th>ALARME</th> 
      </tr> 
      </thead> 
      <tbody> 
      <?php 
      if($count > 0) { 
      //For each row echo <tr></tr>   
        foreach ($all as $item) { 
         echo"<tr>"; 
         //For each column in the row echo each value in a <td></td> 
         foreach ($item as $key => $value) { 
         echo "<td>" . $value ."</td>";      
         } 
         "</tr>"; 
        } 

       } 
      ?> 
     </tbody> 

     </table> 
    </div> 
関連する問題