2017-10-10 16 views
-1

こんにちは私は何をしているのか分かりません:D 私はphpとjsでdbからデータを取得してユーザービューテーブルを作成しています。 私はすべてのエコーを変数やそのようなものに入れる必要があると思います。確信はないけど。サポートに感謝します。
私のjs fucntion データベースからPhpとAjaxを使ってデータを取得する

function dataT(){ 
 
    $.ajax({ 
 
     type:'Post', 
 
     url:'filltable.php', 
 
     data:{context :' showusers'}, 
 
     success:function (data){ 
 
     $('#content').html(data); 
 
     } 
 
    }); 
 

 
    alert(data); 
 
    }

私のhtmlコード
<body onload="dataT()"> 
 
    
 
    <div class="row"> 
 
    <div class="container"> 
 
     <div class="col-sm-3"></div> 
 
     <div class="col-sm-6"> 
 
     <h3>tabla</h3> 
 
     <br> 
 
     <table id="dt" class="table table-hover "> 
 
      <thead> 
 
      <th>Id</th> 
 
      <th>UN</th> 
 
      <th>Mail</th> 
 
      <th>Actions</th> 
 
      </thead> 
 
      <tbody id="content"> 
 

 
      </tbody> 
 
     </table> 
 
     </div> 
 
     <div class="col-sm-3"></div> 
 

 
    </div> 
 
    </div>
ここ
は、私はPHPでフェッチよさ
<?php 
 
$servername = "localhost"; 
 
$username = "root"; 
 
$password = ""; 
 

 
    try { 
 
     $conn = new PDO("mysql:host=$servername;dbname=kyo", $username, $password); 
 
     // set the PDO error mode to exception 
 
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
 

 

 
     if($_POST['context']=='showusers'){ 
 
     fillT($conn); 
 
     } 
 

 
     //end try 
 
     } 
 
    catch(PDOException $e) 
 
     { 
 
     echo "Connection failed: " . $e->getMessage(); 
 
     } 
 

 

 

 
     function fillT(){ 
 
     $sql = "SELECT `username`, `mail`, id FROM `users` "; 
 
     $res = mysqli_query($conn,$sql); 
 

 
     if(!$res){ 
 
      die("Error!!! ... D:"); 
 
     }else{ 
 
      while ($data = mysqli_fetch_assoc($res)) { 
 
      ?> 
 
      <tr> 
 
       <td><?php echo $row['id']; ?></td> 
 
       <td><?php echo $row['username']; ?></td> 
 
       <td><?php echo $row['mail']; ?></td> 
 
      </tr> 
 
      <?php 
 
      } 
 
      echo json_encode($data); 
 
     } 
 

 
     mysqli_free_result($res); 
 
     mysqli_close($conn); 
 
     } 
 
?>

+0

PHPでデータを取得していますか? –

+0

php関数の呼び出し方法 –

+0

あなたのPHPコードに間違いのログがあります。詳細についてはサーバーログを確認してください –

答えて

1

お試しください...

<?php 
    $servername = "localhost"; 
    $username = "root"; 
    $password = ""; 

    try { 
     $conn = new PDO("mysql:host=$servername;dbname=kyo", $username, $password); 
     // set the PDO error mode to exception 
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

     //end try 
     } 
    catch(PDOException $e) 
     { 
     echo "Connection failed: " . $e->getMessage(); 
     } 
?> 

<body> 

    <div class="row"> 
    <div class="container"> 
     <div class="col-sm-3"></div> 
     <div class="col-sm-6"> 
     <h3>tabla</h3> 
     <br> 
     <table id="dt" class="table table-hover "> 
      <thead> 
      <th>Id</th> 
      <th>UN</th> 
      <th>Mail</th> 
      <th>Actions</th> 
      </thead> 
      <tbody id="content"> 


    <?php 
$stmt = $conn->prepare("SELECT `username`, `mail`, id FROM `users` "); 
    $stmt->execute(); 

// set the resulting array to associative 
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
      foreach($stmt->fetchAll() as $row){ 
      ?> 
      <tr> 
       <td><?php echo $row['id']; ?></td> 
       <td><?php echo $row['username']; ?></td> 
       <td><?php echo $row['mail']; ?></td> 
       <td><?php echo "actions"; ?></td> 
      </tr> 
      <?php 
     } 
?> 
      </tbody> 
     </table> 
     </div> 
     <div class="col-sm-3"></div> 

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