[英]Error in MySQL syntax when i try to create database and set user privileges through PHP
This is the first time that i will ask a question in this forum. 这是我第一次在这个论坛上提问。
I am working on a php project in which i have to -through php- create a database and insert some empty tables. 我正在一个php项目中,我必须通过php创建数据库并插入一些空表。
The issue here is that i always get an error message regarding syntax when i try to add privileges to the superuser (for some reason, if i don't, an error message states that i don't have permition to access the database). 这里的问题是,当我尝试向超级用户添加特权时,总是收到关于语法的错误消息(由于某些原因,如果我不这样做,错误消息会指出我没有访问数据库的权限)。
Here is the code for creating the database and assigning privileges: 这是用于创建数据库和分配权限的代码:
$sql = "CREATE DATABASE IF NOT EXISTS `database` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci; ";
$sql .= "GRANT SELECT database.* TO 'superuser'@'localhost' ";
$sql .= "GRANT ALL ON database." TO 'superuser'@'localhost' IDENTIFIED BY 'password'; ";
$sql .= "FLUSH PRIVILEGES";
Can you tell me what did wrong? 你能告诉我哪里错了吗? Thanks in advance! 提前致谢!
You need to end each of your SQL statements with a semi colon ;
您需要以分号结束每个SQL语句;
, it is missing after the first GRANT
. ,在第一次GRANT
之后丢失。 The second GRANT
uses database."
and should be database.*
, should also be surrounded by backticks as "database" is a reserved word. That said, the first GRANT
can be omitted as the it will be included with GRANT ALL
. 第二个GRANT
使用database."
并且应该是database.*
,也应该用反引号包围,因为“ database”是保留字。也就是说,第一个GRANT
可以省略,因为它将包含在GRANT ALL
。
$sql = "CREATE DATABASE IF NOT EXISTS `database` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci; ";
$sql .= "GRANT ALL ON `database`.* TO 'superuser'@'localhost' IDENTIFIED BY 'password'; ";
$sql .= "FLUSH PRIVILEGES;";
You can also use a HEREDOC format to make it easier to read 您还可以使用HEREDOC格式使其更易于阅读
$sql = <<<SQL
CREATE DATABASE IF NOT EXISTS `database` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
GRANT ALL ON `database`.* TO 'superuser'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
SQL;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.