嗨,我不确定我在做什么:D
我正在使用php和js从我的数据库中获取数据并创建一个用户视图表。
我想我需要将所有的回声放在变量或类似的东西中。但我不确定。感谢每一位人士的支持。
我的js fucntion
function dataT(){
$.ajax({
type:'Post',
url:'filltable.php',
data:{context :' showusers'},
success:function (data){
$('#content').html(data);
}
});
alert(data);
}

<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>
&#13;
<?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);
}
?>
&#13;
答案 0 :(得分: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>