我正在尝试使用“ 在PHP中使用jTable ”示例http://www.jtable.org/Home/Downloads,但是当我运行程序并将“ mysql”功能更改为“ mysqli”时,我无法连接到数据库并收到错误:“与服务器通信时发生错误”
我是使用mysqli的新手,我一整天都在搜索,但最终一无所获。
jTableSimplePagedSorted.php
<html>
<head>
<link href="themes/redmond/jquery-ui-1.8.16.custom.css" rel="stylesheet" type="text/css" />
<link href="Scripts/jtable/themes/lightcolor/blue/jtable.css" rel="stylesheet" type="text/css" />
<script src="scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
<script src="scripts/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
<script src="Scripts/jtable/jquery.jtable.js" type="text/javascript"></script>
</head>
<body>
<div id="PeopleTableContainer" style="width: 600px;"></div>
<script type="text/javascript">
$(document).ready(function () {
//Prepare jTable
$('#PeopleTableContainer').jtable({
title: 'Table of people',
paging: true,
pageSize: 2,
sorting: true,
defaultSorting: 'Name ASC',
actions: {
listAction: 'PersonActionsPagedSorted.php?action=list',
createAction: 'PersonActionsPagedSorted.php?action=create',
updateAction: 'PersonActionsPagedSorted.php?action=update',
deleteAction: 'PersonActionsPagedSorted.php?action=delete'
},
fields: {
PersonId: {
key: true,
create: false,
edit: false,
list: false
},
Name: {
title: 'Author Name',
width: '40%'
},
Age: {
title: 'Age',
width: '20%'
},
RecordDate: {
title: 'Record date',
width: '30%',
type: 'date',
create: false,
edit: false
}
}
});
//Load person list from server
$('#PeopleTableContainer').jtable('load');
});
</script>
</body>
</html>
PersonActionsPagedSorted.php
<?php
try
{
//Open database connection
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con, "jtabletestdb");
//Getting records (listAction)
if(isset($_GET["action"]) || "list")
{
//Get record count
$result = mysqli_query("SELECT COUNT(*) AS RecordCount FROM people;");
$row = mysqli_fetch_array($result);
$recordCount = $row['RecordCount'];
//Get records from database
$result = mysqli_query("SELECT * FROM people ORDER BY " . $_GET["jtSorting"] . " LIMIT " . $_GET["jtStartIndex"] . "," . $_GET["jtPageSize"] . ";");
//Add all records to an array
$rows = array();
while($row = mysqli_fetch_array($result))
{
$rows[] = $row;
}
//Return result to jTable
$jTableResult = array();
$jTableResult['Result'] = "OK";
$jTableResult['TotalRecordCount'] = $recordCount;
$jTableResult['Records'] = $rows;
print json_encode($jTableResult);
}
//Creating a new record (createAction)
else if(isset($_GET["action"]) == "create")
{
//Insert record into database
$result = mysqli_query("INSERT INTO people(Name, Age, RecordDate) VALUES('" . $_POST["Name"] . "', " . $_POST["Age"] . ",now());");
//Get last inserted record (to return to jTable)
$result = mysqli_query("SELECT * FROM people WHERE PersonId = LAST_INSERT_ID();");
$row = mysqli_fetch_array($result);
//Return result to jTable
$jTableResult = array();
$jTableResult['Result'] = "OK";
$jTableResult['Record'] = $row;
print json_encode($jTableResult);
}
//Updating a record (updateAction)
else if(isset($_GET["action"]) == "update")
{
//Update record in database
$result = mysqli_query("UPDATE people SET Name = '" . $_POST["Name"] . "', Age = " . $_POST["Age"] . " WHERE PersonId = " . $_POST["PersonId"] . ";");
//Return result to jTable
$jTableResult = array();
$jTableResult['Result'] = "OK";
print json_encode($jTableResult);
}
//Deleting a record (deleteAction)
else if(isset($_GET["action"]) == "delete")
{
//Delete from database
$result = mysqli_query("DELETE FROM people WHERE PersonId = " . $_POST["PersonId"] . ";");
//Return result to jTable
$jTableResult = array();
$jTableResult['Result'] = "OK";
print json_encode($jTableResult);
}
//Close database connection
mysqli_close($con);
}
catch(Exception $ex)
{
//Return error message
$jTableResult = array();
$jTableResult['Result'] = "ERROR";
$jTableResult['Message'] = $ex->getMessage();
print json_encode($jTableResult);
}
?>
答案 0 :(得分:0)
使用以下语法连接到数据库: mysqli_connect(主机,用户名,密码,dbname,端口,套接字); 这是示例的literal_eval。
答案 1 :(得分:0)
{
//Open database connection
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con, "jtabletestdb");
//Getting records (listAction)
if(isset($_GET["action"]) || "list")
{
//Get record count
$result = mysqli_query($con, "SELECT COUNT(*) AS RecordCount FROM people;"); //just add $con
$row = mysqli_fetch_array($result);
$recordCount = $row['RecordCount'];
//Get records from database
$result = mysqli_query($con, "SELECT * FROM people ORDER BY " . $_GET["jtSorting"] . " LIMIT " . $_GET["jtStartIndex"] . "," . $_GET["jtPageSize"] . ";"); //just add $con
//Add all records to an array
$rows = array();
while($row = mysqli_fetch_array($result))
{
$rows[] = $row;
}
//Return result to jTable
$jTableResult = array();
$jTableResult['Result'] = "OK";
$jTableResult['TotalRecordCount'] = $recordCount;
$jTableResult['Records'] = $rows;
print json_encode($jTableResult);
}