1

I am making a website that will use a MySQL database on a friend's web hosting service. It supports PHP and MySQL but I'm not sure what file I am meant to create the database and table in. I understand the syntax so I just need to know where and which files to do it in. I guess you don't put it in the website's html files because it only needs to be created once.

I have the following code:

<?php
$servername = "localhost";
$username   = "username";
$password   = "password";
$conn       = mysqli_connect($servername, $username, $password);
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
$sql = "CREATE DATABASE LeagueData";
if (mysqli_query($conn, $sql)) {
    echo "Database created successfully";
} else {
    echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
11
  • 2
    What have you tried? You'll have to do some work yourself before asking a question here. Commented Nov 27, 2016 at 13:03
  • What do you want to achieve? if you just wanna the simple Email login. you don't even need a database. Just create a CSV file would do the trick. Commented Nov 27, 2016 at 13:13
  • Personally I would develop the db on my pc using a mysql gui and when happy back it up and restore to the target system. Commented Nov 27, 2016 at 13:13
  • Strictly speaking, if you're making a publicly available webpage, and have this little knowledge, I would not recommend you make a PHP/MySQL-driven site. It will end in disaster (or at minimum, a potential disaster), with no knowledge of databases and security. Commented Nov 27, 2016 at 13:26
  • 1
    Your friend should have a control panel for his hosting. Two of the most famous panels are cPanel and Plesk or maybe his panel is something else. anyway, his hosting company should have send him the information of his panel to him by email when he had purchased it. In that panel you have a section related to DB stuff. You can create database and users and then you can have access to some tools like phpMyAdmin for administrating your database. Commented Nov 27, 2016 at 17:07

2 Answers 2

1

You don't have to create a file. You have to create the DB and structure from within MySQL (or you can load a .sql file into MySQL).

If he has phpMyAdmin use that. It's much easier.

Creating a table from within MySQL: http://dev.mysql.com/doc/refman/5.7/en/creating-tables.html
Importing an .sql file from the commandline: mysql -u username -p database_name < file.sql

Sign up to request clarification or add additional context in comments.

3 Comments

What do you mean by 'within MySQL'? sorry if this is really dumb.
from within the application:Easier directly from the command line: mysql -u username -p database_name < file.sql
I've added samples to the original response.
0

Adding on to Patrick's answer:

You need to create your database first, and not through PHP. mysqli allows you to connect to a particular database (which you need to specify in your code), and then execute queries on that database.

First, create a database. You should consult your web-hosting provider on how to do this. Most web-hosts allow you to create a database through one of the CPanel modules. (You'll also have to create a "user", and assign that user to the newly created database.)

Once you have created the database, you will be able to connect to it via PHP. Your code to connect to the database should look like:

$conn = mysqli_connect($servername, $username, $password, $database_name);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.