2017-10-10 1 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> 
관련 문제