php - ORDER BY 和 WHERE 变量和 LIMIT
问题描述
我有一个带有以下 SQL 代码的变量,它给了我一个错误(没有“ORDER BY name ASC”,代码可以正常工作):
$sql = "SELECT * FROM incidents WHERE active = 1 AND username = '".$_SESSION['username']."' LIMIT " . $limitNumber . "," . $pageCounter " ORDER BY name ASC";
给出下一个错误:
解析错误:语法错误,意外 '" ORDER BY name ASC"' (T_CONSTANT_ENCAPSED_STRING) in...
有什么想法可以解决这个问题吗?
问候。
编辑:
出于上下文目的共享所有代码:
<?php
include 'includes/dbh.inc.php';
include 'user_session.php';
// Limit quantity of results per page
$pageCounter = 10;
// Show number of rows in the table
$sql = "SELECT * FROM incidents WHERE active = 1 AND username = '".$_SESSION['username']."'";
$result = mysqli_query($conn, $sql);
$rowNumbers = mysqli_num_rows($result);
//while ($row = mysqli_fetch_array($result)) {
// echo $row['id'] . " " . $row['username'] . " " . $row['phone'] . "<br>";
//}
// Number of pages available
$numberPages = ceil($rowNumbers/$pageCounter);
// Determine in which page is the user
if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
// Determine limit per page
$limitNumber = ($page-1)*$pageCounter;
// Show results
$sql = "SELECT * FROM incidents WHERE active = 1 AND username = '".$_SESSION['username']."' LIMIT " . $limitNumber . "," . $pageCounter " ORDER BY name ASC";
$result = mysqli_query($conn, $sql);
echo "<table>";
echo "<tr><th>Name</th><th>TMC Location</th><th>Non-TMC Location</th><th>Country Code</th><th>Origin Offset</th><th>To Offset</th><th>Start Time</th><th>End Time</th><th>Updated</th><th>Created</th><th>AlertC</th><th>Note</th><th>Group</th><th>Action</th><th>Worked By</th><th>Date</th><th>Incident ID</th><th>Username</th><th>Edit</th><th>Delete</th></tr>";
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$name = $row['name'];
$tmclocation = $row['tmclocation'];
$nontmclocation = $row['nontmclocation'];
$countrycode = $row['countrycode'];
$ooffset = $row['ooffset'];
$toffset = $row['toffset'];
$stime = $row['stime'];
$etime = $row['etime'];
$updated = $row['updated'];
$created = $row['created'];
$alertc = $row['alertc'];
$rcoby = $row['rcoby'];
$note = $row['note'];
$rcogroup = $row['rcogroup'];
$action = $row['action'];
$workedby = $row['workedby'];
$date = $row['rcodate'];
$username = $row['username'];
$incidentid = $row['incidentid'];
echo "<tr><td>".$name."</td><td>".$tmclocation."</td><td>".$nontmclocation."</td><td>".$countrycode."</td><td>".$ooffset."</td><td>".$toffset."</td><td>".$stime."</td><td>".$etime."</td><td>".$updated."</td><td>".$created."</td><td>".$alertc."</td><td>".$note."</td><td>".$rcogroup."</td><td>".$action."</td><td>".$workedby."</td><td>".$date."</td><td>".$incidentid."</td><td>".$username."</td><td><a href='myincidents.php?id=" . $row['id'] ."'>Edit</a></td><td><a id='a_id' href='editor/delete.php?id=" . $row['id'] ."' onClick='return confirm(\"Are you sure?\");'>Delete</a></td></tr>";
}
echo "</table>";
// Display number of pages
for ($page=1; $page <= $numberPages ; $page++) {
echo '<a href="myincidents.php?page=' . $page . '">' . $page . ' </a>';
}
?>
解决方案
order by
首先在做之前limit
。
$sql = "SELECT * FROM incidents WHERE active = 1 AND username = '".$_SESSION['username']."' ORDER BY name ASC LIMIT " . $limitNumber . "," . $pageCounter " ";
推荐阅读
- mysql - mySQL 中的完全外连接计数
- ios - 将解析列中的值抓取到标签中
- powershell - 带有脚本块的 PowerShell Start-Job 使用参数调用另一个脚本
- python - 如何使用列号而不是名称来有条件地对 Pandas 中的行进行子集化
- python - 使 scipy.special 函数并行计算
- curl - curl: (35) SSL 连接错误,NSS 错误 -5938
- json - 映射中的空值以列出颠簸变换
- scala - 打包 fat jar 时出现 No Class Def Found Error:scala/Product$class
- r - 当我使用矩阵变量拟合和预测模型时,predic.lm 给出错误数量的预测值
- vue.js - 为什么我无法在 Vue 组件的“已安装”组件上读取 Vuex 存储?