Building Arduino Projects for the Internet of Things

(Steven Felgate) #1
CHAPTER 8 ■ IOT PATTERNS: WEB APPS

Figure 8-6. Common database connectivity file called util-dbconn.php


Open the file in a text editor and copy or type code from Listing 8-2. As you can
see, there is not much code in this file. The four variables $servername , $username ,
$password , and $dbname contain the connection information. Create a new connection by
passing these four variables and store the connection reference in the $mysqli variable.
The IF condition in the code simply checks for errors during the connection attempt
and prints them if there were any.


Listing 8-2. Common Database Connectivity Code util-dbconn.php


<?php
$servername = "SERVER_NAME";
$dbname = "DB_NAME";
$username = "DB_USERNAME";
$password = "DB_PASSWORD";


//Open a new connection to MySQL server
$mysqli = new mysqli($servername, $username, $password, $dbname);


//Output connection errors
if ($mysqli->connect_error)
{
die("[ERROR] Connection Failed: ". $mysqli->connect_error);
}
?>

Free download pdf