Page 1 of 2
creating database and user
Posted: Wed Sep 23, 2009 10:52 pm
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.
Re: creating database and user
Posted: Wed Sep 23, 2009 10:58 pm
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.
Re: creating database and user
Posted: Wed Sep 23, 2009 10:59 pm
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.
Re: creating database and user
Posted: Wed Sep 23, 2009 11:16 pm
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?
Re: creating database and user
Posted: Wed Sep 23, 2009 11:35 pm
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.
Re: creating database and user
Posted: Wed Sep 23, 2009 11:52 pm
by Ollie Saunders
Yeah, you must have evidence.
Re: creating database and user
Posted: Thu Sep 24, 2009 4:39 am
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>";
}
?>
Re: creating database and user
Posted: Thu Sep 24, 2009 5:53 am
by Ollie Saunders
Please surround any code with [syntax=php]tags when posting.[/syntax]
Re: creating database and user
Posted: Thu Sep 24, 2009 5:55 am
by onion2k
I imagine the database user you're connecting with online doesn't have the CREATE or GRANT privileges in MySQL.
Re: creating database and user
Posted: Thu Sep 24, 2009 6:01 am
by Ollie Saunders
A few stylistic issues:
Strings should be quoted with either a single (') or double quote ("); so:
becomes:
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:
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.
Re: creating database and user
Posted: Thu Sep 24, 2009 6:12 am
by jashankoshal
thanks again..
can plz tell me how to make "error_reporting level to E_ALL"???
Re: creating database and user
Posted: Thu Sep 24, 2009 6:51 am
by dejvos
and setup php.ini
error_reporting = E_ALL
Re: creating database and user
Posted: Thu Sep 24, 2009 7:17 am
by jashankoshal
its not working...
i donno what to do...
my code is working on WAMP...but it is not working on WEB SERVER.
Re: creating database and user
Posted: Thu Sep 24, 2009 2:41 pm
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?
Re: creating database and user
Posted: Thu Sep 24, 2009 3:57 pm
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!