PHP Lab Manual
User Manual:
Open the PDF directly: View PDF
.
Page Count: 115
| Download | |
| Open PDF In Browser | View PDF |
Integrating PHP with Embedded System Integrating PHP with Embedded System www.researchdesignlab.com Page 1 Integrating PHP with Embedded System Contents What is PHP? .......................................................................................................................................... 6 Basic PHP Syntax ................................................................................................................................. 6 Hello World example .......................................................................................................................... 6 Error Management ................................................................................................................................ 8 Finding errors present in the program ............................................................................................... 9 Comments in PHP ................................................................................................................................ 12 PHP is a Loosely Typed Language ...................................................................................................... 12 PHP Variables Example ....................................................................................................................... 12 Global and locally-scoped variables ................................................................................................... 13 Example for Global and locally-scoped variables ............................................................................. 13 Static Keyword in PHP ........................................................................................................................ 14 Static Keyword Example.................................................................................................................... 14 ECHO and PRINT statements in PHP ................................................................................................. 15 PRINT Statement Example in PHP ..................................................................................................... 15 String Functions in PHP ...................................................................................................................... 16 1. strlen() function ........................................................................................................................ 16 2. strpos() function........................................................................................................................ 16 Constant in PHP ................................................................................................................................... 17 Constant string Example ................................................................................................................... 17 PHP Example to calculate the area of the circle ............................................................................... 17 Arithmetic Operators .......................................................................................................................... 18 Arithmetic Operators Example ......................................................................................................... 19 Increment and Decrement Operators ................................................................................................ 20 Increment and Decrement Operators Example ................................................................................ 20 Assignment Operators in PHP ............................................................................................................ 21 Assignment Operators Example........................................................................................................ 21 String Operators in PHP ...................................................................................................................... 23 String Operators Example ................................................................................................................. 23 The if Statement in PHP ...................................................................................................................... 24 The if Statement Example ................................................................................................................. 24 The if…else Statement in PHP ............................................................................................................ 25 The if…else Statement Example........................................................................................................ 25 The if…elseif…else Statement in PHP ................................................................................................ 26 www.researchdesignlab.com Page 2 Integrating PHP with Embedded System The if…elseif…else Statement Example (Comparing two numbers) ................................................. 26 Switch Statement in PHP .................................................................................................................... 27 Switch Statement Example ............................................................................................................... 27 For loop in PHP .................................................................................................................................... 28 For loop Example .............................................................................................................................. 28 Declaring multiple variables in for loop Example ............................................................................. 29 While Loop in PHP............................................................................................................................... 30 While Loop Example ......................................................................................................................... 30 Do While loop in PHP .......................................................................................................................... 31 Do While loop Example ..................................................................................................................... 31 User Defined Function Example........................................................................................................ 32 Swap Numbers PHP Example ............................................................................................................. 33 PHP Functions - Adding parameters .................................................................................................. 34 PHP Functions - Return values ........................................................................................................... 35 Break statement .................................................................................................................................. 35 Break statement example ................................................................................................................. 35 Continue statement ............................................................................................................................. 36 Continue statement example ........................................................................................................... 36 PHP Global Variables - Superglobals ................................................................................................. 36 $GLOBALS.......................................................................................................................................... 37 Example:............................................................................................................................................ 37 $_SERVER .......................................................................................................................................... 38 Example ............................................................................................................................................. 38 Array in PHP .......................................................................................................................................... 39 Numeric Array in PHP ....................................................................................................................... 39 Numeric Array Example .................................................................................................................... 39 Associative array in PHP........................................................................................................................ 40 Associative array Example ................................................................................................................ 40 Loop through an Associative Array ....................................................................................................... 41 Multidimensional array in PHP ............................................................................................................. 42 Multidimensional array Example ...................................................................................................... 42 PHP Forms ............................................................................................................................................. 43 The $_GET Function .......................................................................................................................... 43 www.researchdesignlab.com Page 3 Integrating PHP with Embedded System The $_POST Function ........................................................................................................................ 43 The $_GET Function Example ........................................................................................................... 44 The $_ POST Function Example ........................................................................................................ 46 Another Example for PHP form......................................................................................................... 48 Date() and time() function in PHP ......................................................................................................... 51 Example ............................................................................................................................................. 51 How to connect to MYSQL database using PHP ................................................................................... 52 The functions used to connect web form to the MYSQL database: ..................................................... 60 Display the data from MYSQL database in web form ........................................................................... 61 Insert the data into MYSQL database using web form ......................................................................... 63 Update the data present in MYSQL database using web form............................................................. 66 Delete the data from MYSQL database using web form ...................................................................... 70 Hosting your domain using cPanel........................................................................................................ 74 EMBEDDED SYSTEM WITH PHP ............................................................................................................ 86 OVERVIEW......................................................................................................................................... 86 FEATURES .......................................................................................................................................... 86 APPLICATION DIAGRAM .................................................................................................................... 86 CIRCUIT DIAGRAM ............................................................................................................................ 87 PIN CONNECTION .............................................................................................................................. 88 INTERFACE......................................................................................................................................... 89 LIBRARY ............................................................................................................................................. 89 CODE ................................................................................................................................................. 90 With Arduino ............................................................................................................................. 90 1. ADD hello world example from Examples ................................................................................ 90 2. Add Server Reading Example .................................................................................................... 90 3. relay interface(Web Remote) ................................................................................................... 91 .............................................................................................................................................................. 91 code:.................................................................................................................................................. 91 PIN CONNECTION .............................................................................................................................. 95 WITH ETHERNET MODULE ................................................................................................................ 95 WITH RELAY....................................................................................................................................... 96 CODE ................................................................................................................................................. 98 PHP CODE: ....................................................................................................................................... 102 www.researchdesignlab.com Page 4 Integrating PHP with Embedded System PIN CONNECTION ............................................................................................................................ 106 code................................................................................................................................................. 107 www.researchdesignlab.com Page 5 Integrating PHP with Embedded System What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language PHP scripts are executed on the server PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP is an open source software PHP is free to download and use Basic PHP Syntax A PHP script starts with The default file extension for PHP files is ".php" A PHP file normally contains HTML tags, and some PHP scripting code PHP statements are terminated by semicolon (;) In PHP, all user-defined functions, classes, and keywords (e.g. if, else, while, echo, etc.) are not case-sensitive Hello World example Go to htdocs folder which is present in the apache2triad installed folder. There create a folder and save this program with .php extension such as Hello.php. www.researchdesignlab.com Page 6 Integrating PHP with Embedded System C:\apache2triad\htdocs\MyPHPProgram To execute hello world program, type in the address bar as follows: http://localhost/MyPHPProgram/hello.php http://localhost/MyPHPProgram/hello.php www.researchdesignlab.com Page 7 Integrating PHP with Embedded System Error Management 1. Compile-time errors: Compile-time errors are detected by the parser while it is compiling a script. The compile-time errors cannot be trapped from within the script itself 2. Fatal errors: Fatal errors are the errors that halt the execution of a script. The fatal errors cannot be trapped. 3. Recoverable errors: Recoverable errors that represent significant failures, but can still be handled in a safe way. 4. Warnings: Warnings are recoverable errors that indicate a run-time fault. Warnings do not halt the execution of the script. 5. Notices: Notices indicate that an error condition occurred, but is not necessarily significant. Notices do not halt the execution of the script. www.researchdesignlab.com Page 8 Integrating PHP with Embedded System Finding errors present in the program To find the errors present in the program go to: Start -> All programs -> Apache2triad -> Apache2TriadCP Apache2TriadCP www.researchdesignlab.com Page 9 Integrating PHP with Embedded System Then click on “PHP Error log” PHP Error log www.researchdesignlab.com Page 10 Integrating PHP with Embedded System The list of errors in the program is displayed along with the line number where the error has occurred. [05-Jan-2007 05:21:11] PHP Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\apache2triad\htdocs\MyPHPProgram\hello.php on line 4 www.researchdesignlab.com Page 11 Integrating PHP with Embedded System Comments in PHP // Single line comment (C++ and Java-style comment) # Single line comment (Shell-style comments) /* Multiple line comment (C-style comments) */ PHP is a Loosely Typed Language In PHP, a variable does not need to be declared before adding a value to it PHP automatically converts the variable to the correct data type, depending on its value In PHP, the variable is declared automatically when you use it PHP variables must begin with a “$” sign Variables are used for storing values, like text strings, numbers or arrays The correct way of declaring a variable in PHP: $var_name = value; PHP Variables Example 25; // Numerical variable ‚Hello‛; // String variable 5.7; // Float variable ‚Number is : ‛.$a.‚welcome.php // $_GET to receive the data sent from Form1.html Welcome .
‛; ‚String is : ‛.$b.‚
‛; ‚Float value : ‛.$c; OUTPUT of the above given Example is as follows: Number is : 25 String is : Hello Float value : 5.7 www.researchdesignlab.com Page 12 Integrating PHP with Embedded System Global and locally-scoped variables Global variables can be used anywhere Local variables restricted to a function or class Example for Global and locally-scoped variables "; echo "Variable y is: $y"; } myFunction();// Function call echo "Variable x is: $x"; echo "
"; echo "Variable y is: $y"; ?> OUTPUT of the above given Example is as follows: Variable x is: Variable y is: 59 Test variables outside the function: Variable x is: 24 Variable y is: www.researchdesignlab.com Page 13 Integrating PHP with Embedded System Static Keyword in PHP Static keyword is used when you first declare the variable Each time the function is called, that variable will still have the information it contained from the last time the function was called Static Keyword Example "; $x++; } // Function call myFunction(); myFunction(); myFunction(); myFunction(); myFunction(); ?> OUTPUT of the above given Example is as follows: 45 46 47 48 49 www.researchdesignlab.com Page 14 Integrating PHP with Embedded System ECHO and PRINT statements in PHP ECHO - It can output one or more strings PRINT – It can only output one string, and returns always 1 ECHO is faster compared to PRINT as echo does not return any value ECHO is not a function and, as such, it does not have a return value If you need to output data through a function, you can use PRINT() instead: Example: echo 50; print (50); PRINT Statement Example in PHP ***********"; ?> OUTPUT of the above given Example is as follows: Hello world! *********** www.researchdesignlab.com Page 15 Integrating PHP with Embedded System String Functions in PHP strlen() function strpos() function 1. strlen() function The strlen() function returns the length of a string, in characters OUTPUT of the above given Example is as follows: 12 2. strpos() function The strpos() function is used to search for a specified character or text within a string OUTPUT of the above given Example is as follows: 6 www.researchdesignlab.com Page 16 Integrating PHP with Embedded System Constant in PHP define() function is used to set a constant It takes three parameters they are: 1. Name of the constant 2. Value of the constant 3. Third parameter is optional. It specifies whether the constant name should be case-insensitive. Default is false Constant string Example OUTPUT of the above given Example is as follows: Hello Friend PHP Example to calculate the area of the circle OUTPUT of the above given Example is as follows: Area=706.5 www.researchdesignlab.com Page 17 Integrating PHP with Embedded System Arithmetic Operators Arithmetic operators allow performing basic mathematical operations Operator Description Example Result + Addition $a = 2 + 5; $a=7 - Subtraction $a = 10 - 2; $a=8 * Multiplication $a = 2 * 5; $a=10 / Division $a = 15 / 5; $a=3 % Modulus $a = 23 % 7; $a=3.28 ++ Increment $a =5; $a=6 $a ++; -- Decrement $a =5; $a=4 $a --; www.researchdesignlab.com Page 18 Integrating PHP with Embedded System Arithmetic Operators Example "; echo "j = ".$j."
"; echo "k = ".$k."
"; echo "l = ".$l."
"; echo "m = ".$m."
"; ?> OUTPUT of the above given Example is as follows: i = 30 j = 25 k = 100 l = 50 m=0 www.researchdesignlab.com Page 19 Integrating PHP with Embedded System Increment and Decrement Operators Operator Name Description ++$a Pre-increment Increments $a by one, then returns $a $a++ Post-increment Returns $a, then increments $a by one --$a Pre-decrement Decrements $a by one, then returns $a $a-- Post-decrement Returns $a, then decrements $a by one Increment and Decrement Operators Example "; echo $j."
"; // Post increment $k=$i++; // Pre increment $l=++$j; echo $k."
"; echo $l; ?> OUTPUT of the above given Example is as follows: 11 21 11 22 www.researchdesignlab.com Page 20 Integrating PHP with Embedded System Assignment Operators in PHP Assignment operator is used to write a value to a variable Operator Example Is the same as = x=y x=y += x+=y x=x+y -= x-=y x=x-y *= x*=y x=x*y /= x/=y x=x/y .= x.=y x=x.y %= x%=y x=x%y Assignment Operators Example "; $b=10; $b += 20; echo "b=".$b; echo "
"; $c=15; $c -= 5; echo "c=".$c; echo "
"; $d=20; $d *= 2; echo "d=".$d; echo "
"; www.researchdesignlab.com Page 21 Integrating PHP with Embedded System $e=25; $e /= 5; echo "e=".$e; echo "
"; $f=30; $f %= 4; echo "f=".$f; ?> OUTPUT of the above given Example is as follows: a=5 b=30 c=10 d=40 e=5 f=2 www.researchdesignlab.com Page 22 Integrating PHP with Embedded System String Operators in PHP Operator Name Example Result . Concatenation $a = "Hello" $b = $a . " world!" $b = "Hello world!" .= Concatenation Assignment $a = "Hello" $a .= " world!" $a = "Hello world!" String Operators Example "; $c="Good"; $c .= " Day!"; echo $c; ?> OUTPUT of the above given Example is as follows: Hello Friend! Good Day! www.researchdesignlab.com Page 23 Integrating PHP with Embedded System The if Statement in PHP If statement executes some code only if a specified condition is true Syntax: if (condition) { code to be executed if condition is true; } The if Statement Example OUTPUT of the above given Example is as follows: i is 0 www.researchdesignlab.com Page 24 Integrating PHP with Embedded System The if…else Statement in PHP If…else statement executes some code if a condition is true and some another code if the condition is false Syntax: if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; } The if…else Statement Example OUTPUT of the above given Example is as follows: i is not 0 www.researchdesignlab.com Page 25 Integrating PHP with Embedded System The if…elseif…else Statement in PHP If…elseif…else statement selects one of several blocks of code to be executed Syntax: if (condition) { code to be executed if condition is true; } elseif (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; } The if…elseif…else Statement Example (Comparing two numbers) $j) echo "i is greater"; //statement1 elseif($i<$j) echo "j is greater"; //statement2 else echo "numbers are equal"; //Statement3 ?> OUTPUT of the above given Example is as follows: numbers are equal www.researchdesignlab.com Page 26 Integrating PHP with Embedded System Switch Statement in PHP Switch statement selects one from multiple blocks of code to be executed Syntax: switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; ... default: code to be executed if n is different from all labels; } Switch Statement Example www.researchdesignlab.com Page 27 Integrating PHP with Embedded System OUTPUT of the above given Example is as follows: Number 3 For loop in PHP PHP for loop executes a block of code, a specified number of times Syntax: for (initialization; test condition; increment/decrement) { code to be executed; } For loop Example "; /*in for loop, initialization usually declares a loop variable, condition is a Boolean expression such that if the condition is true, loop body will be executed and after each iteration of loop body, expression is executed which usually increase or decrease loop variable*/ for ($x=0; $x<=20; $x++) { echo "$x "; } ?> OUTPUT of the above given Example is as follows: Numbers from 1 to 20 are: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 www.researchdesignlab.com Page 28 Integrating PHP with Embedded System Declaring multiple variables in for loop Example "; } ?> OUTPUT of the above given Example is as follows: x = 0, y = 1, z = 2 x = 1, y = 1, z = 2 x = 2, y = 1, z = 2 x = 3, y = 1, z = 2 www.researchdesignlab.com Page 29 Integrating PHP with Embedded System While Loop in PHP While loop, loops through a block of code as long as the specified condition is true Syntax: while (condition) { code to be executed; } While Loop Example is a Boolean expression. Loop body is executed as long as condition is true*/ while($i<5){ echo "i is = $i
"; $i++; } ?> OUTPUT of the above given Example is as follows: i is = 1 i is = 2 i is = 3 i is = 4 www.researchdesignlab.com Page 30 Integrating PHP with Embedded System Do While loop in PHP Do while loop will always execute the block of code once, it will then check the condition, and if the condition is true then it repeats the loop Syntax: do { code to be executed; } while (condition ); Do While loop Example is a Boolean expression. Please note that the condition is evaluated after executing the loop body. So loop will be executed at least once even if the condition is false*/ do { echo "i is = $i
"; $i++; }while($i<5); ?> OUTPUT of the above given Example is as follows: i is = 1 i is = 2 i is = 3 i is = 4 www.researchdesignlab.com Page 31 Integrating PHP with Embedded System User Defined Function in PHP Functions are group of statements that can perform a task Syntax: function functionName() { code to be executed; } User Defined Function Example OUTPUT of the above given Example is as follows: Hello world www.researchdesignlab.com Page 32 Integrating PHP with Embedded System Swap Numbers PHP Example "; echo "Num1=".$num1; echo "
Num2=".$num2; // Function call swap($num1,$num2); // Function definition function swap($n1,$n2) { $temp=$n1; $n1=$n2; $n2=$temp; echo "
Numbers after swapping:
"; echo "Num1=".$n1; echo "
Num2=".$n2; } ?> OUTPUT of the above given Example is as follows: Numbers before swapping: Num1=10 Num2=20 Numbers after swapping: Num1=20 Num2=10 www.researchdesignlab.com Page 33 Integrating PHP with Embedded System PHP Functions - Adding parameters "; } echo "My name is "; writeName("Kai Jim"); //Function call echo "My sister's name is "; writeName("Hege"); // Function call echo "My brother's name is "; writeName("Stale"); // Function call ?> OUTPUT of the above given Example is as follows: My name is Kai Jim Refsnes. My sister's name is Hege Refsnes. My brother's name is Stale Refsnes. www.researchdesignlab.com Page 34 Integrating PHP with Embedded System PHP Functions - Return values OUTPUT of the above given Example is as follows: 1 + 16 = 17 Break statement Break statement is used to terminate the loop After the break statement is executed the control goes to the statement immediately after the loop containing break statement Break statement example OUTPUT of the above given Example is as follows: 012 www.researchdesignlab.com Page 35 Integrating PHP with Embedded System Continue statement There are cases in which, rather than terminating a loop, you simply want to skip over the remainder of iteration and immediately skip over to the next. Continue statement is used to skip a particular iteration of the loop. Continue statement example OUTPUT of the above given Example is as follows: 01245 PHP Global Variables - Superglobals "Superglobals" are predefined variables in PHP They are always accessible, regardless of scope - and can access them from any function, class or file The PHP superglobal variables are: $GLOBALS $_SERVER $_REQUEST $_POST $_GET $_FILES $_ENV $_COOKIE $_SESSION www.researchdesignlab.com Page 36 Integrating PHP with Embedded System $GLOBALS $GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods) PHP stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable Example: OUTPUT of the above given Example is as follows: 60 www.researchdesignlab.com Page 37 Integrating PHP with Embedded System $_SERVER $_SERVER is a PHP super global variable which holds information about headers, paths, and script locations Example $_SERVER['PHP_SELF']; "
"; $_SERVER['SERVER_NAME']; "
"; $_SERVER['HTTP_HOST']; "
"; $_SERVER['HTTP_USER_AGENT']; "
"; $_SERVER['SCRIPT_NAME']; OUTPUT of the above given Example is as follows: /User/server.php localhost localhost Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.5; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET CLR 1.1.4322) /User/server.php www.researchdesignlab.com Page 38 Integrating PHP with Embedded System Array in PHP An array stores multiple values in one single variable In PHP, there are three kinds of arrays: Numeric array Associative array Multidimensional array Numeric Array in PHP Numeric array is an array with a numeric index Numeric Array Example OUTPUT of the above given Example is as follows: Flowers: rose, daisy, orchid www.researchdesignlab.com Page 39 Integrating PHP with Embedded System Associative array in PHP Associative array is an array where each ID key is associated with a value Associative array Example "5.00", "daisy" => "4.00", "orchid" => "2.00" ); // Display the array values echo "rose costs .$flower_shop['rose'].",daisy costs ".$flower_shop['daisy'].",and orchild costs ".$flower_shop['orchild'].""; ?> OUTPUT of the above given Example is as follows: rose costs 5.00,daisy costs 4.00,and orchild costs www.researchdesignlab.com Page 40 Integrating PHP with Embedded System Loop through an Associative Array "5.00", "daisy"=>"4.00","orchid"=>"2.00"); /* for each loop works only on arrays, and is used to loop through each key/value pair in an array */ foreach($flower_shop as $x=>$x_value) { echo "Flower=" . $x . ", Value=" . $x_value; echo "
"; } ?> OUTPUT of the above given Example is as follows: Flower=rose, Value=5.00 Flower=daisy, Value=4.00 Flower=orchid, Value=2.00 www.researchdesignlab.com Page 41 Integrating PHP with Embedded System Multidimensional array in PHP Multidimensional array is an array containing one or more arrays Multidimensional array Example array( "5.00", "7 items", "red" ), "daisy" => array( "4.00", "3 items", "blue" ), "orchid" => array( "2.00", "1 item", "white" ), ); /* in the array $flower_shop['rose'][0], ‘rose’ indicates row and ‘0’ indicates column */ echo "rose costs ".$flower_shop['rose'][0]. ", and you get ".$flower_shop['rose'][1].".
"; echo "daisy costs ".$flower_shop['daisy'][0]. ", and you get ".$flower_shop['daisy'][1].".
"; echo "orchid costs ".$flower_shop['orchid'][0]. ", and you get ".$flower_shop['orchid'][1].".
"; ?> OUTPUT of the above given Example is as follows: rose costs 5.00, and you get 7 items. daisy costs 4.00, and you get 3 items. orchid costs 2.00, and you get 1 item. www.researchdesignlab.com Page 42 Integrating PHP with Embedded System PHP Forms Scripts will interact with their clients using one of the two HTTP methods. The methods are GET and POST When a form is submitted using the GET method, its values are encoded directly in the query string portion of the URL When a form is submitted using the POST method, its values will not be displayed the query string portion of the URL The $_GET Function The built-in $_GET function is used to collect values from a form sent with method="get" Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's URL) and has limits on the amount of information to send (max. 100 characters) This method should not be used when sending passwords or other sensitive information. However, because the variables are displayed in the URL, it is possible to bookmark the page The get method is not suitable for large variable values; the value cannot exceed 100 characters The $_POST Function The built-in $_POST function is used to collect values from a form sent with method="post" Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send However, there is an 8 Mb max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file) www.researchdesignlab.com Page 43 Integrating PHP with Embedded System The $_GET Function Example Form1.html /* form submitted using ‘get’ method, action specifies next page which is to be loaded when button is clicked*/
You are years old! www.researchdesignlab.com Page 44 Integrating PHP with Embedded System OUTPUT of the above given Example is as follows: In this example, when you click on the “submit” button, the values entered in the textbox are encoded directly in the query string portion of the URL. www.researchdesignlab.com Page 45 Integrating PHP with Embedded System The $_ POST Function Example form1.html /* form submitted using ‘post’ method, action specifies next page which is to be loaded when button is clicked */ welcome1.php // $_GET to receive the data sent from form1.html Welcome .
You are years old! www.researchdesignlab.com Page 46 Integrating PHP with Embedded System OUTPUT of the above given Example is as follows: In this example, when you click on the “submit” button, the values will not be displayed the query string portion of the URL. www.researchdesignlab.com Page 47 Integrating PHP with Embedded System Another Example for PHP form Form.html
"; echo "Your password: {$_POST['Password']}
"; echo "Your favourite season: {$_POST['Seasons']}
"; echo "You are from: {$_POST['Country']}
"; echo "Colors you chose: {$_POST['Colors']}
"; } else { echo "You can't see this page without submitting the form."; } ?> www.researchdesignlab.com Page 49 Integrating PHP with Embedded System OUTPUT of the above given Example is as follows: www.researchdesignlab.com Page 50 Integrating PHP with Embedded System Date() and time() function in PHP The PHP date() function formats a timestamp to a more readable date and time A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred Some characters that are commonly used for date and time: d - Represents the day of the month (01 to 31) m - Represents a month (01 to 12) Y - Represents a year (in four digits) l (lowercase 'L') - Represents the day of the week h - 12-hour format of an hour with leading zeros (01 to 12) i - Minutes with leading zeros (00 to 59) s - Seconds with leading zeros (00 to 59) a - Lowercase Ante meridiem and Post meridiem (am or pm) Example "; to display the day " . date("l"). "
"; time in the format HH:MM:SS is " . date("h:i:sa"); ?> OUTPUT of the above given Example is as follows: Today is 2014/08/19 Today is Tuesday The time is 09:13:22am www.researchdesignlab.com Page 51 Integrating PHP with Embedded System How to connect to MYSQL database using PHP To connect MYSQL using PHP go to: http://localhost//phpmyadmin Enter the username and password Give the database name in the field ‘create new database’ Click on create button www.researchdesignlab.com Page 52 Integrating PHP with Embedded System Create a new table in the database by giving a table name and number of fields then click on Go www.researchdesignlab.com Page 53 Integrating PHP with Embedded System To give field name to the created table, write the field name in the ‘field’ column, select the data types for each fields, specify the length of each field then click on save to save the fields and click on Go www.researchdesignlab.com Page 54 Integrating PHP with Embedded System When clicked on Go the table field details will be displayed www.researchdesignlab.com Page 55 Integrating PHP with Embedded System To insert values in the field, go to insert and enter the values. Then click on Go To view the created table, go to browse www.researchdesignlab.com Page 56 Integrating PHP with Embedded System To insert the values, go to SQL and write the query to insert the values and click on Go SQL query for insert: Syntax: Insert into table_name values(‘value1’,’value2’,…); Example: Insert into Login values(‘Radha’,’hello’); www.researchdesignlab.com Page 57 Integrating PHP with Embedded System To update the values, go to SQL and write the query to update the values and click on Go SQL query for update: Syntax: Update table_name set field_name=’value’ where field_name=’value’; Example: Update Login set password=’abcde’ where name=’Radha’; www.researchdesignlab.com Page 58 Integrating PHP with Embedded System To delete the values, go to SQL and write the query to delete the values and click on go SQL query for delete: Syntax: Delete from table_name where field_name=’value’; Example: Delete from Login where name=’Radha’; www.researchdesignlab.com Page 59 Integrating PHP with Embedded System The functions used to connect web form to the MYSQL database: mysql_connect(): This function opens a link to a MySQL server on the specified host (in this case it's localhost) along with a username (root) and password (q1w2e3r4/). The result of the connection is stored in the variable $db. mysql_select_db(): This tells PHP that any queries we make are against the mydb database. mysql_query(): Using the database connection identifier, it sends a line of SQL to the MySQL server to be processed. The results that are returned are stored in the variable $result. mysql_result(): This is used to display the values of fields from our query. Using $result, we go to the first row, which is numbered 0, and display the value of the specified fields. mysql_result($result,0,"position")): This should be treated as a string and printed. www.researchdesignlab.com Page 60 Integrating PHP with Embedded System Display the data from MYSQL database in web form "; echo "
", $PHP_SELF, $myrow["id"],$myrow["first"], $myrow["last"]); } } ?> www.researchdesignlab.com Page 67 Integrating PHP with Embedded System OUTPUT of the above given Example would be: www.researchdesignlab.com Page 68 Integrating PHP with Embedded System www.researchdesignlab.com Page 69 Integrating PHP with Embedded System Delete the data from MYSQL database using web form %s %s
", $PHP_SELF, $myrow["id"],$myrow["first"], $myrow["last"]); } } ?> OUTPUT of the above given Example would be: www.researchdesignlab.com Page 71 Integrating PHP with Embedded System www.researchdesignlab.com Page 72 Integrating PHP with Embedded System www.researchdesignlab.com Page 73 Integrating PHP with Embedded System Hosting your domain using cPanel Step 1: Register your required Domain name by visiting godaddy.com www.researchdesignlab.com Page 74 Integrating PHP with Embedded System Step 2: Buy required domain Space to upload your code www.researchdesignlab.com Page 75 Integrating PHP with Embedded System Step 3: Launch your cPanel – your-domain-name.com/cPanel Provide your cPanel username and password click on login www.researchdesignlab.com Page 76 Integrating PHP with Embedded System Step 4: Click on file Manger www.researchdesignlab.com Page 77 Integrating PHP with Embedded System Step 5: Click on Web root and Go www.researchdesignlab.com Page 78 Integrating PHP with Embedded System Step 6: Select the Public Folder_html www.researchdesignlab.com Page 79 Integrating PHP with Embedded System Step 7: Click on Upload www.researchdesignlab.com Page 80 Integrating PHP with Embedded System Step 8: Click on the choose file to upload your code Note: Once the code is uploaded your website will be live on internet. That you can check with open the browser with your-domain-name.com www.researchdesignlab.com Page 81 Integrating PHP with Embedded System Step 9: To create database – click on the phpMyAdmin www.researchdesignlab.com Page 82 Integrating PHP with Embedded System Step 10: Manage your Tables and database on phpMyAdmin console www.researchdesignlab.com Page 83 Integrating PHP with Embedded System Step 11: Importing Database or table by clicking on import www.researchdesignlab.com Page 84 Integrating PHP with Embedded System Step 12: Exporting your database to local machine www.researchdesignlab.com Page 85 Integrating PHP with Embedded System EMBEDDED SYSTEM WITH PHP OVERVIEW This Ethernet Breakout-Module is simplest way to add LAN connectivity to your microcontroller based products and projects. Use this module to enable Ethernet interface for your product. It works with any microcontroller operating at 3.3V or 5V. This module works at 3.3V and is compatible with 5V interface lines. FEATURES Brand new and high quality. Chip board ENC28J60-I/SO. The board 25MHZ crystal. The network interface board HR911105A. 3.3 V power supply pin. Size: 6cm x 3.2cm - 2.4inch x 1.28inch. High quality PCB FR4 Grade with FPT Certified APPLICATION DIAGRAM www.researchdesignlab.com Page 86 Integrating PHP with Embedded System CIRCUIT DIAGRAM www.researchdesignlab.com Page 87 Integrating PHP with Embedded System PIN CONNECTION www.researchdesignlab.com Page 88 Integrating PHP with Embedded System INTERFACE LIBRARY (add this library on your arduino folder in program files) https://drive.google.com/file/d/0BzrGD4zr88GnZFRRalNjU1E0UmM/view www.researchdesignlab.com Page 89 Integrating PHP with Embedded System CODE With Arduino 1. ADD hello world example from Examples 2. Add Server Reading Example www.researchdesignlab.com Page 90 Integrating PHP with Embedded System 3. relay interface(Web Remote) code: #include "etherShield.h" #include "ETHER_28J60.h" int outputPin1 = 2; int outputPin2 = 3; int outputPin3 = 4; int outputPin4 = 5; static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24}; // this just needs to be unique for your network, // so unless you have more than one of these boards // connected, you should be fine with this value. static uint8_t ip[4] = {192, 168, 1, 15}; // the IP address for your board. Check your home hub // to find an IP address not in use and pick that // this or 10.0.0.15 are likely formats for an address // that will work. static uint16_t port = 80; // Use port 80 - the standard for HTTP ETHER_28J60 e; char flag1=0,flag2=0,flag3=0,flag4=0; void setup() { www.researchdesignlab.com Page 91 Integrating PHP with Embedded System e.setup(mac, ip, port); pinMode(outputPin1, OUTPUT); pinMode(outputPin2, OUTPUT); pinMode(outputPin3, OUTPUT); pinMode(outputPin4, OUTPUT); } void loop() { char* params; if (params = e.serviceRequest()) { e.print("
Web Remote
"); e.print("RDL"); // dispaly(); if (strcmp(params, "?cmd1=on") == 0) { digitalWrite(outputPin1, HIGH); flag1=1; dispaly(); } else if (strcmp(params, "?cmd1=off") == 0) // Modified -- 2011 12 15 # Ben Schueler { digitalWrite(outputPin1, LOW); flag1=0; dispaly(); } if (strcmp(params, "?cmd2=on") == 0) { digitalWrite(outputPin2, HIGH); flag2=1; dispaly(); } else if (strcmp(params, "?cmd2=off") == 0) // Modified -- 2011 12 15 # Ben Schueler { digitalWrite(outputPin2, LOW); flag2=0; dispaly(); } if (strcmp(params, "?cmd3=on") == 0) { digitalWrite(outputPin3, HIGH); flag3=1; dispaly(); } else if (strcmp(params, "?cmd3=off") == 0) // Modified -- 2011 12 15 # Ben Schueler www.researchdesignlab.com Page 92 Integrating PHP with Embedded System { digitalWrite(outputPin3, LOW); flag3=0; dispaly(); } if (strcmp(params, "?cmd4=on") == 0) { digitalWrite(outputPin4, HIGH); flag4=1; dispaly(); } else if (strcmp(params, "?cmd4=off") == 0) // Modified -- 2011 12 15 # Ben Schueler { digitalWrite(outputPin4, LOW); flag4=0; dispaly(); } e.respond(); } } void dispaly() { if(flag1==0) e.print("RELAY1 ON"); else e.print("RELAY1 OFF"); if(flag2==0) e.print("'>RELAY2 ON"); else e.print("RELAY2 OFF"); if(flag3==0) e.print("'>RELAY3 ON"); else e.print("RELAY3 OFF"); if(flag4==0) e.print("'>RELAY4 ON"); else e.print("RELAY4 OFF"); } www.researchdesignlab.com Page 93 Integrating PHP with Embedded System www.researchdesignlab.com Page 94 Integrating PHP with Embedded System 4. web integration PIN CONNECTION WITH ETHERNET MODULE www.researchdesignlab.com Page 95 Integrating PHP with Embedded System WITH RELAY www.researchdesignlab.com Page 96 Integrating PHP with Embedded System Connection Diagrams www.researchdesignlab.com Page 97 Integrating PHP with Embedded System CODE #includeRESEARCH DESIGN LAB,
\
\
\ "; const char *indexPageBODY = "
|
| \
| |||||||||||
This is HTTP request #
\ "; //************************************************************************* /*********************************** * RAM variables */ unsigned char myMacAddr[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24}; // my MAC address unsigned char myIpAddr[4] = {192, 168, 1, 15}; // my IP address unsigned char getRequest[15]; // HTTP request buffer unsigned char get_Request, digit_getRequest, etat_interrupteur; www.researchdesignlab.com Page 109 Integrating PHP with Embedded System unsigned char dyna[30]; unsigned long httpCounter = 0; // buffer for dynamic response // counter of HTTP requests /******************************************* * functions */ /* * put the constant string pointed to by s to the ENC transmit buffer. */ /*unsigned int putConstString(const char *s) { unsigned int ctr = 0 ; while(*s) { Spi_Ethernet_putByte(*s++) ; ctr++ ; } return(ctr) ; }*/ /* * it will be much faster to use library Spi_Ethernet_putConstString routine * instead of putConstString routine above. However, the code will be a little * bit bigger. User should choose between size and speed and pick the implementation that * suites him best. If you choose to go with the putConstString definition above * the #define line below should be commented out. * */ #define putConstString SPI_Ethernet_putConstString /* * put the string pointed to by s to the ENC transmit buffer */ /*unsigned int putString(char *s) { unsigned int ctr = 0 ; while(*s) { Spi_Ethernet_putByte(*s++) ; ctr++ ; } return(ctr) ; }*/ /* * it will be much faster to use library Spi_Ethernet_putString routine * instead of putString routine above. However, the code will be a little * bit bigger. User should choose between size and speed and pick the implementation that * suites him best. If you choose to go with the putString definition above www.researchdesignlab.com Page 110 Integrating PHP with Embedded System * the #define line below should be commented out. * */ #define putString SPI_Ethernet_putString /* * this function is called by the library * the user accesses to the HTTP request by successive calls to Spi_Ethernet_getByte() * the user puts data in the transmit buffer by successive calls to Spi_Ethernet_putByte() * the function must return the length in bytes of the HTTP reply, or 0 if nothing to transmit * * if you don't need to reply to HTTP requests, * just define this function with a return(0) as single statement * */ unsigned int SPI_Ethernet_UserTCP(unsigned char *remoteHost, unsigned int remotePort, unsigned int localPort, unsigned int reqLength, TEthPktFlags *flags) { unsigned int len = 0 ; // my reply length unsigned int i ; // general purpose integer // should we close tcp socket after response is sent? // library closes tcp socket by default if canClose flag is not reset here // flags->canClose = 0; // 0 - do not close socket // otherwise - close socket if(localPort != 80) { // I listen only to web request on port 80 return(0) ; } // get 10 first bytes only of the request, the rest does not matter here for(i = 0 ; i < 10 ; i++) { getRequest[i] = SPI_Ethernet_getByte() ; } getRequest[i] = 0 ; if(memcmp(getRequest, httpMethod, 5)) { // only GET method is supported here return(0) ; } httpCounter++ ; // one more request done get_Request = getRequest[5]; // s , d if(get_Request == 's') // utiliser pour { // if request path name starts with s, store dynamic data in transmit buffer www.researchdesignlab.com Page 111 Integrating PHP with Embedded System // the text string replied by this request can be interpreted as javascript statements // by browsers len = putConstString(httpHeader); // HTTP header len += putConstString(httpMimeTypeScript); // with text MIME type // add AN2 value to reply IntToStr(ADC_Read(2), dyna); len += putConstString("var AN2="); len += putString(dyna); len += putConstString(";"); // add AN3 value to reply IntToStr(ADC_Read(3), dyna); len += putConstString("var AN3="); len += putString(dyna); len += putConstString(";"); // add PORTB value (buttons) to reply len += putConstString("var PORTB="); IntToStr(PORTB, dyna); len += putString(dyna); len += putConstString(";"); // add PORTD value (LEDs) to reply len += putConstString("var PORTD="); IntToStr(PORTD, dyna); len += putString(dyna); len += putConstString(";"); // add HTTP requests counter to reply IntToStr(httpCounter, dyna); len += putConstString("var REQ="); len += putString(dyna); len += putConstString(";"); } else { // if(get_Request == 'd') // Commande PORTD { if( isdigit(getRequest[6]) ) { digit_getRequest = getRequest[6] - '0'; // numéro de port 0 à 7 if( getRequest[7] == 'o' ) // Contact Ouvert (OFF) etat_interrupteur = 0; if( getRequest[7] == 'f' ) // Contact Fermer (ON) etat_interrupteur = 1; switch(digit_getRequest) www.researchdesignlab.com Page 112 Integrating PHP with Embedded System { case 0: PORTD.B0 = etat_interrupteur; break; case 1: PORTD.B1 = etat_interrupteur; break; case 2: PORTD.B2 = etat_interrupteur; break; case 3: PORTD.B3 = etat_interrupteur; break; case 4: PORTD.B4 = etat_interrupteur; break; case 5: PORTD.B5 = etat_interrupteur; break; case 6: PORTD.B6 = etat_interrupteur; break; case 7: PORTD.B7 = etat_interrupteur; break; } } } // } if(len == 0) { // what do to by default len = putConstString(httpHeader); // HTTP header len += putConstString(httpMimeTypeHTML); // with HTML MIME type len += putConstString(indexPageHEAD); // HTML page first part len += putConstString(indexPageBODY); // HTML page second part len += putConstString(indexPageBODY2); // HTML page second part } return(len) ; // return to the library with the number of bytes to transmit } /* * this function is called by the library * the user accesses to the UDP request by successive calls to Spi_Ethernet_getByte() * the user puts data in the transmit buffer by successive calls to Spi_Ethernet_putByte() * the function must return the length in bytes of the UDP reply, or 0 if nothing to transmit * * if you don't need to reply to UDP requests, * just define this function with a return(0) as single statement * */ unsigned int SPI_Ethernet_UserUDP(unsigned char *remoteHost, unsigned int remotePort, unsigned int destPort, unsigned int reqLength, TEthPktFlags *flags) { return 0; // back to the library with the length of the UDP reply } /* * main entry */ void main() { //ANSEL = 0x0C ; //C1ON_bit = 0; //C2ON_bit = 0; // AN2 and AN3 convertors will be used // Disable comparators www.researchdesignlab.com Page 113 Integrating PHP with Embedded System PORTA = 0 ; TRISA = 0xff ; // set PORTA as input for ADC //ANSELH = 0; PORTB = 0 ; TRISB = 0xff ; // Configure other AN pins as digital I/O // set PORTB as input for buttons PORTD = 0 ; TRISD = 0 ; // set PORTD as output /* * starts ENC28J60 with : * reset bit on RC0 * CS bit on RC1 * my MAC & IP address * full duplex */ SPI1_Init(); SPI_Ethernet_Init(myMacAddr, myIpAddr, Spi_Ethernet_FULLDUPLEX) ; while(1) { // do forever /* * if necessary, test the return value to get error code */ SPI_Ethernet_doPacket() ; // process incoming Ethernet packets /* * add your stuff here if needed * Spi_Ethernet_doPacket() must be called as often as possible * otherwise packets could be lost */ } } www.researchdesignlab.com Page 114 Integrating PHP with Embedded System www.researchdesignlab.com Page 115
Source Exif Data:
File Type : PDF File Type Extension : pdf MIME Type : application/pdf PDF Version : 1.5 Linearized : No Page Count : 115 Language : en-US Tagged PDF : Yes Title : PHP Lab Manual Author : Lavita Creator : Microsoft® Word 2010 Create Date : 2015:08:29 10:16:54+05:30 Modify Date : 2015:08:29 10:16:54+05:30 Producer : Microsoft® Word 2010EXIF Metadata provided by EXIF.tools