php - 使用 MySQLi 设置 MySQL 变量(不是 PHP 变量)

标签 php mysql variables mysqli set

它适用于 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/

相关文章:

php - 通过 AJAX 评估 PHP

php - 使用 HTML 表单选择和 php 文本输入搜索数据库

php - Laravel - 所有用户的一个通知

java - java中sql字符串单引号的无效字符

电子邮件函数中的 PHP 循环

matlab - 在 MATLAB 中使用 i 和 j 作为变量

python - Python 中的 If 语句和设置变量

c - 逐行读取文本并为每行分配变量

php - $_SERVER ['REQUEST_URI' ] 也带有#hash?

php - bind_param 错误,准备好的语句中的参数数量不匹配