CREATE DATABASE via a PHP script?

Asked

Viewed 103 times

1

How could I create a database every time for example a user, in this case, a company registers on my web system? Like, today, the only thing I have is administrative access data to FTP and database data like host, port ...

How could you within an administrative interface create a complete database for a client from CREATE DATABASE? What are the necessary requirements and what the hosting service could hinder this procedure?

This curiosity came to me by the following fact: a company like Conta Azul for example has a whole financial administrative system and I imagine that for each client is installed a new bank with a new dump only with the tables but I have no idea how this is done.

In this case, if each client actually had a personal database and that this database was created through a simple registration of personal and legal information, how to arrive at the expected result without having to access the hosting or the phpmyadmin or any enabler.

  • 1

    First: There is no need to create a new dump. If you predict in your relationships that there will be a "Customer" entity you can normalize your bank for this. Second: In any decent DBMS you can create bases via SQL script, just connect with a login with the proper permissions. I don’t believe that hosting (especially shared) allows for this approach

  • That’s sort of what I’d like to get at. @jean so shared hosting probably and certainly won’t allow for such action?

  • 1

    It would probably require hosting with a dedicated server but the best place to ask is at the hosting companies themselves (remember that they want to sell the service). But the central point in the comment is that your approach may not be the right one

1 answer

2


If your user has Create database Grant you can normally run as the example below:

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$sql = 'CREATE DATABASE my_db';
if (mysql_query($sql, $link)) {
    echo "Database my_db created successfully\n";
} else {
    echo 'Error creating database: ' . mysql_error() . "\n";
}
?>

Of course, the right thing would be to work with the right bank modeling to use the same bank for several companies.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.