2017-10-10 192 views
-1

嗨,我不知道我在做什麼工作:D 我正在使用php和js從我的數據庫中獲取數據並創建一個用戶查看錶。 我想我需要把所有的回聲放入一個變量或者類似的東西中。但我不確定。感謝每一位的支持。
我的js溫控功能 使用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> 
相關問題