Building Arduino Projects for the Internet of Things

(Steven Felgate) #1
CHAPTER 7 ■ IOT PATTERNS: ON-DEMAND CLIENTS

Start a text editor of your choice.

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


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


Database Connection


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


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


Open the file in a text editor and copy or type the code from Listing 7-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 storing 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 7-2. Common Database Connectivity Code util-dbconn.php


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

Free download pdf