Php 在jQuery数据表中显示数据库记录
我想从数据库中获取记录并将其显示在数据表中,但当我在浏览器中运行此PHP代码时,它只在一个简单的表中显示记录。我希望这个结果出现在jquery数据表中,用于搜索和排序 数据表的javascript函数使用jquery.dataTables.jsPhp 在jQuery数据表中显示数据库记录,php,jquery,mysql,jquery-datatables,Php,Jquery,Mysql,Jquery Datatables,我想从数据库中获取记录并将其显示在数据表中,但当我在浏览器中运行此PHP代码时,它只在一个简单的表中显示记录。我希望这个结果出现在jquery数据表中,用于搜索和排序 数据表的javascript函数使用jquery.dataTables.js <script type="text/javascript" language="javascript"> $(document).ready(function() { ('#datatable').D
<script type="text/javascript" language="javascript">
$(document).ready(function() {
('#datatable').DataTable();
})
</script>
用于获取记录的mysql函数
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("runindia",$con);
$query="select *from admin_login";
$rs=mysql_query($query,$con);
?>
<div class="container">
<table class="table table-bordered table-responsive table-hover display" id="datatable">
<thead>
<th> Admin ID</th>
<th> User Name</th>
<th> First Name</th>
<th> Last Name</th>
<th> Email</th>
</thead>
<?php
while($r=mysql_fetch_array($rs))
{ ?>
<tbody>
<tr>
<td><?php echo $r['admin_id'];?></td>
<td><?php echo $r['username'];?></td>
<td><?php echo $r['first_name'];?></td>
<td><?php echo $r['last_name'];?></td>
<td><?php echo $r['email'];?></td>
</tr>
</tbody>
<?php
} //closing while
mysql_close($con);//mysql connection close
?>
</table>
尝试将“tbody”排除在循环之外:
<table class="table table-bordered table-responsive table-hover display" id="datatable">
<thead>
<th> Admin ID</th>
<th> User Name</th>
<th> First Name</th>
<th> Last Name</th>
<th> Email</th>
</thead>
<tbody>
<?php
while($r=mysql_fetch_array($rs))
{ ?>
<tr>
<td><?php echo $r['admin_id'];?></td>
<td><?php echo $r['username'];?></td>
<td><?php echo $r['first_name'];?></td>
<td><?php echo $r['last_name'];?></td>
<td><?php echo $r['email'];?></td>
</tr>
<?php
} //closing while
mysql_close($con);//mysql connection close
?>
</tbody>
</table>
或者尝试更好的方法,例如通过JSON格式的AJAX调用获取数据mysql\ux函数已被弃用。改为使用mysqli_*或PDO。@Fabian,mysql_*函数在PHP5.5.0中被弃用,目前只有大约4%的PHP安装使用它。OP使用小于5.5.0的PHP的可能性非常高,因此使用mysql_*未被弃用的PHP的可能性非常高。它仍然被弃用,即不鼓励使用。当开始一个可能会存在一段时间的新项目时,最好不要依赖不推荐的方法。特别是在这种情况下,在方法调用中添加一个简单的i就足够了。很抱歉,但我看不出省略会有什么区别。这是因为在一个“表”中应该只有一个“tbody”,而您的循环将导致它有多个“tbody”,所以是的-您完全正确!谢谢你澄清了你回答的原因!您是否也使用@davidkonrad的建议来更改“datatable”;到$'datatable'.datatable?还要检查您的SQL查询:$query=select*from admin\u login;你错过了一个空间后*
<?php
$con=mysqli_connect("localhost","root","", "runindia");
$query="select * from admin_login";
$rs=mysqli_query($con, $query);
?>
<div class="container">
<table class="table table-bordered table-responsive table-hover display" id="datatable">
<thead>
<th> Admin ID</th>
<th> User Name</th>
<th> First Name</th>
<th> Last Name</th>
<th> Email</th>
</thead>
<?php
while($r = mysqli_fetch_array($rs, MYSQLI_ASSOC))
{ ?>
<tbody>
<tr>
<td><?php echo $r['admin_id'];?></td>
<td><?php echo $r['username'];?></td>
<td><?php echo $r['first_name'];?></td>
<td><?php echo $r['last_name'];?></td>
<td><?php echo $r['email'];?></td>
</tr>
</tbody>
<?php
} //closing while
mysqli_close($con);//mysqli connection close
?>
</table>