You should be learning, developing, and debugging code/query(ies) on a localhost development system and only put it onto a live/public server once you have a complete and secure application.
However, thereās nothing in the posted code that is php7+ specific or that would prevent a database connection using the PDO extension from working.
If you werenāt able to connect to the database server, the problem is in the connection values you are using, and you should have been getting an uncaught php exception that would contain information that would help pin down the problem. If there was no connection related error displayed or logged, you need to set phpās error_reporting to E_ALL and either set display_errors to ON or set log_errors to ON.
For fasthosts, it appears that the host value should be the database serverās ip address (if you used your web siteās ip address, that is not correct.) The database name is whatever you picked when you created the database. The username and password should be whatever you picked when you created the database user and you shouldnāt have been able to pick ārootā as the username and if you were able to, pick some other less common and more unique username.
The database connection code needs to also -
- Set the character set to match the character set being used by your database tables (UTF8 in a common/good choice.)
- Set emulated prepared queries to false.
- Set the error mode to exceptions, so that all the rest of the PDO database statements will throw exceptions.
- Set the default fetch mode to assoc, so that you donāt have to specify a fetch mode in most statements.
Here is an example of typical PDO connection code -
<?php
// fill in your connection values -
$DB_HOST = ''; // db hostname or ip address
$DB_USER = ''; // db username
$DB_PASS = ''; // db password
$DB_NAME = ''; // db name
$DB_ENCODING = 'utf8'; // db character encoding
$pdo = new pdo("mysql:host=$DB_HOST;dbname=$DB_NAME;charset=$DB_ENCODING",$DB_USER,$DB_PASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // set the error mode to exceptions
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); // run real prepared queries
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); // set default fetch mode to assoc
Hereās specifically what the code is doing incorrectly for the prepared query - itās putting external/unknown data directly into the sql query statement (which is how sql injection is accomplished) via a php variable, then has php statements like it is using an actual prepared query, which will function and query for and retrieve the data, but is not secure. The code should instead be showing you how to put a ? place-holder in the sql query statement where the data goes, then supply the data when the query gets executed (the simplest method of which is to supply an array of data in the execute() method call.)
The code is also having the programmer (you) manually write out navigation links (a correct content management system would dynamically produce navigation) and is creating unnecessary variables by copying variables to other variables without a good reason.