I know this is a begginer question but I did not find any good tutorials/advices so StackOverflow was my last hope.
I'm new to programming and I'm trying to make a website that uses a MySQL database to store users data such as login info, their content in the site, etc.
What I would like to know is how to safely and propely link the MySQL DB to my website and get/send data from/to it.
Now, my server looks like this:
www
|__ public_html (my site html, assets, css and js)
|__ php (where I think I should put my php files, outside the public http)
Inside the php
folder, I have a config.ini
file that contains the DB's address, username and password. Also inside this folder, I have my single .php
file, that looks like this:
<?php
function db_connect() {
static $connection;
if(!isset($connection)) {
$config = parse_ini_file('config.ini');
$connection = mysqli_connect($config['host'],$config['username'],$config['password'],$config['dbname']);
}
if($connection === false) {
return mysqli_connect_error();
}
return $connection;
}
How can I start this connection in my website and use it (send/get data) in a safe way so I don't get attacked throught a SQL Injection?
EDIT: I'm hosting my site with a company and the PHP version is 7.2 but I can change it. I also have at my root
folder more files/folder that weren't created by me, like .bash_profile
, php.ini
, and .gemrc