[英]MYSQL won't ORDER BY variable
I cannot get this Function to ORDER BY a variable. 我无法将此函数传递给ORDER BY变量。 As it appears below, it works perfectly. 如下所示,它工作正常。 If I change the word "cabin" to be a variable, even the same variable that already works for this, it doesn't work and returns the error listed below. 如果将“ cabin”一词更改为变量,即使是已经对此起作用的变量,它也将不起作用并返回以下错误。
Works: 作品:
function makedropdown($connection, $table) {
$result = mysqli_query($connection,"SELECT * FROM $table ORDER BY cabin ASC");
if (!$result) {
die ("Database query failed:" . mysqli_error());
}
echo "<select name='delcabin'>";
While ($row= mysqli_fetch_array($result)){
echo "<option>" . $row[1] . "</option>";
}
echo "</select>";
}
Doesn't work: 不起作用:
$result = mysqli_query($connection,"SELECT * FROM $table ORDER BY $table ASC");
Error Message Received: 收到错误消息:
mysqli_error() expects exactly 1 parameter, 0 given mysqli_error()期望恰好有1个参数,给定0
Thanks in advance. 提前致谢。
Personally, i'd modify the function to accept a third parameter to set the order by variable. 就个人而言,我将修改该函数以接受第三个参数以按变量设置顺序。
function makedropdown($connection, $table,$OrderBy) {
$result = mysqli_query($connection,"SELECT * FROM $table ORDER BY $OrderBy ASC");
if (!$result) {
die ("Database query failed:" . mysqli_error($connection));
}
echo "<select name='delcabin'>";
While ($row= mysqli_fetch_array($result)){
echo "<option>" . $row[1] . "</option>";
}
echo "</select>";
}
Then respectively call it by: 然后分别通过以下方式调用它:
$Conn = new mysqli(); // This is a test database connection call, for example only.
$Tablename = "TestTable"; // This is a valid table within the schema
$ColumnName = "TestColumn"; // This is a valid column within the SQL Table
makedropdown($Conn,$TableName,$ColumnName);
This way, you could avoid your query looking like: 这样,您可以避免查询看起来像:
$Table = "Cabin";
SELECT * FROM $Table ORDER BY $Table ASC
when echoed out, will print: 回显后,将打印:
SELECT * FROM Cabin ORDER BY Cabin ASC
Just do this... 只是做这个...
$result = mysqli_query($connection,"SELECT * FROM $table ORDER BY '{$yourvariable}' ASC");
Also....make sure that your variable is a valid column, otherwise it wont work. 另外....请确保您的变量是有效的列,否则它将无法工作。
So in this case is $table, being your table name, also a valid column name?? 因此,在这种情况下,$ table是您的表名,也是一个有效的列名?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.