Building Arduino Projects for the Internet of Things

(Steven Felgate) #1

CHAPTER 9 ■ IOT PATTERNS: LOCATION AWARE


This project also uses PHP for building the storage and user interface services.
Create a new folder called gpstracker in the public/root folder of your PHP server.
All of the PHP source code for this project will go in this gpstracker folder.
Start the text editor of your choice.


■ Note All the PHP code was developed using Brackets, which is an open source text


editor. See http://brackets.io/ for more information.


Database Connection


Both PHP scripts for storing and displaying data will need to connect to the database. As
shown in Figure  9-6 , create a new file called util-dbconn.php in the gpstracker folder.
This file will be used by both scripts instead of repeating the code.


Open the file in a text editor and copy or type code from Listing 9-2. As you can
see, there is not much code in this file. The four variables $servername , $username ,
$password , and $dbname contain 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 are any.


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


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


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

Free download pdf