在MySQL中使用PHP的DataTables:如何修改mysql查询?
所以我想在我的数据库中使用一个表来填充一个html表格。让我们称之为“商业”,其中一列称为“id”。 我想使用WHERE。我 例如类似..在MySQL中使用PHP的DataTables:如何修改mysql查询?
SELECT * all FROM business WHERE id=4.
我需要修改$ sWhere吗?
这是我用于服务器端处理的代码。
/*
* Script: DataTables server-side script for PHP and MySQL
* Copyright: 2010 - Allan Jardine, 2012 - Chris Wright
* License: GPL v2 or BSD (3-point)
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/
/* Array of database columns which should be read and sent back to DataTables. Use a space where
* you want to insert a non-database field (for example a counter or static image)
*/
$aColumns = array('bus_num', 'bus_phone', 'category_name', 'bus_status');
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "id";
/* DB table to use */
$sTable = "business";
/* Database connection information */
$gaSql['user'] = "dev";
$gaSql['password'] = "dev";
$gaSql['db'] = "dev";
$gaSql['server'] = "localhost";
/* REMOVE THIS LINE (it just includes my SQL connection user/pass) */
$db = mysql_connect('localhost', 'dev', 'dev');
if (!$db){
die ("Could not connect to database" . mysql_error());
exit();
}
$db_selected = mysql_select_db('dev', $db);
if(!$db_selected) {
die ('Can\'t use:' . mysql_error());
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP server-side, there is
* no need to edit below this line
*/
/*
* Local functions
*/
function fatal_error ($sErrorMessage = '')
{
header($_SERVER['SERVER_PROTOCOL'] .' 500 Internal Server Error');
die($sErrorMessage);
}
/*
* MySQL connection
*/
if (! $gaSql['link'] = mysql_pconnect($gaSql['server'], $gaSql['user'], $gaSql['password'] ))
{
fatal_error('Could not open connection to server');
}
if (! mysql_select_db($gaSql['db'], $gaSql['link']))
{
fatal_error('Could not select database ');
}
/*
* Paging
*/
$sLimit = "";
if (isset($_GET['iDisplayStart']) && $_GET['iDisplayLength'] != '-1')
{
$sLimit = "LIMIT ".intval($_GET['iDisplayStart']).", ".
intval($_GET['iDisplayLength']);
}
/*
* Ordering
*/
$sOrder = "";
if (isset($_GET['iSortCol_0']))
{
$sOrder = "ORDER BY ";
for ($i=0 ; $i<intval($_GET['iSortingCols']) ; $i++)
{
if ($_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true")
{
$sOrder .= $aColumns[ intval($_GET['iSortCol_'.$i]) ]."
".($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
}
}
$sOrder = substr_replace($sOrder, "", -2);
if ($sOrder == "ORDER BY")
{
$sOrder = "";
}
}
/*
* Filtering
* NOTE this does not match the built-in DataTables filtering which does it
* word by word on any field. It's possible to do here, but concerned about efficiency
* on very large tables, and MySQL's regex functionality is very limited
*/
$sWhere = "";
if (isset($_GET['sSearch']) && $_GET['sSearch'] != "")
{
$sWhere = "WHERE (";
for ($i=0 ; $i<count($aColumns) ; $i++)
{
if (isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true")
{
$sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch'])."%' OR ";
}
}
$sWhere = substr_replace($sWhere, "", -3);
$sWhere .= ')';
}
/* Individual column filtering */
for ($i=0 ; $i<count($aColumns) ; $i++)
{
if (isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '')
{
if ($sWhere == "")
{
$sWhere = "WHERE ";
}
else
{
$sWhere .= " AND ";
}
$sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
}
}
/*
* SQL queries
* Get data to display
*/
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
$sWhere
$sOrder
$sLimit
";
$rResult = mysql_query($sQuery, $gaSql['link']) or fatal_error('MySQL Error: ' . mysql_errno());
/* Data set length after filtering */
$sQuery = "
SELECT FOUND_ROWS()
";
$rResultFilterTotal = mysql_query($sQuery, $gaSql['link']) or fatal_error('MySQL Error: ' . mysql_errno());
$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];
/* Total data set length */
$sQuery = "
SELECT COUNT(".$sIndexColumn.")
FROM $sTable
";
$rResultTotal = mysql_query($sQuery, $gaSql['link']) or fatal_error('MySQL Error: ' . mysql_errno());
$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];
/*
* Output
*/
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ($aRow = mysql_fetch_array($rResult))
{
$row = array();
for ($i=0 ; $i<count($aColumns) ; $i++)
{
if ($aColumns[$i] == "version")
{
/* Special output formatting for 'version' column */
$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
}
else if ($aColumns[$i] != ' ')
{
/* General output */
$row[] = $aRow[ $aColumns[$i] ];
}
}
$output['aaData'][] = $row;
}
echo json_encode($output);
DataTables是jQuery JavaScript库一个插件。这是一个高度灵活的工具,基于渐进式 增强功能的基础,它将向任何HTML 表添加高级交互控件。
您可能需要使用:
服务器端处理|使用MySQL的PHP此脚本充当用于DataTables测试和开发的 基础,因此 它始终保持最新状态,并将始终实现DataTable中受服务器端处理支持的所有 功能 (除了正则表达式过滤 - 对于数据库访问速度 的原因)。
使用的代码在自己的服务器上,只需更改$ aColumns阵列 列出你希望从你的数据库中的列,设置 $ sIndexColumn到其索引(速度)一列,$稳定 表名,最后填入你的数据库连接参数为 $ gaSql。请注意,这个脚本使用了json_encode,它需要PHP5.2或更新版本的PHP5.。对于一个版本脚本这是与旧版本兼容 请参阅PHP 4兼容
我得到了这部分的PHP部分工作,所有的数据提出,与我的index.php,但按列,分页和每页条目数过滤不起作用。我的index.php看起来像[this](https://datatables.net/examples/api/multi_filter。html),但'tfoot'和'theader'是静态的,并且tbody由db返回的内容填充。任何想法? – HattrickNZ 2016-04-18 22:48:25
/*
* Filtering
* NOTE this does not match the built-in DataTables filtering which does it
* word by word on any field. It's possible to do here, but concerned about efficiency
* on very large tables, and MySQL's regex functionality is very limited
*/
//$sWhere = "";
if (isset($_GET['sSearch']) && $_GET['sSearch'] != "")
{
$sWhere = "WHERE id='4' AND (";
for ($i=0 ; $i<count($aColumns) ; $i++)
{
if (isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true")
{
$sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch'])."%' OR ";
}
}
$sWhere = substr_replace($sWhere, "", -3);
$sWhere .= ')';
} else {
$sWhere = "WHERE id = '4' ";
}
这对我有用,谢谢! – BSUK 2015-01-19 17:23:43
欢迎堆栈溢出!请不要使用'mysql_ *'函数来编写新的代码。他们不再被维护,社区已经开始[弃用流程](http://news.php.net/php.internals/53799)。看到[红色框](http://uk.php.net/manual/en/function.mysql-connect.php)?相反,您应该了解[准备好的陈述](http://en.wikipedia.org/wiki/Prepared_statement)并使用[PDO](http://php.net/pdo)或[MySQLi](http:// php.net/mysqli)。如果你选择PDO [这里是一个很好的教程](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers)。 – War10ck 2013-03-06 14:32:42
谢谢你的抬头。 – theBookeyMan 2013-03-06 14:34:38
没问题的朋友。快乐的编码! – War10ck 2013-03-06 14:37:15