[SOLVED] Creating mysql db user

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

[SOLVED] Creating mysql db user

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What does the SQL look like with which you are trying to enter the user?

Mac
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post 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...
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post 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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post by yaron »

I have checked and rechecked this issue and it seems alright...
am I going mad?!
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post 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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Please show us your code anyway just in case. When you say manual - what do you mean?

Mac
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

my code

Post 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
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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 . . .
Post Reply