Alastair Roy wrote:
> Greetings everyone, I am having a problem with PHP and mysql I have copied a
> script exactly off a website to open the database and insert a new user I
> try to access the mysql database using the root user, and enter information
> into the user table, this is the PHP I am using
> 
> <? 
> include 'library/config.php'; 
> include 'library/opendb.php'; 

you can't issue a query until you've defined your connection.  Is that done in
one of the above include files?  We need to see your connection code (don't show
us the password, of course) if we are to help troubleshoot this.

> $query = "INSERT INTO user (host, user, password, select_priv, insert_priv,
> update_ priv) ". 
>          "VALUES ('localhost', 'phpcake', PASSWORD('mypass'), 'Y', 'Y',
> 'Y')"; 
> mysql_query($query) or die('Error, insert query failed'); 

"Error, insert query failed" isn't very helpful.  You need to print the mysql
error if you want to know what went wrong.

> $query = "FLUSH PRIVILEGES"; 
> mysql_query($query) or die('Error, insert query failed'); 

Same here, except this is even worse, as it's the same message as before for a
different query.  How will you tell which statement didn't work?

> include 'library/closedb.php'; 
> ?>
> 
> When I try this I get the access for user [EMAIL PROTECTED] host denied, it is

Perhaps that's just a typo, but it should be '[EMAIL PROTECTED]', not
'[EMAIL PROTECTED] host'.

> driving me nuts no matter what I try I get the same thing other scripts I
> have tried do the same thing, have tried messing around with the config.php
> and opendb.php included scripts but nothing works if I run php -f opendb.php
> I get no errors which I think means it is working, then again I don't know.
> 
> Anyone have any ideas ??

First, from what I can see, you don't know what's wrong because you don't print
the errors when something goes wrong.  Of course, that means we can't tell you
either.  I recommend following Example 1 in the PHP manual
<http://www.php.net/manual/en/ref.mysql.php#mysql.examples>, but I'd add that
you should actually print the query string in addition to the error.  That way,
you can verify the query is actually what you intended.  Something like this:

  $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
    or die('Could not connect: ' . mysql_error());
   echo 'Connected successfully';

  $query = 'SELECT * FROM my_table';
  $result = mysql_query($query) or die("Query: $query
                                        failed: " . mysql_error());

Then, if you still need help, copy and paste the exact error message into your
next post.

Second, you really should use GRANT to add a user and set privileges rather than
directly editing the user table.  It's safer (for example, your INSERT will fail
if [EMAIL PROTECTED] already exists, but the equivalent GRANT statement would
still work), and you don't need FLUSH PRIVILEGES.

You appear to be trying to grant global SELECT, INSERT, and UPDATE privs to
[EMAIL PROTECTED]  In that case, your two queries can be replaced by this one:

$query = "GRANT SELECT, INSERT, UPDATE ON *.*
          TO [EMAIL PROTECTED] IDENTIFIED BY 'mypass'"

See the manual for details of using GRANT (and REVOKE) to manage users
<http://dev.mysql.com/doc/refman/5.0/en/grant.html>.

Michael


-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to