[英]Displaying mysql database with php and Jquery Datatables?
我一直试图第一次使用Jquery的数据表,而可用的教程让我很困惑。 我已经完成了以下脚本,试图将jquery合并到我知道运行良好的php表显示中。 我在这里可能有一些根本性的错误,现在我很困。 谁能解决这个问题? (只是尝试显示带有jquery数据表的基本mysql数据库。)非常感谢。
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Sid, Fname, Lname, Email, Dtype, Mac, Date FROM StudentDeviceReg";
$result = $conn->query($sql);
<script src="media/js/jquery.js" type="text/javascript"></script>
<script src="media/js/jquery.dataTables.js" type="text/javascript"></script>
<style type="text/css">
@import "media/css/demo_table.css";
</style>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$('#datatables').dataTable();
})
</script>
if ($result->num_rows > 0) {
echo "<table id="datatables" class="display">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Device</th>
<th>Mac Address</th>
<th>Date</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>".$row["Sid"]."</td>
<td>".$row["Fname"]."</td>
<td>".$row["Lname"]."</td>
<td>".$row["Email"]."</td>
<td>".$row["Dtype"]."</td>
<td>".$row["Mac"]."</td>
<td>".$row["Date"]."</td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
以下部分需要在php括号外进行echoed
或声明
?>
<script src="media/js/jquery.js" type="text/javascript"></script>
<script src="media/js/jquery.dataTables.js" type="text/javascript"></script>
<style type="text/css">
@import "media/css/demo_table.css";
</style>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$('#datatables').dataTable();
})
</script>
<?php
另外,以下部分需要转义序列或单引号:
echo "<table id=\"datatables\" class=\"display\">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Device</th>
<th>Mac Address</th>
<th>Date</th>
</tr>";
您这条线上的错误是什么意思?
echo "<table id="datatables" class="display">
您应该注意属性表中的双引号,它表示结束回显。 您可以使用单引号。
echo "<table id='datatables' class='display'>
或使用反斜杠。
echo "<table id="\datatables\" class="\display\">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.