Page 1 of 3
[SOLVED] Creating mysql db user
Posted: Tue Dec 16, 2003 8:32 am
by yaron
Hello all.
I'm trying to automate the the user creation on mysql db.
As you may know the db which hold the user information and the privilages is called 'mysq'l and the table is 'user'.
Everything works smoothly and the user is created but when I try to connect to my db via this user I get denied.
If I put it manually (via phpmyadmin) everything works
any ideas?
Posted: Tue Dec 16, 2003 8:39 am
by twigletmac
What does the SQL look like with which you are trying to enter the user?
Mac
Posted: Tue Dec 16, 2003 8:43 am
by malcolmboston
for this to be happening theres got to be something wrong with your code, either that or the database that you use to insert the data into is not the same as the one you use to retrieve for login
Posted: Tue Dec 16, 2003 9:04 am
by yaron
Hmmm...
Something is strange.
I've tried to manual enter a user to mysql db and it didn't work either.
I have other users I have entered before and are working fine...
It can't be my code because the manual doesn't work as well.
And i'm putting it on the right db becasue I can see there the records of the users that does work...
Posted: Tue Dec 16, 2003 9:05 am
by malcolmboston
do you mean you want to create a user?
give me you tablename, username field, password field and database name and ill do it for you
this explanationmost definitely will be explained in the PHP or Mysql manual
Posted: Tue Dec 16, 2003 9:14 am
by yaron
I know the table name fields etc.
The user name is entered successfully to the right place!!!
when I try to connect with this user I get an error.
Actually the mysql_connect works ok but it fails on mysql_select_db with the error that access is denied to that user
Posted: Tue Dec 16, 2003 9:20 am
by malcolmboston
i know what your problem is
your connection username, host or password is wrong
make sure you use the same as you do in PHPmyAdmim
Posted: Tue Dec 16, 2003 9:30 am
by yaron
I have checked and rechecked this issue and it seems alright...
am I going mad?!
Posted: Tue Dec 16, 2003 9:37 am
by malcolmboston
Code: Select all
// database connection values
$host = "localhost";
$user = "your_mysql_username";
$password = "your_mysql_password";
//connection variables completeed
// establishing connections
mysql_connect ($host, $user, $password);
//connection established
it can be done other ways but thats how i do it
Posted: Tue Dec 16, 2003 9:40 am
by malcolmboston
Code: Select all
// database connection values
$host = "localhost";
$user = "your_mysql_username";
$password = "your_mysql_password";
//connection variables completeed
// establishing connections
mysql_connect ($host, $user, $password);
//connection established
it can be done other ways but thats how i do it
Posted: Tue Dec 16, 2003 9:51 am
by yaron
This is how do it too.
Something is not right and I don't know what it is....
I must be missing something because I did this kinda thing many tiimes before
Posted: Tue Dec 16, 2003 9:53 am
by malcolmboston
from the error message you get it is definitely a MySQL connection problem (i know, ive had them) this should fix it, go through all your code regarding connections to make sure its ok
also it may be worth your time viewing your mysql privileges (can be done through PHPmyAdmin)
Posted: Tue Dec 16, 2003 9:54 am
by twigletmac
Please show us your code anyway just in case. When you say manual - what do you mean?
Mac
my code
Posted: Tue Dec 16, 2003 10:25 am
by yaron
here is my code:
Code: Select all
<?php
mysql_select_db("mysql",$db);
$sqlDbUsers="INSERT INTO user(Host,User,Password,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Reload_priv,Shutdown_priv,Process_priv,File_priv,Grant_priv,References_priv,Index_priv,Alter_priv) VALUES ('localhost','superuser','5194351','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y')";
mysql_query($sqlDbUsers) or die('Query failed: ' . mysql_error());
mysql_close($db);
$db = @mysql_connect("localhost","superuser","5194351") or die("Could not connect: " . mysql_error());
if(!mysql_select_db("myDB",$db))
{
echo "<br>No database selected<br>". mysql_error();
exit;
}
?>
When I say manual I mean via phpmyadmin
Posted: Tue Dec 16, 2003 2:26 pm
by DuFF
Well you are trying to connect to the MySQL database with a username that is held in the table. This is not how it works. You always connect with your username and your password.
See, you are inserting the username here:
Code: Select all
<?php
$sqlDbUsers="INSERT INTO user(Host,User,Password,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Reload_priv,Shutdown_priv,Process_priv,File_priv,Grant_priv,References_priv,Index_priv,Alter_priv) VALUES ('localhost','superuser','5194351','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y')";
?>
and then trying to connect to the database using that usename and password:
Code: Select all
<?php
$db = @mysql_connect("localhost","superuser","5194351") or die("Could not connect: " . mysql_error());
?>
You can only connect to the MySQL database with the username given to you by your host. Unless these are your username and password for the actual database, then you might want to edit them . . .