creating database and 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

jashankoshal
Forum Newbie
Posts: 7
Joined: Wed Sep 23, 2009 10:50 pm

creating database and user

Post by jashankoshal »

hello,
i want to create database,user,tables and insert rows using the code.
do not want to use the phpmyadmin and cpanel.
filename.php should do everything.
i have written the code and it is working in WAMP but not working in web server...
can anyone figure it out plz..
thnks.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: creating database and user

Post by Ollie Saunders »

So, um, what happens?

Make sure error reporting is set to E_ALL and display_errors is on and tell us what error you are getting. If you're using the error suppression operator anywhere, stop.
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: creating database and user

Post by Robert07 »

Usually that happens because the connection string needs to change in order to connect to the server instead of your WAMP box. Check the host, port, dbname, and password and make sure they are valid on the server.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: creating database and user

Post by Ollie Saunders »

Are you failing to make a database connection then? Is that a hunch or do you have an error telling you that's the problem?
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: creating database and user

Post by Robert07 »

That is my guess based on similar issues I've had before. But if the error reporting is turned on like you mentioned that would tell us for sure if that's the case.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: creating database and user

Post by Ollie Saunders »

Yeah, you must have evidence.
jashankoshal
Forum Newbie
Posts: 7
Joined: Wed Sep 23, 2009 10:50 pm

Re: creating database and user

Post by jashankoshal »

thankyou guys for reply..
here is the code...
can u help me in changing it...

<?php
$user=root;// give your phpmyadmin/cpanel username
$password="";// give your phpmyadmin/cpanel password
$database_name=jashan_db;// give your databse name
$database_user=jashan_koshal;// give your database user name
$user_password=12345;// give your password
$db=mysql_connect("localhost","$user","$password") or die("Not Working");

// code to create database
$database=" create database $database_name";
$result_db=mysql_query($database);
if($result_db)
{
echo"<br>Database Created<br>";
}

// code to create database user
$dbuser="GRANT ALL PRIVILEGES ON $database_name.* TO $database_user@localhost IDENTIFIED BY '$user_password'";
$result_user=mysql_query($dbuser);
if($result_user)
{
echo"<br>User Created</br>";
}
?>
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: creating database and user

Post by Ollie Saunders »

Please surround any code with [syntax=php]tags when posting.[/syntax]
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: creating database and user

Post by onion2k »

I imagine the database user you're connecting with online doesn't have the CREATE or GRANT privileges in MySQL.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: creating database and user

Post by Ollie Saunders »

A few stylistic issues:

Strings should be quoted with either a single (') or double quote ("); so:

Code: Select all

$user=root;
becomes:

Code: Select all

$user = 'root';
Any comment that tells you something that your code already does is redundant, and redundancy is a programmer's enemy. For example:

Code: Select all

$database_name=jashan_db;// give your databse name
should become:

Code: Select all

$database_name = 'jashan_db';
Finally, performing string interpolation around a single variable with double quotes is redundant. Code like this:

Code: Select all

$db=mysql_connect("localhost","$user","$password")
should be changed to something more like this:

Code: Select all

$db = mysql_connect('localhost', $user, $password)
To solve your problem you might want to download a graphical MySQL client and attempt to connect and perform the GRANT operation with that. If you can't connect with that, you won't be able to connect with PHP, which would make it a configuration issue. Any other errors in your PHP might be found by setting your error_reporting level to E_ALL, which I strongly recommend.
jashankoshal
Forum Newbie
Posts: 7
Joined: Wed Sep 23, 2009 10:50 pm

Re: creating database and user

Post by jashankoshal »

thanks again..
can plz tell me how to make "error_reporting level to E_ALL"???
dejvos
Forum Contributor
Posts: 122
Joined: Tue Mar 10, 2009 8:40 am

Re: creating database and user

Post by dejvos »

Code: Select all

 
error_reporting(E_ALL);
 
and setup php.ini

error_reporting = E_ALL
jashankoshal
Forum Newbie
Posts: 7
Joined: Wed Sep 23, 2009 10:50 pm

Re: creating database and user

Post by jashankoshal »

its not working...
i donno what to do...
my code is working on WAMP...but it is not working on WEB SERVER.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: creating database and user

Post by Ollie Saunders »

Oh, I missed an important point here. What kind of hosting do you have? Unless you have SSH access to the server there's probably no way you can actually write any PHP that will have permission to create a database table and issue a grant query.

Does your server not come with facilities for creating tables etc. Contact your host's support / view the instructional literature.

Also, please don't say "not working". These two words lack all information; what isn't working? What happens when you try?
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: creating database and user

Post by Robert07 »

Also, please don't say "not working". These two words lack all information; what isn't working? What happens when you try?
Amen to that!
Post Reply