• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1471
  • Last Modified:

PHP - MySQL Grant

I have problems with using 'GRANT'. My aim is to create a database, then assign permissions for the new user:

<?php

// set your infomation.
$dbhost='localhost';
$dbusername='root';
$dbuserpass='rootabc';
$dbname='testbase';
$newuser='abc';
$newuserpasswd='abc123';

// connect to the mysql database server.
$link_id = mysql_connect ($dbhost, $dbusername, $dbuserpass);
echo "success in database connection.";

// create the database.
$dbname=$newuser."_".$dbname;
if (!mysql_query("CREATE DATABASE $dbname")) die(mysql_error());
echo "success in database creation.";

//grant user rights.
if (!mysql_query("grant all on $dbname.* to $newuser identified by $newuserpasswd")) die(mysql_error());
echo "success in grant creation.";  
?>

The database is created Ok, but fails on the 'Grant' sections. The error message reads:

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 'abc123' at line 1

Any help appreciated.

0
theaussie
Asked:
theaussie
1 Solution
 
_Marcel_Commented:
If abc123 is the password of the user you are trying to grant, then I thin you need single quotes around the password in your query like this:

if (!mysql_query("grant all on $dbname.* to $newuser identified by '$newuserpasswd'")) die(mysql_error());
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now