3

I tried:

SET @user = 'foo@localhost';
SET @pass = 'bar';
SET @sql = 'CREATE USER ? IDENTIFIED BY ?';
PREPARE stmt FROM $sql;

and I get error

ERROR 1064 (42000): 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 '? IDENTIFIED BY ?'

I also tried multiple variants how to define variables, but still without any luck.

SET @user = "'foo'@'localhost'";
SET @sql = 'CREATE USER ? IDENTIFIED BY ?';

and even

SET @user = 'foo';
SET @host = 'localhost';
SET @sql = 'CREATE USER ?@? IDENTIFIED BY ?';

Quite similar question was already asked more than 2 years ago, but there still is no useful answer :( Create User with MySQLi Prepared Statement

4

1 回答 1

5

这是我设法创建用户的唯一方法,不幸的是它没有为用户变量使用占位符:

SET @user := 'foo';
SET @host := 'localhost';
SET @pass := 'bar';
SET @sql := CONCAT("CREATE USER ", QUOTE(@user), "@", QUOTE(@host), " IDENTIFIED BY ", QUOTE(@pass));
PREPARE stmt FROM @sql;
EXECUTE stmt;

{CREATE | RENAME | DROP} USER从 MySQL 5.1.12 开始应该支持PS语句

于 2013-12-18T00:58:30.843 回答