PHP - MySQL Grant

Posted on 2005-04-12
Last Modified: 2013-12-12
I have problems with using 'GRANT'. My aim is to create a database, then assign permissions for the new user:


// set your infomation.

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

// create the database.
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.

Question by:theaussie
    1 Comment
    LVL 8

    Accepted Solution

    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());

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to count occurrences of each item in an array.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now