它适用于 PHP 变量:
从查询函数中准备一个结果
extract($GLOBALS);
$GLOBALS['mysqli'] = $mysqli;
function select($a,$b){
$mysqli = $GLOBALS['mysqli'];
$query = $mysqli -> query($b);
while($row = ($query -> fetch_assoc())){
return $row[$a]."<br>";
}// while
$query -> free();
}
从点击位置开始排序
$self = "SELECT order FROM channels WHERE id='$id'";
$self = "(SELECT order FROM (($self)AS x))";
if($btn == "Up"){
// Order previows
$other = "SELECT MAX(order) FROM channels WHERE order < $self";
}
if($btn == "Down"){
// Order next
$other = "SELECT MIN(order) FROM channels WHERE order > $self";
}
$other = "SELECT order FROM channels WHERE order=($other)";
设置 PHP 变量而不是 MYSQL 变量 $other = select('order',$other);
$query = "UPDATE channels SET order=$self WHERE order='$other';";
$query .= "UPDATE channels SET order='$other' WHERE id='$id'";
$mysqli->multi_query($query);
问题?我需要使用一个 MySQL 查询进行更快的查询
这不起作用:
设置 MYSQL 变量代替 PHP 变量
$query = "DEFINE other INT;"; // return: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFINE other INT(...);
$query .= "DECLARE other INT;"; // return: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE other INT(...);
$query .= "SET other = $other;"; // return: Unknown system variable 'other'
$query .= "UPDATE channels SET order=$self WHERE order=other;";
$query .= "UPDATE channels SET order=other WHERE id='$id'";
$mysqli->multi_query($query);
最佳答案
你必须使用user defined variable :
User-Defined Variables
You can store a value in a user-defined variable in one statement and then refer to it later in another statement. This enables you to pass values from one statement to another. User-defined variables are session-specific. That is, a user variable defined by one client cannot be seen or used by other clients. All variables for a given client session are automatically freed when that client exits.
User variables are written as @var_name, where the variable name var_name consists of alphanumeric characters, “.”, “_”, and “$”. A user variable name can contain other characters if you quote it as a string or identifier (for example, @'my-var', @"my-var", or @
my-var
).
$query .= "SET @other = $other;"; // return: Unknown system variable 'other'
关于php - 使用 MySQLi 设置 MySQL 变量(不是 PHP 变量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24853684/