By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

I can't create New Database using PHP command on server. My coding is here...

P: 8
<?
//phpmyadmin version on server 4.1.21

$DBSERVER = "localhost";
//$DATABASENAME = "test";
$USERNAME = "username";
$PASSWORD = "password";
$db=mysql_connect($DBSERVER, $USERNAME, $PASSWORD);

$dbname = 'testdb1';
mysql_query("CREATE DATABASE $dbname") or die("Couldn't Create Database: $dbname");

echo mysql_error();
?>

// THIS GIVES ME ERROR "Couldn't create Database : testdb1"
// i can create databse using this username and password through control panel of server.. why can't through PHP
May 4 '07 #1
Share this Question
Share on Google+
4 Replies


P: 4
hi!
i think you need to select an existing database like this
$db=mysql_connect($server,$user,$passwd)or die("mysql_error());
mysql_select_db($mydatabase,$db)or die("mysql_error());

and then you can create a new database

mysql_query("CREATE DATABASE $my_new_database")
or die("Couldn't Create Database: $my_new_database");


i try it and run smoothly.
May 4 '07 #2

P: 8
Thanks teesha,

very thanks for your reply, but u don't understand my question...

you said that first I select db using mysql_select_db... ok
then I create new db using mysql_query("CREATE ...");... ok

Now please tell me how can a database create with in existing database..

I just want to create a fresh new database when my user registered firstime.
For that i have to direct create a fresh database with random name and after that have to create predefined tables in that...


So, if you have any solution of that then please tell me.
May 4 '07 #3

100+
P: 136
Hi,

I don't think there is no such MySql function in php using that u can create a database. U can only drop a database.

IMO if u like to create a database in MySql only using php, then u need to execute direct mysql command using passthru() or exec() function.

susen
May 5 '07 #4

pbmods
Expert 5K+
P: 5,821
I just want to create a fresh new database when my user registered firstime.
For that i have to direct create a fresh database with random name and after that have to create predefined tables in that...
Sounds like you might want to rethink your database design. One of the points of using MySQL is that you *don't* have to create a whole new database for each User. If you wanted to do that, you could just save data in text files in numbered folders on your server (not recommended).

Is it necessary for every User to have his own database (which would get VERY expensive on a shared server, btw), or could the problem be solved by adding a `userid` field to key tables to associate that data with a particular User?
May 5 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.