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.
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 ;
, it is missing after the first 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
.
$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
$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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.