Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with php By: Seth Larson. A little bit about PHP  PHP stands for PHP:  Hypertext Preprocessor  PHP is a widely-used general-purpose server-side.

Similar presentations


Presentation on theme: "Programming with php By: Seth Larson. A little bit about PHP  PHP stands for PHP:  Hypertext Preprocessor  PHP is a widely-used general-purpose server-side."— Presentation transcript:

1 Programming with php By: Seth Larson

2 A little bit about PHP  PHP stands for PHP:  Hypertext Preprocessor  PHP is a widely-used general-purpose server-side scripting language that is suited for Web development and can be embedded into HTML.  PHP scripts are executed on the server  PHP supports many different databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)  PHP is an open source software (OSS), so its free  PHP files have a file extension of ".php", ".php3", or ".phtml"  PHP files are returned to the browser as plain HTML

3 Server Side o o PHP rules! ';?> o

4 Client Side   PHP rules! 

5 What do you need installed on your computer to use php?  A server that supports php. Some servers that support php are Apache(open source), IIS (Microsoft), and Oracle.  The latest version of PHP found at  http://www.php.net  You can test if php is installed and working with the following code.

6 Show php information   <?php  phpinfo();  ?> 

7 Some PHP syntax  A php statement always begins with.  php statements can be placed anywhere in the document.  Statements end with semicolon.

8 PHP variables  Variables start with $, example $var  Variables have the same syntax rules as other high level languages.  To concatenate two or more variables together, use the dot (.) operator:  Using the “\” right next to a variable name tells the server to print what directly follows and not to treat it as a label.

9 Sample Variables   <?php  $var_A = " Print \$var_A ";  $var_B = "To print a variable name instead  of what the variable is holding use the \  character.";  echo $var_B;  echo" ";  echo $var_A;  ?> 

10 PHP operators  Arithmetic Operators  +, -, /, *  Assignment Operators  =, +=, -=, *=, /=, %=  Comparison Operators  ==, !=, >, =, <=  Logical Operators  &&, ||, !  If – else statement  if (condition)  code to be executed  else  code to be executed  While  while { code to be executed; }  Do-while  do { code to be executed; } while (condition);  For  Foreach

11 PHP forms  Form is created with html.  Any html element you put in a form is available after a submit because php automatically makes element variables.  Examples: $_POST["first"],$_POST["last"  Notice that if you use post in the form header the variables are accessible with $_POST, if you use get the variables are accessible with $_GET.

12 Form   Your Id:   Your First name:   Your Last name:  <?php  echo "male: <input type = checkbox name = sex value  male>  Female: <input type = checkbox name= sex  value=female>  ?> 

13 Print form variables   You said your Id number was:   You said your First name was <?php echo  $_POST["first"]; ?>  You said you Last name was <?php echo  $_POST["last"]; ?>  You also said you are <?php echo  $_POST["sex"];?>  Your favorite color is <?php echo  $_POST["color"];?> 

14 Text File Read/Write   <?php  if (!($file=fopen("php_file.txt","r")))  exit("Can’t open file.");  while (!feof($file))  { $char=fgetc($file);  echo $char;  }  //$write = fputs($fp, “write hi”);  fclose($file);  ?> 

15 Using a database  The mysql_connect() function opens a link to a MySQL server on the specified host along with a username (root) and a password.  mysql_select_db() is used to tell my MySQL to use the specified database.  mysql_query()sends a line of SQL to the MySQL database identified in the connection for processing. The returned results are stored in $result.  mysql_result() is used to display the values of fields from our query.  %s indicates that the variable in the second half of the expression (e.g., mysql_result($result,0,"position")) should be treated as a string and printed.

16 Reading one field from each record in the database   <?php  $db = mysql_connect("localhost", "root","seth");  mysql_select_db("mydb",$db);  $result = mysql_query("SELECT * FROM employee",$db);  $count=mysql_numrows($result);  $i=0;  while ($i < $count)  {  printf("First Name: %s",  mysql_result($result,$i,"first"));  echo " ";  $i++;  }  mysql_close();  ?> 

17 Reading records one at a time   <?php  $db = mysql_connect("localhost", "root", "seth");  mysql_select_db("mydb",$db);  $result = mysql_query("SELECT * FROM employee",$db);  echo " \n";  echo " id First Name and Last Name \n";  while ($myrow = mysql_fetch_row($result))  {  printf(" %s %s %s \n",  $myrow[0], $myrow[1], $myrow[2]);  }  echo " \n";  ?> 

18 Inserting into a Database: form part   Your Id:  Your First name:  Your Last name: 

19 Insert into a MySQL database   <?php  $id = $_POST["id"];  $first = $_POST["first"];  $last = $_POST["last"];  $db = mysql_connect("localhost", "root","seth");  mysql_select_db("mydb",$db);  $result = mysql_query("SELECT * FROM employee",$db);  echo " \n";  echo " id First Name and Last Name \n";  while ($myrow = mysql_fetch_row($result))  {  printf(" %s %s %s \n",  $myrow[0], $myrow[1], $myrow[2]);  }  echo " \n";  $query = "INSERT INTO employee VALUES ('$id','$first','$last')";  mysql_query($query);  ?> 


Download ppt "Programming with php By: Seth Larson. A little bit about PHP  PHP stands for PHP:  Hypertext Preprocessor  PHP is a widely-used general-purpose server-side."

Similar presentations


Ads by Google