How to connect to MySQL using PHP
Connecting to MySQL via PHP is the most popular way of using MySQL databases on free hosting. Almost all dynamic websites (Websites that show different content to each user, such as the TinkerHost client area) use databases.
There are two ways to connect to MySQL using PHP, using the MySQLi library, or using PDO. You may want to start with a simple project, such as a basic MySQL exercise.
In order to use a database, you must create it first. In the control panel, look for the “Database” section and open it. In the top form, enter any name you want (We recommend using lowercase letters only without spaces), and click create.
In order to use PHP to connect to MySQL, you will need the proper database information. Listed below is the information you will need, and how to find it.
Database server - Can be found under the “MySQL” section in the client panel
Database username - Can be found under the “MySQL” section in the client panel
Database password - Can be found under the “MySQL” section in the client panel
Database name - Can be found in the “Database” section of the control panel (Where you created the database)
Lastly, make sure that your website and the database are hosted on the same account, otherwise the connection will fail. We do not support connecting to your MySQL database from an external application.
For example, here is the PHP code needed to connect to your MySQL database:
if(!isset($connect)){
try {
$servername = "sqlxxx.thsite.top";
$dbname = "DataBaseName";
$username = "thsi_12345678";
$password = "pAssW0rd!";
$connect = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception, and set $sql->fetch() to ASSOC
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connect->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
}