0

I have 2 ways in mind to setup a database on a remote server:
1. Having a PHP Script setup the tables and insert some pre-determined values:

<?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // sql to create table
    $sql = "CREATE TABLE MyGuests (
            id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            firstname VARCHAR(30) NOT NULL,
            lastname VARCHAR(30) NOT NULL,
            email VARCHAR(50),
            reg_date TIMESTAMP)";

    if ($conn->query($sql) === TRUE) {
        echo "Table MyGuests created successfully";
    } else {
        echo "Error creating table: " . $conn->error;
    }

    $conn->close();
?>
  1. Have a SQL file, that will contain all the queries required to setup the database.

What are the advantages and disadvantages of each method?

LatentDenis
  • 2,839
  • 12
  • 48
  • 99
NotGI
  • 458
  • 9
  • 21
  • 1
    You shouldn't put credential to access to db in this way , check the [link](https://stackoverflow.com/questions/45060696/query-doesnt-working-using-php/45061004#45061004) to know how to do it – Frank Jul 12 '17 at 18:13
  • @Frank, it's just an example code for w3schools. – NotGI Jul 12 '17 at 19:12

2 Answers2

0

I agree to what Frank commented, you must create a configuration file for database credentials.

As far as creating a database on a remote server is concerned, regardless of whichever method you choose, your purpose will be solved.

I see you are using PHP-SQL, so if you have PHPMYADMIN already installed then it will be very easy for you to import the .sql file directly.

I recommend using Phpmyadmin as it will make it much easier for you to perform sql activities with a UI. If you do not have Phpmyadmin here is a link to install it : https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-on-ubuntu-14-04

Good luck!

Aaditya Damani
  • 355
  • 1
  • 2
  • 12
0

It is much more secure to create your tables and database from the Administrative level which is phpmyadmin here.

Joel Wembo
  • 814
  • 6
  • 10