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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rohitsavalia
    New Member
    • Apr 2007
    • 8

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

    <?
    //phpmyadmin version on server 4.1.21

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

    $dbname = 'testdb1';
    mysql_query("CR EATE 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
  • teesha
    New Member
    • May 2007
    • 4

    #2
    hi!
    i think you need to select an existing database like this
    $db=mysql_conne ct($server,$use r,$passwd)or die("mysql_erro r());
    mysql_select_db ($mydatabase,$d b)or die("mysql_erro r());

    and then you can create a new database

    mysql_query("CR EATE DATABASE $my_new_databas e")
    or die("Couldn't Create Database: $my_new_databas e");


    i try it and run smoothly.

    Comment

    • rohitsavalia
      New Member
      • Apr 2007
      • 8

      #3
      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("CR EATE ...");... 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.

      Comment

      • devsusen
        New Member
        • Feb 2007
        • 136

        #4
        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

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Originally posted by rohitsavalia
          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?

          Comment

          Working...