<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://classes.engineering.wustl.edu/cse330/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Juliad</id>
	<title>CSE330 Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="http://classes.engineering.wustl.edu/cse330/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Juliad"/>
	<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Special:Contributions/Juliad"/>
	<updated>2026-11-15T21:45:36Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.34.4</generator>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=265</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=265"/>
		<updated>2010-10-20T17:21:24Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* jQuery */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
Javascript is one of the most widely used scripting languages for preforming client-side functions in the web environment.  That is, the javascript code is interpreted and executed in a client's web browser.. not on the web server.  It is vaguely similar to Java, in that it is an object oriented language, but it was designed to be simpler for non-programmers to be able to accomplish common web processing tasks.&lt;br /&gt;
&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
Since the javascript code runs in the web browser, you'll have to use web browser tools for all debugging.  Generally speaking, if you have syntax or runtime errors in your javascript, the web browser will report nothing but not attempt to run the script.  So if it appears that your code is doing nothing, then it's likely a syntax error of some sort.  Fortunately, in Firefox, you can use the error console to view warnings and errors produced as javascripts are executed.  Go to Tools-&amp;gt;Error Console to access it.  This is most useful for simple syntatic debugging.  If you need a more serious debugger, you can use [http://getfirebug.com/ Firebug] for Firefox.&lt;br /&gt;
&lt;br /&gt;
=Asynchronous Javascript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that a large fraction of the work happens at the web browser, and larger requests happen behind the scenes, keeping waiting times for data to come back over the network to a minimum.  AJAX is generally also associated with higher-level APIs and libraries that aid in producing cleaner user interfaces for web applications.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   if(xmlHttp.readyState==4)&lt;br /&gt;
   {&lt;br /&gt;
     .... do some cool stuff ...&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are sent either in the url (to, for example, a PHP page) or through the ''send'' function.&lt;br /&gt;
Consider following PHP code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply ()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the x and y values are sent as part of the url as GET parameters. You can also send them as part of the ''send'' function, provided that your receiving page uses POST parameters.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
multiply-ajax-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply-post.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=jQuery=&lt;br /&gt;
&lt;br /&gt;
jQuery is a fairly lightweight javascript library that has a number of particularly useful features.  Even thought it's only been around for a few years now, it has gained a lot of momentum in the web development community.  jQuery provides a simple interface to easily modify CSS attributes on all the elements in the HTML document, has simplified AJAX integration, and is accompanied by a number of handy user interface related APIs.  You can get a good overview of jQuery in the jQuery [http://docs.jquery.com/Tutorials:How_jQuery_Works Tutorials], and there is a short summary on [http://en.wikipedia.org/wiki/JQuery Wikipedia].  Of course, there are plenty of other tutorials a google search away.&lt;br /&gt;
&lt;br /&gt;
To use jQuery in your javascript, you need to include the jQuery javascript library, which is just another javascript file.  It's freely available online at the jQuery [http://docs.jquery.com/Downloading_jQuery website].  Assuming you've downloaded the file to your web server, put it in the directory with your PHP and HTML files, and renamed it ''jquery.js'', you include it by adding this line to the head section of your HTML of PHP file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;jquery.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also link to the jQuery library directly on other websites as mentioned in the above tutorials.  The trade-off is that having a local copy will load faster, but then requires you to manage the library files your self (e.g., downloading new versions).&lt;br /&gt;
&lt;br /&gt;
A few code snippets are given below, but the best way to learn about all the features of jQuery is to head to the [http://jquery.com jQuery website] and look through the tutorials, references, and examples there.&lt;br /&gt;
&lt;br /&gt;
The ''$'' symbol is used to denote jQuery functionality.  You can use it to call jQuery functions, access HTML document elements in order to read or write values, or modify style information via CSS for a range of document elements.  For example, here is some code that sets up a function to be called whenever the user clicks on a link:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $(&amp;quot;a&amp;quot;).click( function(event)&lt;br /&gt;
 {&lt;br /&gt;
   event.preventDefault();&lt;br /&gt;
   $(this).hide(&amp;quot;slow&amp;quot;);&lt;br /&gt;
 } );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code leverages many useful javascript and jQuery features.  ''$(&amp;quot;a&amp;quot;)'' is the jQuery way of selecting all HTML ''a'' elements.  That is, in this case, all elements in the HTML document that are ''&amp;lt;a&amp;gt;'' tags.  The ''click'' function is invoked for all such ''a'' elements in the document.  The argument to the ''click'' function is another function that is to be called when the selected elements are clicked by the user.  The ''event'' argument to this user-defined function is a default argument sent to functions which handle user actions like clicking a mouse button.  In this example, the code calls another function, ''preventDefault()'' on the event passed to stop the standard action on that event.  Here that means stopping the web browser from following the link that the user clicked on.  Finally, the ''$(this)'' variable is the jQuery way of accessing the current encapsulating object, as in Java.  This example calls the jQuery hide function on the object to make the object (again, in this case, ''this'' is referring to the ''&amp;lt;a&amp;gt;'' tag that the user clicked) disappear from the browser.  The script also passes an argument to the hide function to make the link disappear slowly as described in the API [http://docs.jquery.com/Hide documentation].&lt;br /&gt;
&lt;br /&gt;
jQuery also comes with some easy to use widgets for building better user interfaces.  One of the handiest is the [http://docs.jquery.com/UI/Dialog dialog] widget that pops-up an in-browser dialog window.  To use the dialog functions, you need to include a few more javascript libraries in your HTML header block like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;link href=&amp;quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/start/jquery-ui.css&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
type=&amp;quot;text/css&amp;quot; rel=&amp;quot;Stylesheet&amp;quot; /&amp;gt; &amp;lt;!-- We need the style sheet linked&lt;br /&gt;
 above or the dialogs/other parts of jquery-ui won't display correctly!--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;!-- The main library. &lt;br /&gt;
Note: must be listed before the jquery-ui library --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;!-- jquery-UI &lt;br /&gt;
hosted on Google's Ajax CDN--&amp;gt;&lt;br /&gt;
&amp;lt;!-- Note: you can download the javascript file from the link&lt;br /&gt;
&lt;br /&gt;
provided on the google doc, or simply provide its URL&lt;br /&gt;
&lt;br /&gt;
in the src attribute (microsoft and google also host the jQuery&lt;br /&gt;
&lt;br /&gt;
library--&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
   // your code goes here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can then use the ''$().dialog()'' function to make dialog boxes.  The ''dialog'' function can take many arguments to customize its behavior and add standard elements like buttons; see the documentation for details.  The content of the dialog box is typically placed in a ''div'' container and then that container is hidden when the document loads.  You can put any elements in the ''div'' and they will be displayed as normal in the dialog box.  Here is a full example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&lt;br /&gt;
 &amp;lt;head&amp;gt;&lt;br /&gt;
  &amp;lt;style&amp;gt;&lt;br /&gt;
    #mydialog { display:none }&lt;br /&gt;
  &amp;lt;/style&amp;gt;&lt;br /&gt;
  &amp;lt;link href=&amp;quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/start/jquery-ui.css&amp;quot;&lt;br /&gt;
   type=&amp;quot;text/css&amp;quot; rel=&amp;quot;Stylesheet&amp;quot; /&amp;gt; &lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
    function showdialog()&lt;br /&gt;
    {&lt;br /&gt;
      $(&amp;quot;#mydialog&amp;quot;).dialog();&lt;br /&gt;
    }&lt;br /&gt;
  &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  &amp;lt;input type=&amp;quot;button&amp;quot; value=&amp;quot;Show Dialog&amp;quot; onclick=showdialog() /&amp;gt;&lt;br /&gt;
  &amp;lt;div id=&amp;quot;mydialog&amp;quot; title=&amp;quot;Howdy&amp;quot;&amp;gt;Look at me!&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To understand the page, start at the bottom.  There is a ''div'' element with a title and some string content.  It also has a unique id (the ids can be anything you want) which is used to identify this particular element in the rest of the document.  This div's id is ''mydialog''.  Above that is a button input.  The only thing special about it is the ''onclick'' attribute which specifies a javascript function to call when the button is clicked by a user.  In this case, the function is ''showdialog()'' which is a user defined javascript function contained in the document header.  All the function does is access the document element with id ''mydialog'' (the ''#identifier'' is the jQuery way of accessing an individual element based on the assigned id in the element attribute list), and build a dialog box from it.  Here, the dialog box will have the title ''Howdy'' from the title attribute in the ''mydialog'' element, and have a text string ''Look at me!''.  Finally, at the top of the document is the ''style'' section, which is a normal HTML/CSS section describing appearance of parts of the document.  There is only one entry, again describing the document element with id ''mydialog''.  One style attribute is set, namely the ''display'' attribute is set to ''none'' to indicate that the element should not be displayed.  Otherwise, the ''mydialog'' div would be displayed like any other part of the document rather than being used solely for our dialog box.&lt;br /&gt;
&lt;br /&gt;
Here is another example of using jQuery to do simple animations by moving an image across the browser window:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
  // these are for storing target locations&lt;br /&gt;
  var targetLeft=0;&lt;br /&gt;
  var targetTop=0;&lt;br /&gt;
  function moveTarget()&lt;br /&gt;
  {&lt;br /&gt;
    targetLeft+=10; //increase target location&lt;br /&gt;
    targetTop+=10; &lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
    if(targetLeft&amp;amp;lt;200) &lt;br /&gt;
    { //stop if we reached left=200&lt;br /&gt;
      window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready( function() &lt;br /&gt;
  { //at the beginning create a clock timeout&lt;br /&gt;
    window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
  });&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
&amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Other Resources=&lt;br /&gt;
There are a number of other w3schools tutorials and references that you may find useful, including [http://www.w3schools.com/php/default.asp PHP] (note the PHP and AJAX section), [http://www.w3schools.com/htmldom/default.asp DOM], [http://www.w3schools.com/ajax/default.asp AJAX], and [http://www.w3schools.com/css/default.asp CSS].  Many other resources exist around the web, and you should feel free to incorporate any existing libraries or other javascript or AJAX-related tools.  For example, the [http://code.google.com/apis/ajaxlibs/ Google AJAX APIs].&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=264</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=264"/>
		<updated>2010-10-20T17:20:06Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* jQuery */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
Javascript is one of the most widely used scripting languages for preforming client-side functions in the web environment.  That is, the javascript code is interpreted and executed in a client's web browser.. not on the web server.  It is vaguely similar to Java, in that it is an object oriented language, but it was designed to be simpler for non-programmers to be able to accomplish common web processing tasks.&lt;br /&gt;
&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
Since the javascript code runs in the web browser, you'll have to use web browser tools for all debugging.  Generally speaking, if you have syntax or runtime errors in your javascript, the web browser will report nothing but not attempt to run the script.  So if it appears that your code is doing nothing, then it's likely a syntax error of some sort.  Fortunately, in Firefox, you can use the error console to view warnings and errors produced as javascripts are executed.  Go to Tools-&amp;gt;Error Console to access it.  This is most useful for simple syntatic debugging.  If you need a more serious debugger, you can use [http://getfirebug.com/ Firebug] for Firefox.&lt;br /&gt;
&lt;br /&gt;
=Asynchronous Javascript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that a large fraction of the work happens at the web browser, and larger requests happen behind the scenes, keeping waiting times for data to come back over the network to a minimum.  AJAX is generally also associated with higher-level APIs and libraries that aid in producing cleaner user interfaces for web applications.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   if(xmlHttp.readyState==4)&lt;br /&gt;
   {&lt;br /&gt;
     .... do some cool stuff ...&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are sent either in the url (to, for example, a PHP page) or through the ''send'' function.&lt;br /&gt;
Consider following PHP code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply ()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the x and y values are sent as part of the url as GET parameters. You can also send them as part of the ''send'' function, provided that your receiving page uses POST parameters.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
multiply-ajax-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply-post.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=jQuery=&lt;br /&gt;
&lt;br /&gt;
jQuery is a fairly lightweight javascript library that has a number of particularly useful features.  Even thought it's only been around for a few years now, it has gained a lot of momentum in the web development community.  jQuery provides a simple interface to easily modify CSS attributes on all the elements in the HTML document, has simplified AJAX integration, and is accompanied by a number of handy user interface related APIs.  You can get a good overview of jQuery in the jQuery [http://docs.jquery.com/Tutorials:How_jQuery_Works Tutorials], and there is a short summary on [http://en.wikipedia.org/wiki/JQuery Wikipedia].  Of course, there are plenty of other tutorials a google search away.&lt;br /&gt;
&lt;br /&gt;
To use jQuery in your javascript, you need to include the jQuery javascript library, which is just another javascript file.  It's freely available online at the jQuery [http://docs.jquery.com/Downloading_jQuery website].  Assuming you've downloaded the file to your web server, put it in the directory with your PHP and HTML files, and renamed it ''jquery.js'', you include it by adding this line to the head section of your HTML of PHP file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;jquery.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also link to the jQuery library directly on other websites as mentioned in the above tutorials.  The trade-off is that having a local copy will load faster, but then requires you to manage the library files your self (e.g., downloading new versions).&lt;br /&gt;
&lt;br /&gt;
A few code snippets are given below, but the best way to learn about all the features of jQuery is to head to the [http://jquery.com jQuery website] and look through the tutorials, references, and examples there.&lt;br /&gt;
&lt;br /&gt;
The ''$'' symbol is used to denote jQuery functionality.  You can use it to call jQuery functions, access HTML document elements in order to read or write values, or modify style information via CSS for a range of document elements.  For example, here is some code that sets up a function to be called whenever the user clicks on a link:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $(&amp;quot;a&amp;quot;).click( function(event)&lt;br /&gt;
 {&lt;br /&gt;
   event.preventDefault();&lt;br /&gt;
   $(this).hide(&amp;quot;slow&amp;quot;);&lt;br /&gt;
 } );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code leverages many useful javascript and jQuery features.  ''$(&amp;quot;a&amp;quot;)'' is the jQuery way of selecting all HTML ''a'' elements.  That is, in this case, all elements in the HTML document that are ''&amp;lt;a&amp;gt;'' tags.  The ''click'' function is invoked for all such ''a'' elements in the document.  The argument to the ''click'' function is another function that is to be called when the selected elements are clicked by the user.  The ''event'' argument to this user-defined function is a default argument sent to functions which handle user actions like clicking a mouse button.  In this example, the code calls another function, ''preventDefault()'' on the event passed to stop the standard action on that event.  Here that means stopping the web browser from following the link that the user clicked on.  Finally, the ''$(this)'' variable is the jQuery way of accessing the current encapsulating object, as in Java.  This example calls the jQuery hide function on the object to make the object (again, in this case, ''this'' is referring to the ''&amp;lt;a&amp;gt;'' tag that the user clicked) disappear from the browser.  The script also passes an argument to the hide function to make the link disappear slowly as described in the API [http://docs.jquery.com/Hide documentation].&lt;br /&gt;
&lt;br /&gt;
jQuery also comes with some easy to use widgets for building better user interfaces.  One of the handiest is the [http://docs.jquery.com/UI/Dialog dialog] widget that pops-up an in-browser dialog window.  To use the dialog functions, you need to include a few more javascript libraries in your HTML header block like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;link href=&amp;quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/start/jquery-ui.css&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
type=&amp;quot;text/css&amp;quot; rel=&amp;quot;Stylesheet&amp;quot; /&amp;gt; &amp;lt;!-- We need the style sheet linked&lt;br /&gt;
 above or the dialogs/other parts of jquery-ui won't display correctly!--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;!-- The main library. Note: must be listed before the jquery-ui library --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;!-- jquery-UI hosted on Google's Ajax CDN--&amp;gt;&lt;br /&gt;
&amp;lt;!-- Note: you can download the javascript file from the link&lt;br /&gt;
&lt;br /&gt;
provided on the google doc, or simply provide its URL&lt;br /&gt;
&lt;br /&gt;
in the src attribute (microsoft and google also host the jQuery&lt;br /&gt;
&lt;br /&gt;
library--&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
   // your code goes here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can then use the ''$().dialog()'' function to make dialog boxes.  The ''dialog'' function can take many arguments to customize its behavior and add standard elements like buttons; see the documentation for details.  The content of the dialog box is typically placed in a ''div'' container and then that container is hidden when the document loads.  You can put any elements in the ''div'' and they will be displayed as normal in the dialog box.  Here is a full example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&lt;br /&gt;
 &amp;lt;head&amp;gt;&lt;br /&gt;
  &amp;lt;style&amp;gt;&lt;br /&gt;
    #mydialog { display:none }&lt;br /&gt;
  &amp;lt;/style&amp;gt;&lt;br /&gt;
  &amp;lt;link href=&amp;quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/start/jquery-ui.css&amp;quot;&lt;br /&gt;
   type=&amp;quot;text/css&amp;quot; rel=&amp;quot;Stylesheet&amp;quot; /&amp;gt; &lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
    function showdialog()&lt;br /&gt;
    {&lt;br /&gt;
      $(&amp;quot;#mydialog&amp;quot;).dialog();&lt;br /&gt;
    }&lt;br /&gt;
  &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  &amp;lt;input type=&amp;quot;button&amp;quot; value=&amp;quot;Show Dialog&amp;quot; onclick=showdialog() /&amp;gt;&lt;br /&gt;
  &amp;lt;div id=&amp;quot;mydialog&amp;quot; title=&amp;quot;Howdy&amp;quot;&amp;gt;Look at me!&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To understand the page, start at the bottom.  There is a ''div'' element with a title and some string content.  It also has a unique id (the ids can be anything you want) which is used to identify this particular element in the rest of the document.  This div's id is ''mydialog''.  Above that is a button input.  The only thing special about it is the ''onclick'' attribute which specifies a javascript function to call when the button is clicked by a user.  In this case, the function is ''showdialog()'' which is a user defined javascript function contained in the document header.  All the function does is access the document element with id ''mydialog'' (the ''#identifier'' is the jQuery way of accessing an individual element based on the assigned id in the element attribute list), and build a dialog box from it.  Here, the dialog box will have the title ''Howdy'' from the title attribute in the ''mydialog'' element, and have a text string ''Look at me!''.  Finally, at the top of the document is the ''style'' section, which is a normal HTML/CSS section describing appearance of parts of the document.  There is only one entry, again describing the document element with id ''mydialog''.  One style attribute is set, namely the ''display'' attribute is set to ''none'' to indicate that the element should not be displayed.  Otherwise, the ''mydialog'' div would be displayed like any other part of the document rather than being used solely for our dialog box.&lt;br /&gt;
&lt;br /&gt;
Here is another example of using jQuery to do simple animations by moving an image across the browser window:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
  // these are for storing target locations&lt;br /&gt;
  var targetLeft=0;&lt;br /&gt;
  var targetTop=0;&lt;br /&gt;
  function moveTarget()&lt;br /&gt;
  {&lt;br /&gt;
    targetLeft+=10; //increase target location&lt;br /&gt;
    targetTop+=10; &lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
    if(targetLeft&amp;amp;lt;200) &lt;br /&gt;
    { //stop if we reached left=200&lt;br /&gt;
      window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready( function() &lt;br /&gt;
  { //at the beginning create a clock timeout&lt;br /&gt;
    window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
  });&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
&amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Other Resources=&lt;br /&gt;
There are a number of other w3schools tutorials and references that you may find useful, including [http://www.w3schools.com/php/default.asp PHP] (note the PHP and AJAX section), [http://www.w3schools.com/htmldom/default.asp DOM], [http://www.w3schools.com/ajax/default.asp AJAX], and [http://www.w3schools.com/css/default.asp CSS].  Many other resources exist around the web, and you should feel free to incorporate any existing libraries or other javascript or AJAX-related tools.  For example, the [http://code.google.com/apis/ajaxlibs/ Google AJAX APIs].&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=262</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=262"/>
		<updated>2010-10-04T20:27:24Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Foreign Keys */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig --add mysqld&lt;br /&gt;
 sudo /sbin/chkconfig mysqld on&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is the following: &lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to USERNAME@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER USER_NAME@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to boss@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO david@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO TODD@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
&lt;br /&gt;
===Granting the Ability to Grant: With Grant Option===&lt;br /&gt;
Additionally, in order to allow the user to grant other users privileges you must add ''WITH GRANT OPTION'' to your grant statement. For example:&lt;br /&gt;
 GRANT SELECT on powerplant.reactions, TO homer@'powerplant.springfield.gov' WITH GRANT OPTION;&lt;br /&gt;
The above command not only allows user ''homer@powerplant.springfield.gov'' to ''SELECT'' from the powerplant database's reactions table, but also allows him to grant other users the ability to select on those databases.&lt;br /&gt;
&lt;br /&gt;
i.e. Homer can issue the following command:&lt;br /&gt;
 GRANT SELECT ON powerplant.reactions TO moe@'moestavern';&lt;br /&gt;
&lt;br /&gt;
===A Note on Privileges===&lt;br /&gt;
Privileges in MySQL are highly flexible in terms of specificity, that is to say that you can grant a user specific privileges such as SELECT on a database, table, or column level. Note that only the SELECT, INSERT, and UPDATE privileges are available at the column level, if this seems confusing to you, take a moment to think about why this might be. For instance, why shouldn't there exist a column-level DROP privilege?&lt;br /&gt;
&lt;br /&gt;
The flexibility allows a database administrator (you) to give other users access only to the minimal level of privileges that they require, in accordance with the [http://en.wikipedia.org/wiki/Principle_of_least_privilege principal of least privilege].&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it. For this Command to work, you must issue the following command to grant the FILE privilege to your user&lt;br /&gt;
 GRANT FILE on *.* to 'user'@'host';&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword. When loading data from the local machine you do not need to grant your user the FILE privilege, think about why this might be the case.&lt;br /&gt;
Hint: what is going on behind the scenes when you load the data in from a local machine when your database is hosted on a different machine? &lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.w3schools.com/sql/sql_join.asp here] for a good explanation of how to use joins (see the next 4 chapters for explanations of inner, left, right and full joins).&lt;br /&gt;
&lt;br /&gt;
====Aggregate Functions====&lt;br /&gt;
The following is a brief overview of aggregate functions, however it is highly recommended that you go over [http://www.learn-mysql-tutorial.com/Aggregation.cfm this tutorial].&lt;br /&gt;
Aggregate functions are functions which take multiple data entries in one column or group and return a single value.&lt;br /&gt;
MySQL's aggregate functions include AVG (average), STD (standard deviation), SUM, MAX, MIN, and COUNT.&lt;br /&gt;
Aggregate functions are often, if not necessarily, used in conjunction with the GROUP BY clause which uses a field in the table to define the groups (sets) on which to apply the aggregate function.&lt;br /&gt;
The HAVING clause is used to filter the aggregate data and is similar in function to the where clause.&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys] for a nice definition of what a foreign key is.&lt;br /&gt;
&lt;br /&gt;
[http://articles.techrepublic.com.com/5100-10878_11-6035435.html Here] is a brief intro to foreign keys in MySQL, which is worth a read if you have never used foreign keys before.&lt;br /&gt;
&lt;br /&gt;
[http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html This] is the MySQL reference manual page on foreign keys. This is a more in depth document which details foreign keys in MySQL's InnoDB. It is highly recommended that you look it over, paying careful attention to the section enumerating the conditions to which foreign keys are subject.&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=261</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=261"/>
		<updated>2010-10-04T20:26:59Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Foreign Keys */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig --add mysqld&lt;br /&gt;
 sudo /sbin/chkconfig mysqld on&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is the following: &lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to USERNAME@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER USER_NAME@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to boss@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO david@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO TODD@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
&lt;br /&gt;
===Granting the Ability to Grant: With Grant Option===&lt;br /&gt;
Additionally, in order to allow the user to grant other users privileges you must add ''WITH GRANT OPTION'' to your grant statement. For example:&lt;br /&gt;
 GRANT SELECT on powerplant.reactions, TO homer@'powerplant.springfield.gov' WITH GRANT OPTION;&lt;br /&gt;
The above command not only allows user ''homer@powerplant.springfield.gov'' to ''SELECT'' from the powerplant database's reactions table, but also allows him to grant other users the ability to select on those databases.&lt;br /&gt;
&lt;br /&gt;
i.e. Homer can issue the following command:&lt;br /&gt;
 GRANT SELECT ON powerplant.reactions TO moe@'moestavern';&lt;br /&gt;
&lt;br /&gt;
===A Note on Privileges===&lt;br /&gt;
Privileges in MySQL are highly flexible in terms of specificity, that is to say that you can grant a user specific privileges such as SELECT on a database, table, or column level. Note that only the SELECT, INSERT, and UPDATE privileges are available at the column level, if this seems confusing to you, take a moment to think about why this might be. For instance, why shouldn't there exist a column-level DROP privilege?&lt;br /&gt;
&lt;br /&gt;
The flexibility allows a database administrator (you) to give other users access only to the minimal level of privileges that they require, in accordance with the [http://en.wikipedia.org/wiki/Principle_of_least_privilege principal of least privilege].&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it. For this Command to work, you must issue the following command to grant the FILE privilege to your user&lt;br /&gt;
 GRANT FILE on *.* to 'user'@'host';&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword. When loading data from the local machine you do not need to grant your user the FILE privilege, think about why this might be the case.&lt;br /&gt;
Hint: what is going on behind the scenes when you load the data in from a local machine when your database is hosted on a different machine? &lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.w3schools.com/sql/sql_join.asp here] for a good explanation of how to use joins (see the next 4 chapters for explanations of inner, left, right and full joins).&lt;br /&gt;
&lt;br /&gt;
====Aggregate Functions====&lt;br /&gt;
The following is a brief overview of aggregate functions, however it is highly recommended that you go over [http://www.learn-mysql-tutorial.com/Aggregation.cfm this tutorial].&lt;br /&gt;
Aggregate functions are functions which take multiple data entries in one column or group and return a single value.&lt;br /&gt;
MySQL's aggregate functions include AVG (average), STD (standard deviation), SUM, MAX, MIN, and COUNT.&lt;br /&gt;
Aggregate functions are often, if not necessarily, used in conjunction with the GROUP BY clause which uses a field in the table to define the groups (sets) on which to apply the aggregate function.&lt;br /&gt;
The HAVING clause is used to filter the aggregate data and is similar in function to the where clause.&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys] for a nice definition of what a foreign key is.&lt;br /&gt;
&lt;br /&gt;
[Here http://articles.techrepublic.com.com/5100-10878_11-6035435.html] is a brief intro to foreign keys in MySQL, which is worth a read if you have never used foreign keys before.&lt;br /&gt;
&lt;br /&gt;
[This http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html] is the MySQL reference manual page on foreign keys. This is a more in depth document which details foreign keys in MySQL's InnoDB. It is highly recommended that you look it over, paying careful attention to the section enumerating the conditions to which foreign keys are subject.&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=259</id>
		<title>Python</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=259"/>
		<updated>2010-08-11T15:27:23Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Installing Pydev (Optional) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Python is an interpreted language with familiar syntax. It's a great language to use for parsing text files and rapid application development. Rather than the time-consuming write-compile-test-rewrite-recompile-retest-etc. cycle of languages like C and C++, Python scripts run through an interpreter. This eliminates the compile step and speeds up the development process.&lt;br /&gt;
=Getting Started=&lt;br /&gt;
==Installing Python==&lt;br /&gt;
&lt;br /&gt;
 yum install python&lt;br /&gt;
&lt;br /&gt;
should do the trick. Make sure that python is in your path with&lt;br /&gt;
&lt;br /&gt;
 which python&lt;br /&gt;
&lt;br /&gt;
and modify your path if necessary.&lt;br /&gt;
&lt;br /&gt;
===Installing Pydev (Optional)===&lt;br /&gt;
If, like me you're an IDE junkie then you may wish to install the eclipse pydev plugin (though it can be a little annoying when [[#Installing Python Modules | Installing Python Modules]].&lt;br /&gt;
&lt;br /&gt;
The Pydev installation guide is available [http://pydev.org/manual_101_install.html here].&lt;br /&gt;
&lt;br /&gt;
==Using Python==&lt;br /&gt;
&lt;br /&gt;
Python programs can be run in several ways. Two of the most common ways are through the python shell and in .py files. &lt;br /&gt;
To enter the python shell, simply type &lt;br /&gt;
&lt;br /&gt;
 python&lt;br /&gt;
&lt;br /&gt;
into your shell. Interactive mode should start up, and you can type commands to be directly executed. You can exit interactive mode with ctrl+d or with &lt;br /&gt;
&lt;br /&gt;
 quit()&lt;br /&gt;
&lt;br /&gt;
To run a .py file, simply use:&lt;br /&gt;
&lt;br /&gt;
 python &amp;lt;filename&amp;gt; &amp;lt;args&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Syntax==&lt;br /&gt;
&lt;br /&gt;
Since the online documentation for Python is quite extensive and helpful, [http://docs.python.org/tutorial/index.html the online Python tutorial] is definitely the place for any and all of your Python-related needs. Be sure to read the [http://docs.python.org/tutorial/datastructures.html data structures] page to understand how they work in Python.&lt;br /&gt;
=Installing Python Modules=&lt;br /&gt;
Most likely the module will be distributed according to the python standard described [http://docs.python.org/install/#the-new-standard-distutils here], so you can simply unpack the archive, cd into the resulting directory, and execute the following command:&lt;br /&gt;
 python setup.py install&lt;br /&gt;
Note: you may have to execute the command using sudo depending on the permissions of the directories involved.&lt;br /&gt;
&lt;br /&gt;
Though there are yum packages available for installing many python modules, it is recommended that you use the above method if you are using eclipse with pydev, otherwise eclipse does not recognize that the module is installed, and you will simply have to ignore the unresolved import error and make due without autocomplete for that module. It's not ideal, but sometimes that's how it goes.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=258</id>
		<title>Python</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=258"/>
		<updated>2010-08-11T15:26:47Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Installing Pydev (Optional) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Python is an interpreted language with familiar syntax. It's a great language to use for parsing text files and rapid application development. Rather than the time-consuming write-compile-test-rewrite-recompile-retest-etc. cycle of languages like C and C++, Python scripts run through an interpreter. This eliminates the compile step and speeds up the development process.&lt;br /&gt;
=Getting Started=&lt;br /&gt;
==Installing Python==&lt;br /&gt;
&lt;br /&gt;
 yum install python&lt;br /&gt;
&lt;br /&gt;
should do the trick. Make sure that python is in your path with&lt;br /&gt;
&lt;br /&gt;
 which python&lt;br /&gt;
&lt;br /&gt;
and modify your path if necessary.&lt;br /&gt;
&lt;br /&gt;
===Installing Pydev (Optional)===&lt;br /&gt;
If, like me you're an IDE junkie then you may wish to install the eclipse pydev plugin (though it can be a little annoying when [[#Installing Python Modules | Installing Python Modules]].&lt;br /&gt;
&lt;br /&gt;
Their installation guide is available [http://pydev.org/manual_101_install.html here].&lt;br /&gt;
&lt;br /&gt;
==Using Python==&lt;br /&gt;
&lt;br /&gt;
Python programs can be run in several ways. Two of the most common ways are through the python shell and in .py files. &lt;br /&gt;
To enter the python shell, simply type &lt;br /&gt;
&lt;br /&gt;
 python&lt;br /&gt;
&lt;br /&gt;
into your shell. Interactive mode should start up, and you can type commands to be directly executed. You can exit interactive mode with ctrl+d or with &lt;br /&gt;
&lt;br /&gt;
 quit()&lt;br /&gt;
&lt;br /&gt;
To run a .py file, simply use:&lt;br /&gt;
&lt;br /&gt;
 python &amp;lt;filename&amp;gt; &amp;lt;args&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Syntax==&lt;br /&gt;
&lt;br /&gt;
Since the online documentation for Python is quite extensive and helpful, [http://docs.python.org/tutorial/index.html the online Python tutorial] is definitely the place for any and all of your Python-related needs. Be sure to read the [http://docs.python.org/tutorial/datastructures.html data structures] page to understand how they work in Python.&lt;br /&gt;
=Installing Python Modules=&lt;br /&gt;
Most likely the module will be distributed according to the python standard described [http://docs.python.org/install/#the-new-standard-distutils here], so you can simply unpack the archive, cd into the resulting directory, and execute the following command:&lt;br /&gt;
 python setup.py install&lt;br /&gt;
Note: you may have to execute the command using sudo depending on the permissions of the directories involved.&lt;br /&gt;
&lt;br /&gt;
Though there are yum packages available for installing many python modules, it is recommended that you use the above method if you are using eclipse with pydev, otherwise eclipse does not recognize that the module is installed, and you will simply have to ignore the unresolved import error and make due without autocomplete for that module. It's not ideal, but sometimes that's how it goes.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=257</id>
		<title>Python</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=257"/>
		<updated>2010-08-11T15:26:31Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Installing Pydev (Optional) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Python is an interpreted language with familiar syntax. It's a great language to use for parsing text files and rapid application development. Rather than the time-consuming write-compile-test-rewrite-recompile-retest-etc. cycle of languages like C and C++, Python scripts run through an interpreter. This eliminates the compile step and speeds up the development process.&lt;br /&gt;
=Getting Started=&lt;br /&gt;
==Installing Python==&lt;br /&gt;
&lt;br /&gt;
 yum install python&lt;br /&gt;
&lt;br /&gt;
should do the trick. Make sure that python is in your path with&lt;br /&gt;
&lt;br /&gt;
 which python&lt;br /&gt;
&lt;br /&gt;
and modify your path if necessary.&lt;br /&gt;
&lt;br /&gt;
===Installing Pydev (Optional)===&lt;br /&gt;
If, like me you're an IDE junkie then you may wish to install the eclipse pydev plugin (though it can be a little annoying when [[#Installing Python Modules | Installing Python Modules]].&lt;br /&gt;
&lt;br /&gt;
Their installation guide is available [http://pydev.org/manual_101_install.html here]&lt;br /&gt;
&lt;br /&gt;
==Using Python==&lt;br /&gt;
&lt;br /&gt;
Python programs can be run in several ways. Two of the most common ways are through the python shell and in .py files. &lt;br /&gt;
To enter the python shell, simply type &lt;br /&gt;
&lt;br /&gt;
 python&lt;br /&gt;
&lt;br /&gt;
into your shell. Interactive mode should start up, and you can type commands to be directly executed. You can exit interactive mode with ctrl+d or with &lt;br /&gt;
&lt;br /&gt;
 quit()&lt;br /&gt;
&lt;br /&gt;
To run a .py file, simply use:&lt;br /&gt;
&lt;br /&gt;
 python &amp;lt;filename&amp;gt; &amp;lt;args&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Syntax==&lt;br /&gt;
&lt;br /&gt;
Since the online documentation for Python is quite extensive and helpful, [http://docs.python.org/tutorial/index.html the online Python tutorial] is definitely the place for any and all of your Python-related needs. Be sure to read the [http://docs.python.org/tutorial/datastructures.html data structures] page to understand how they work in Python.&lt;br /&gt;
=Installing Python Modules=&lt;br /&gt;
Most likely the module will be distributed according to the python standard described [http://docs.python.org/install/#the-new-standard-distutils here], so you can simply unpack the archive, cd into the resulting directory, and execute the following command:&lt;br /&gt;
 python setup.py install&lt;br /&gt;
Note: you may have to execute the command using sudo depending on the permissions of the directories involved.&lt;br /&gt;
&lt;br /&gt;
Though there are yum packages available for installing many python modules, it is recommended that you use the above method if you are using eclipse with pydev, otherwise eclipse does not recognize that the module is installed, and you will simply have to ignore the unresolved import error and make due without autocomplete for that module. It's not ideal, but sometimes that's how it goes.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=256</id>
		<title>Python</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=256"/>
		<updated>2010-08-11T15:24:54Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Installing Python */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Python is an interpreted language with familiar syntax. It's a great language to use for parsing text files and rapid application development. Rather than the time-consuming write-compile-test-rewrite-recompile-retest-etc. cycle of languages like C and C++, Python scripts run through an interpreter. This eliminates the compile step and speeds up the development process.&lt;br /&gt;
=Getting Started=&lt;br /&gt;
==Installing Python==&lt;br /&gt;
&lt;br /&gt;
 yum install python&lt;br /&gt;
&lt;br /&gt;
should do the trick. Make sure that python is in your path with&lt;br /&gt;
&lt;br /&gt;
 which python&lt;br /&gt;
&lt;br /&gt;
and modify your path if necessary.&lt;br /&gt;
&lt;br /&gt;
===Installing Pydev (Optional)===&lt;br /&gt;
If, like me you're an IDE junkie then you may wish to install the eclipse pydev plugin (though it can be a little annoying when [[#Installing Python Modules]]&lt;br /&gt;
&lt;br /&gt;
==Using Python==&lt;br /&gt;
&lt;br /&gt;
Python programs can be run in several ways. Two of the most common ways are through the python shell and in .py files. &lt;br /&gt;
To enter the python shell, simply type &lt;br /&gt;
&lt;br /&gt;
 python&lt;br /&gt;
&lt;br /&gt;
into your shell. Interactive mode should start up, and you can type commands to be directly executed. You can exit interactive mode with ctrl+d or with &lt;br /&gt;
&lt;br /&gt;
 quit()&lt;br /&gt;
&lt;br /&gt;
To run a .py file, simply use:&lt;br /&gt;
&lt;br /&gt;
 python &amp;lt;filename&amp;gt; &amp;lt;args&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Syntax==&lt;br /&gt;
&lt;br /&gt;
Since the online documentation for Python is quite extensive and helpful, [http://docs.python.org/tutorial/index.html the online Python tutorial] is definitely the place for any and all of your Python-related needs. Be sure to read the [http://docs.python.org/tutorial/datastructures.html data structures] page to understand how they work in Python.&lt;br /&gt;
=Installing Python Modules=&lt;br /&gt;
Most likely the module will be distributed according to the python standard described [http://docs.python.org/install/#the-new-standard-distutils here], so you can simply unpack the archive, cd into the resulting directory, and execute the following command:&lt;br /&gt;
 python setup.py install&lt;br /&gt;
Note: you may have to execute the command using sudo depending on the permissions of the directories involved.&lt;br /&gt;
&lt;br /&gt;
Though there are yum packages available for installing many python modules, it is recommended that you use the above method if you are using eclipse with pydev, otherwise eclipse does not recognize that the module is installed, and you will simply have to ignore the unresolved import error and make due without autocomplete for that module. It's not ideal, but sometimes that's how it goes.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=255</id>
		<title>Python</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=255"/>
		<updated>2010-08-10T21:40:50Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Installing Python Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Python is an interpreted language with familiar syntax. It's a great language to use for parsing text files and rapid application development. Rather than the time-consuming write-compile-test-rewrite-recompile-retest-etc. cycle of languages like C and C++, Python scripts run through an interpreter. This eliminates the compile step and speeds up the development process.&lt;br /&gt;
=Getting Started=&lt;br /&gt;
==Installing Python==&lt;br /&gt;
&lt;br /&gt;
 yum install python&lt;br /&gt;
&lt;br /&gt;
should do the trick. Make sure that python is in your path with&lt;br /&gt;
&lt;br /&gt;
 which python&lt;br /&gt;
&lt;br /&gt;
and modify your path if necessary.&lt;br /&gt;
==Using Python==&lt;br /&gt;
&lt;br /&gt;
Python programs can be run in several ways. Two of the most common ways are through the python shell and in .py files. &lt;br /&gt;
To enter the python shell, simply type &lt;br /&gt;
&lt;br /&gt;
 python&lt;br /&gt;
&lt;br /&gt;
into your shell. Interactive mode should start up, and you can type commands to be directly executed. You can exit interactive mode with ctrl+d or with &lt;br /&gt;
&lt;br /&gt;
 quit()&lt;br /&gt;
&lt;br /&gt;
To run a .py file, simply use:&lt;br /&gt;
&lt;br /&gt;
 python &amp;lt;filename&amp;gt; &amp;lt;args&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Syntax==&lt;br /&gt;
&lt;br /&gt;
Since the online documentation for Python is quite extensive and helpful, [http://docs.python.org/tutorial/index.html the online Python tutorial] is definitely the place for any and all of your Python-related needs. Be sure to read the [http://docs.python.org/tutorial/datastructures.html data structures] page to understand how they work in Python.&lt;br /&gt;
=Installing Python Modules=&lt;br /&gt;
Most likely the module will be distributed according to the python standard described [http://docs.python.org/install/#the-new-standard-distutils here], so you can simply unpack the archive, cd into the resulting directory, and execute the following command:&lt;br /&gt;
 python setup.py install&lt;br /&gt;
Note: you may have to execute the command using sudo depending on the permissions of the directories involved.&lt;br /&gt;
&lt;br /&gt;
Though there are yum packages available for installing many python modules, it is recommended that you use the above method if you are using eclipse with pydev, otherwise eclipse does not recognize that the module is installed, and you will simply have to ignore the unresolved import error and make due without autocomplete for that module. It's not ideal, but sometimes that's how it goes.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=254</id>
		<title>Python</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=254"/>
		<updated>2010-08-10T21:19:11Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Installing Python Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Python is an interpreted language with familiar syntax. It's a great language to use for parsing text files and rapid application development. Rather than the time-consuming write-compile-test-rewrite-recompile-retest-etc. cycle of languages like C and C++, Python scripts run through an interpreter. This eliminates the compile step and speeds up the development process.&lt;br /&gt;
=Getting Started=&lt;br /&gt;
==Installing Python==&lt;br /&gt;
&lt;br /&gt;
 yum install python&lt;br /&gt;
&lt;br /&gt;
should do the trick. Make sure that python is in your path with&lt;br /&gt;
&lt;br /&gt;
 which python&lt;br /&gt;
&lt;br /&gt;
and modify your path if necessary.&lt;br /&gt;
==Using Python==&lt;br /&gt;
&lt;br /&gt;
Python programs can be run in several ways. Two of the most common ways are through the python shell and in .py files. &lt;br /&gt;
To enter the python shell, simply type &lt;br /&gt;
&lt;br /&gt;
 python&lt;br /&gt;
&lt;br /&gt;
into your shell. Interactive mode should start up, and you can type commands to be directly executed. You can exit interactive mode with ctrl+d or with &lt;br /&gt;
&lt;br /&gt;
 quit()&lt;br /&gt;
&lt;br /&gt;
To run a .py file, simply use:&lt;br /&gt;
&lt;br /&gt;
 python &amp;lt;filename&amp;gt; &amp;lt;args&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Syntax==&lt;br /&gt;
&lt;br /&gt;
Since the online documentation for Python is quite extensive and helpful, [http://docs.python.org/tutorial/index.html the online Python tutorial] is definitely the place for any and all of your Python-related needs. Be sure to read the [http://docs.python.org/tutorial/datastructures.html data structures] page to understand how they work in Python.&lt;br /&gt;
=Installing Python Modules=&lt;br /&gt;
Most likely the module will be distributed according to the python standard described [http://docs.python.org/install/#the-new-standard-distutils here], so you can simply unpack the archive, cd into the resulting directory, and execute the following command:&lt;br /&gt;
 python setup.py install&lt;br /&gt;
Note: you may have to execute the command using sudo depending on the permissions of the directories involved.&lt;br /&gt;
&lt;br /&gt;
Though there are yum packages available for installing many python modules, it is recommended that you use the above method because it saves you the trouble of having to move the module directory around or having to add the module to your python path if the install doesn't correctly place it in a directory that is already in your python path.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=253</id>
		<title>Python</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=253"/>
		<updated>2010-08-10T21:18:37Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Installing Python Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Python is an interpreted language with familiar syntax. It's a great language to use for parsing text files and rapid application development. Rather than the time-consuming write-compile-test-rewrite-recompile-retest-etc. cycle of languages like C and C++, Python scripts run through an interpreter. This eliminates the compile step and speeds up the development process.&lt;br /&gt;
=Getting Started=&lt;br /&gt;
==Installing Python==&lt;br /&gt;
&lt;br /&gt;
 yum install python&lt;br /&gt;
&lt;br /&gt;
should do the trick. Make sure that python is in your path with&lt;br /&gt;
&lt;br /&gt;
 which python&lt;br /&gt;
&lt;br /&gt;
and modify your path if necessary.&lt;br /&gt;
==Using Python==&lt;br /&gt;
&lt;br /&gt;
Python programs can be run in several ways. Two of the most common ways are through the python shell and in .py files. &lt;br /&gt;
To enter the python shell, simply type &lt;br /&gt;
&lt;br /&gt;
 python&lt;br /&gt;
&lt;br /&gt;
into your shell. Interactive mode should start up, and you can type commands to be directly executed. You can exit interactive mode with ctrl+d or with &lt;br /&gt;
&lt;br /&gt;
 quit()&lt;br /&gt;
&lt;br /&gt;
To run a .py file, simply use:&lt;br /&gt;
&lt;br /&gt;
 python &amp;lt;filename&amp;gt; &amp;lt;args&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Syntax==&lt;br /&gt;
&lt;br /&gt;
Since the online documentation for Python is quite extensive and helpful, [http://docs.python.org/tutorial/index.html the online Python tutorial] is definitely the place for any and all of your Python-related needs. Be sure to read the [http://docs.python.org/tutorial/datastructures.html data structures] page to understand how they work in Python.&lt;br /&gt;
=Installing Python Modules=&lt;br /&gt;
Most likely the module will be distributed according to the python standard described [http://docs.python.org/install/#the-new-standard-distutils here], so you can simply unpack the archive, cd into the resulting directory, and execute the following command:&lt;br /&gt;
 python setup.py install&lt;br /&gt;
Note: you may have to execute the command using sudo depending on the permissions of the directories involved.&lt;br /&gt;
&lt;br /&gt;
Though there are yum packages available for installing many python modules, it is recommended that you use the above method because it saves you the trouble of having to move the module directory around or having to add the module to your python path if the install doesn't correctly place it to a directory in your python path already.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=252</id>
		<title>Python</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=252"/>
		<updated>2010-08-10T20:44:33Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Installing Python Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Python is an interpreted language with familiar syntax. It's a great language to use for parsing text files and rapid application development. Rather than the time-consuming write-compile-test-rewrite-recompile-retest-etc. cycle of languages like C and C++, Python scripts run through an interpreter. This eliminates the compile step and speeds up the development process.&lt;br /&gt;
=Getting Started=&lt;br /&gt;
==Installing Python==&lt;br /&gt;
&lt;br /&gt;
 yum install python&lt;br /&gt;
&lt;br /&gt;
should do the trick. Make sure that python is in your path with&lt;br /&gt;
&lt;br /&gt;
 which python&lt;br /&gt;
&lt;br /&gt;
and modify your path if necessary.&lt;br /&gt;
==Using Python==&lt;br /&gt;
&lt;br /&gt;
Python programs can be run in several ways. Two of the most common ways are through the python shell and in .py files. &lt;br /&gt;
To enter the python shell, simply type &lt;br /&gt;
&lt;br /&gt;
 python&lt;br /&gt;
&lt;br /&gt;
into your shell. Interactive mode should start up, and you can type commands to be directly executed. You can exit interactive mode with ctrl+d or with &lt;br /&gt;
&lt;br /&gt;
 quit()&lt;br /&gt;
&lt;br /&gt;
To run a .py file, simply use:&lt;br /&gt;
&lt;br /&gt;
 python &amp;lt;filename&amp;gt; &amp;lt;args&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Syntax==&lt;br /&gt;
&lt;br /&gt;
Since the online documentation for Python is quite extensive and helpful, [http://docs.python.org/tutorial/index.html the online Python tutorial] is definitely the place for any and all of your Python-related needs. Be sure to read the [http://docs.python.org/tutorial/datastructures.html data structures] page to understand how they work in Python.&lt;br /&gt;
=Installing Python Modules=&lt;br /&gt;
If there isn't a yum package or rpm available for the desired module, then most likely the module will be distributed according to the python standard described [http://docs.python.org/install/#the-new-standard-distutils here], so you can simply unpack the archive, cd into the resulting directory, and execute the following command:&lt;br /&gt;
 python setup.py install&lt;br /&gt;
&lt;br /&gt;
Note: you may have to execute the command using sudo depending on the permissions of the directories involved.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=251</id>
		<title>Python</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=251"/>
		<updated>2010-08-10T20:21:04Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Getting Started */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Python is an interpreted language with familiar syntax. It's a great language to use for parsing text files and rapid application development. Rather than the time-consuming write-compile-test-rewrite-recompile-retest-etc. cycle of languages like C and C++, Python scripts run through an interpreter. This eliminates the compile step and speeds up the development process.&lt;br /&gt;
=Getting Started=&lt;br /&gt;
==Installing Python==&lt;br /&gt;
&lt;br /&gt;
 yum install python&lt;br /&gt;
&lt;br /&gt;
should do the trick. Make sure that python is in your path with&lt;br /&gt;
&lt;br /&gt;
 which python&lt;br /&gt;
&lt;br /&gt;
and modify your path if necessary.&lt;br /&gt;
==Using Python==&lt;br /&gt;
&lt;br /&gt;
Python programs can be run in several ways. Two of the most common ways are through the python shell and in .py files. &lt;br /&gt;
To enter the python shell, simply type &lt;br /&gt;
&lt;br /&gt;
 python&lt;br /&gt;
&lt;br /&gt;
into your shell. Interactive mode should start up, and you can type commands to be directly executed. You can exit interactive mode with ctrl+d or with &lt;br /&gt;
&lt;br /&gt;
 quit()&lt;br /&gt;
&lt;br /&gt;
To run a .py file, simply use:&lt;br /&gt;
&lt;br /&gt;
 python &amp;lt;filename&amp;gt; &amp;lt;args&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Syntax==&lt;br /&gt;
&lt;br /&gt;
Since the online documentation for Python is quite extensive and helpful, [http://docs.python.org/tutorial/index.html the online Python tutorial] is definitely the place for any and all of your Python-related needs. Be sure to read the [http://docs.python.org/tutorial/datastructures.html data structures] page to understand how they work in Python.&lt;br /&gt;
=Installing Python Modules=&lt;br /&gt;
If there isn't a yum package or rpm available for the desired module, then most likely the module will be distributed according to the python standard described [http://docs.python.org/install/#the-new-standard-distutils here], so you can simply unpack the archive, cd into the resulting directory, and execute the following command:&lt;br /&gt;
 python setup.py install&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=250</id>
		<title>PHP Original</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=250"/>
		<updated>2010-07-27T16:06:17Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Strings: Differences Between Java and PHP */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= PHP5 =&lt;br /&gt;
PHP is a server-side language. When a web request for PHP file comes in, the web server processes the PHP file to produce HTML output.  That is, the main function of most PHP scripts is to dynamically create HTML content.  PHP5 support for Apache is provided by mod_php5. In Ubuntu, you can provide PHP5 support by installing ''php5''. This will install the apache2 module and the php5 command line interpreter (useful for debugging). PHP files may have different extensions, but ''.php'' is the most common.  In the Apache configuration, you can specify which extensions are going to be treated as PHP scripts by adding the extensions to the list in the ''/etc/httpd/conf.d/php.conf'' file: &lt;br /&gt;
&lt;br /&gt;
  AddType text/html .php .phtml .php3&lt;br /&gt;
&lt;br /&gt;
==Declaring PHP==&lt;br /&gt;
If a PHP file is requested by a web user, Apache parses and interprets the file.  Any code declared between PHP tags is then executed and the total HTML output is sent back to the user. PHP code can be declared in a script file in any of the following ways, with the first being the most common:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 php?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script language=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You may see the following shortcut syntax for declaring php, but it may not work unless the shortcut syntax is configured for your server, as such we generally recommend sticking with one of the first ways of declaring php.&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
  PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, edit a new PHP file named ''info.php'', and put these lines in the file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Put the file in the web directory for your user account (remember that this is ''~username/.html/'' if you completed the first Module).  Then access the web page by going to ''http://server/~username/info.php''. You will see something similar to the picture below:&lt;br /&gt;
&lt;br /&gt;
[[Image:Php_info.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you look at the HTML code, you will see quite a lot of HTML output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;DTD/xhtml1-transitional.dtd&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&lt;br /&gt;
 body {background-color: #ffffff; color: #000000;}&lt;br /&gt;
 body, td, th, h1, h2 {font-family: sans-serif;}&lt;br /&gt;
 pre {margin: 0px; font-family: monospace;}&lt;br /&gt;
 a:link {color: #000099; text-decoration: none; background-color: #ffffff;}&lt;br /&gt;
 a:hover {text-decoration: underline;}&lt;br /&gt;
 table {border-collapse: collapse;}&lt;br /&gt;
 .center {text-align: center;}&lt;br /&gt;
 .center table { margin-left: auto; margin-right: auto; text-align: left;}&lt;br /&gt;
 .center th { text-align: center !important; }&lt;br /&gt;
 td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}&lt;br /&gt;
 h1 {font-size: 150%;}&lt;br /&gt;
 h2 {font-size: 125%;}&lt;br /&gt;
 .p {text-align: left;}&lt;br /&gt;
 .e {background-color: #ccccff; font-weight: bold; color: #000000;}&lt;br /&gt;
 .h {background-color: #9999cc; font-weight: bold; color: #000000;}&lt;br /&gt;
 .v {background-color: #cccccc; color: #000000;}&lt;br /&gt;
 .vr {background-color: #cccccc; text-align: right; color: #000000;}&lt;br /&gt;
 img {float: right; border: 0px;}&lt;br /&gt;
 hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}&lt;br /&gt;
 &amp;lt;/style&amp;gt;&lt;br /&gt;
 &amp;lt;title&amp;gt;phpinfo()&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
 &amp;lt;body&amp;gt;&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;table border=&amp;quot;0&amp;quot; cellpadding=&amp;quot;3&amp;quot; width=&amp;quot;600&amp;quot;&amp;gt;&lt;br /&gt;
 ....&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, none of that HTML was in the ''info.php'' file.  Instead, the ''phpinfo()'' line is a built-in PHP function that produces all of that HTML.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Variables require ''$'' at the beginning of their names (regardless of the purpose, be it setting, be it accessing). You don't need to specify the type of the variable; PHP will use the variable as the correct type based on how it is used in the code.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   #this is an integer&lt;br /&gt;
   $i=5;&lt;br /&gt;
   #this is a string&lt;br /&gt;
   $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in many other languages, statements end with a '';''.  To actually produce HTML output, you simply print the HTML from the PHP code.  The simplest way is to use the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
If you use a variable inside of a string, the value of the variable will be put in the string (you might also look at the ''echo'' function):&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   $i=5;&lt;br /&gt;
   print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will generate the string&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
If you create a file with just the above code, you will notice that your browser will only receive the above line. That is, there is no ''html'' tag or anything else. Thus, PHP files are normally a mix of HTML code and PHP code.&lt;br /&gt;
Most PHP scripts, then, look more like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code that the browser will receive is then:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  The value of i is 5&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can have any number of PHP segments and HTML segments in a PHP file.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?php $i=5; ?&amp;gt;&lt;br /&gt;
The value of i is &amp;lt;?php  print &amp;quot;$i&amp;quot;; ?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, whatever you print out will be interpreted as HTML by the client's browser, so you can (and will) use HTML tags in the output you produce.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?php print &amp;quot;This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt; ?&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in the following at the user's browser:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which would then render ''test'' in bold.&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
&lt;br /&gt;
Declaring arrays is very easy in PHP. Either you can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
$student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
$student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
$student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or alternatively&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student=array(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you can access the contents with an index into the array, as normal:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find the size of an array with ''count'' function, e.g., ''count($student)''&lt;br /&gt;
&lt;br /&gt;
In PHP, you can also have associative arrays, which are arrays whose members can be accessible with string indices. Here is an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$test[&amp;quot;test1&amp;quot;]=1;&lt;br /&gt;
$test[&amp;quot;test2&amp;quot;]=2;&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;test1=&amp;quot;,$test[test1],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
echo &amp;quot;test2=&amp;quot;,$test[test2],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access array keys through ''array_keys'' function.&lt;br /&gt;
===Strings: Differences Between Java and PHP===&lt;br /&gt;
&lt;br /&gt;
In php, unlike in java, you can enclose strings in single quotes or double quotes&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
The following is a valid string, just like in java.&lt;br /&gt;
 //A valid string in php and in java.&lt;br /&gt;
 $str=&amp;quot;hey jeff!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And in PHP, the following is also a valid string&lt;br /&gt;
 //A valid string in PHP, but invalid in java.&lt;br /&gt;
 $str='hey jeff!' &lt;br /&gt;
&lt;br /&gt;
However, there is a caveat when using the single quotes. If we want to then use MORE single quotes within the string We have to escape them&lt;br /&gt;
To yield the string&lt;br /&gt;
''I'm Jeff's string''&lt;br /&gt;
&lt;br /&gt;
we could use either of the following definitions:&lt;br /&gt;
&lt;br /&gt;
 $str=&amp;quot;I'm Jeff's string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 $str='I\'m Jeff\'s string'&lt;br /&gt;
&lt;br /&gt;
Additionally, when enclosing the string in single quotes we can use unescaped double quotes inside the string in the manner shown below which is especially useful when outputting html:&lt;br /&gt;
 $str='The man said &amp;quot;yes.&amp;quot;'&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if/elseif/else'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   some statements&lt;br /&gt;
 }&lt;br /&gt;
 elseif( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   more statements&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
    yet more statements&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string == &amp;quot;this is my string&amp;quot;) {&lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such thing as global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
PHP has ''for'' and ''while'' loops, similar to Java and C:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, the following code will print the sum of numbers from 1 to 10.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 echo &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The advantage of loops  becomes more obvious when you have to create a long table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Number&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Square&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
for($i=0;$i&amp;lt;100;$i++) {&lt;br /&gt;
echo &amp;quot;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;$i &amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;quot;,$i*$i,&amp;quot;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a table with two columns, each row contains a number and its square. &lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt normal control flow through a loop.  ''break'' jumps out of the loop entirely, while ''continue'' jumps to the end of the current loop iteration and flow resumes with the next loop iteration.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
PHP functions are identified with the keyword ''function'' followed by a function name and argument list for the function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
function name($arg1 [= constant], $arg2 [= constant], ...) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   function add($x,$y) {&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
   }&lt;br /&gt;
   echo &amp;quot;Sum of 2+5=&amp;quot;,add(2,5); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If an argument has a default value, it can be specified with the argument on the list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
      print &amp;quot;hello $name how are you today?&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
   helloMsg(&amp;quot;Alice&amp;quot;);&lt;br /&gt;
   helloMsg();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 hello Alice how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
 hello there how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Passing Variables ==&lt;br /&gt;
Input values can be passed to a script either through the URL request line or through data submitted by a form. Built in arrays $_GET['var'] and $_POST['var'] return the value of ''var'' depending on the access method. As we will see in the next section, values can also be passed through session variables. Finally, you can use cookies to store and retrieve values. &lt;br /&gt;
&lt;br /&gt;
Note that upon submitting a form, the page specified in the form tag's action attribute is loaded (with variables sent via post or get).&lt;br /&gt;
&lt;br /&gt;
===Get: Passing Variables via URL===&lt;br /&gt;
If you want to pass values in a URL, the format is ''http://yourserver/yourphpfile.php?var1=value1&amp;amp;var2=value2.....''&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code would be used to print the previous hello message according to a name given in the URL:&lt;br /&gt;
&lt;br /&gt;
greeting.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $person= $_GET['name'];&lt;br /&gt;
 helloMsg($person);&lt;br /&gt;
 function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
     print &amp;quot;hello $name how are you today?&amp;lt;/br&amp;gt;&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you access the URL like this:  ''greeting.php?name=Alice'', it will show ''hello Alice how are you today?''. However, if you access it with ''greeting.php'', it will print ''hello how are you today?''. Note that it does not use the default value ''there'' since ''$name'' is now a string (in this case, an empty string) and so there is argument being being passed to ''helloMsg''.&lt;br /&gt;
&lt;br /&gt;
===POST: Passing Variables via Form===&lt;br /&gt;
In PHP one method for passing variables from page to page is by sending it to the other page via posting. The way post works in&lt;br /&gt;
PHP is that PHP retrieves the variable names and values from a (slightly modified) html form and sends an those variables in the associative array named $_POST to the page specified in the FORM tag's ''action'' attribute. &lt;br /&gt;
&lt;br /&gt;
Note that receiving the data from a form is very similar to getting it from the URL. Consider the following form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;FORM action=&amp;quot;info.php&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;P&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;firstnamelabel&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;firstname&amp;quot; id=&amp;quot;firstnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;lastnamelabel&amp;quot;&amp;gt;Last name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;lastname&amp;quot; id=&amp;quot;lastnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;birthyearlabel&amp;quot;&amp;gt;Birth Year: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;birthyear&amp;quot; id=&amp;quot;birthyearlabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Male&amp;quot;&amp;gt; Male&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Female&amp;quot;&amp;gt; Female&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;submit&amp;quot; value=&amp;quot;Send&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;reset&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;/P&amp;gt;&lt;br /&gt;
&amp;lt;/FORM&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This posts the form data to ''info.php'', with inputs ''firstname'', ''lastname'', ''birthyear'', and ''gender''.&lt;br /&gt;
The values passed through the form for each input can be accessed in ''info.php'' with the $_POST array.&lt;br /&gt;
*&amp;lt;label&amp;gt; : The label tag just provides a text label for the form inputs.&lt;br /&gt;
*&amp;lt;input ''type=&amp;quot;input_type_here&amp;quot;''&amp;gt; : The input tag provides various means of inputting information into the form, there are various types of inputs such as text, radio, submit, reset, etc. &lt;br /&gt;
*&amp;lt;input ... ''name=&amp;quot;input_name&amp;quot;''&amp;gt; : &lt;br /&gt;
*&amp;lt;label ''for=&amp;quot;identifying_string&amp;quot;''&amp;gt; and &amp;lt;INPUT ... ''id=&amp;quot;identifying_string&amp;quot;'': The optional ''for'' attribute of the label tag associates the label with what we'll call an &amp;quot;identifying string&amp;quot;, which should match the string specified in the corresponding ''id'' portion of the input tag. If both of these attributes are in place, then upon clicking on the label, the corresponding input will be selected.&lt;br /&gt;
&lt;br /&gt;
info.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 echo  &amp;quot;Personal Data:&amp;quot;, $_POST['firstname'], &amp;quot; &amp;quot;, $_POST['lastname'], &amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 $birthyear=$_POST['birthyear'];&lt;br /&gt;
 echo  &amp;quot;Birth year:&amp;quot;, $_birthyear, &amp;quot; (Age:&amp;quot; , 2007-$birthyear, &amp;quot;)&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 echo &amp;quot;Gender: &amp;quot;, $_POST['gender'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For further help with using HTML forms with PHP, we recommend [http://www.tizag.com/phpT/forms.php this tutorial]&lt;br /&gt;
===Self-Submitting Forms===&lt;br /&gt;
Oftentimes we don't want to pass form values to a different page, but rather to the same page as contains the form so that we can process the information and display output on that same page. The way we do this is to specify that the form should submit to $_SERVER['PHP_SELF'] and not say, other_page.php.&lt;br /&gt;
Note that you can make self-submitting forms that use either the get or post method.&lt;br /&gt;
&lt;br /&gt;
The following example simply takes the data input from a form and, via posting, echoes it back to the user as bold text on the same page:&lt;br /&gt;
&lt;br /&gt;
bold_printer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Bold Printer&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action= &amp;lt;?php echo $_SERVER['PHP_SELF'] ?&amp;gt; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;label for=&amp;quot;text_field&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;user_text&amp;quot; id=&amp;quot;text_field&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Print in Bold&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    if(isset($_POST['user_text']))&lt;br /&gt;
    echo &amp;quot;&amp;lt;b&amp;gt;&amp;quot;, $_POST['user_text'], &amp;quot;&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Session Variables==&lt;br /&gt;
PHP provides a global array, ''$_SESSION'', for values that are available globally throughout a ''session''. The values will stay there until either you remove them in a PHP script, the user's browser is closed, or their session expires (you can tune the timeout period in the PHP configuration files).&lt;br /&gt;
&lt;br /&gt;
In order to access session values, in each php file, you should first initiate the session with the ''session_start()'' function call. After that, when you access a value with ''$_SESSION'', it will become a session values. For example:&lt;br /&gt;
&lt;br /&gt;
setsessionvars.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$_SESSION['SESSION']=42;&lt;br /&gt;
$_SESSION['username']=&amp;quot;ArthurDent&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This sets two variables, ''SESSION'' and ''username'' to be accessible by other pages in this session. In another page, you can access them like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
session_start();&lt;br /&gt;
if (!isset($_SESSION['SESSION'])) {&lt;br /&gt;
  echo &amp;quot;error session is not registered&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else echo &amp;quot;username=&amp;quot;,$_SESSION['username'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also remove a session variable from the session with unset command:&lt;br /&gt;
&lt;br /&gt;
 unset($_SESSION['SESSION']);&lt;br /&gt;
&lt;br /&gt;
==Redirect the browser to a different page==&lt;br /&gt;
&lt;br /&gt;
It is often useful to redirect the user's browser to a different webpage, for example, after processing inputs from a form.  Here is a simple example that shows how to do this, based on whether or not the ''LOGIN'' session variable is set:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
if(isset($_SESSION[&amp;quot;LOGIN&amp;quot;])){&lt;br /&gt;
	Header(&amp;quot;Location:/welcome.php&amp;quot;);&lt;br /&gt;
}else{&lt;br /&gt;
	Header(&amp;quot;Location:/login.php&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that anytime you call the ''Header'' function, it is adding information to the header section in the HTML object that is being requested.  As such, all ''Header'' calls should go before any other HTML output in the PHP script.&lt;br /&gt;
&lt;br /&gt;
==Sending a file to the Browser==&lt;br /&gt;
&lt;br /&gt;
Another useful function to know is the ''readfile'' function.  It takes a file on the webserver's file system and sends it to the web browser.  A simple example is given below, but note that you'll probably need to specify addition header fields in order to have the web browser download the file correctly. &lt;br /&gt;
For those of you who have used previous versions of PHP, note that mime_content_type($path) is now deprecated, use finfo_file instead&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$file=$_GET['file'];&lt;br /&gt;
$name = $_SESSION[&amp;quot;UNAME&amp;quot;];&lt;br /&gt;
$path=&amp;quot;/home/uploads/$name/$file&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension&lt;br /&gt;
if (!$finfo) {&lt;br /&gt;
 	echo &amp;quot;Opening fileinfo database failed&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$mime=finfo_file($finfo, $path);&lt;br /&gt;
finfo_close($finfo);&lt;br /&gt;
&lt;br /&gt;
header('content-type:'. $mime);&lt;br /&gt;
readfile($path);    &lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uploading files with PHP==&lt;br /&gt;
&lt;br /&gt;
This section describes one way to upload files with PHP.  First, you need an HTML form that contains an input with type ''file'':&lt;br /&gt;
&lt;br /&gt;
transfer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;uploader.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
Choose a file to upload: &amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you use a ''file'' input in a form, PHP automatically adds an entry in the ''$_FILES'' array with information about the file. For example, transfer.php called the file ''uploadedfile'', so $_FILES['uploadedfile'] refers to file that the user selected in the form.  The name of the original file is $_FILES['uploadedfile']['name'].   When you upload a file, it is put in a temporary location by the web server. This temporary name is $_FILES['uploadedfile']['tmp_name']. In PHP, you can move a file from its original location to a new location by calling a built-in function, ''move_uploaded_file'', which takes an argument for the source (temporary location) and destination location. Note that relative paths in the destination location are relative to the location of the PHP script being executed.&lt;br /&gt;
&lt;br /&gt;
uploader.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$target_path = &amp;quot;uploads/&amp;quot;;&lt;br /&gt;
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);&lt;br /&gt;
&lt;br /&gt;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {&lt;br /&gt;
    echo &amp;quot;The file &amp;quot;.  basename( $_FILES['uploadedfile']['name']) . &amp;quot; has been uploaded&amp;quot;;&lt;br /&gt;
} else{&lt;br /&gt;
    echo &amp;quot;There was an error uploading the file, please try again!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Eclipse ==&lt;br /&gt;
&lt;br /&gt;
For those of you who like programming with IDEs, Eclipse has support for PHP.  See:&lt;br /&gt;
http://www.eclipse.org.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=249</id>
		<title>PHP Original</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=249"/>
		<updated>2010-07-26T15:51:52Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Sending a file to the Browser */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= PHP5 =&lt;br /&gt;
PHP is a server-side language. When a web request for PHP file comes in, the web server processes the PHP file to produce HTML output.  That is, the main function of most PHP scripts is to dynamically create HTML content.  PHP5 support for Apache is provided by mod_php5. In Ubuntu, you can provide PHP5 support by installing ''php5''. This will install the apache2 module and the php5 command line interpreter (useful for debugging). PHP files may have different extensions, but ''.php'' is the most common.  In the Apache configuration, you can specify which extensions are going to be treated as PHP scripts by adding the extensions to the list in the ''/etc/httpd/conf.d/php.conf'' file: &lt;br /&gt;
&lt;br /&gt;
  AddType text/html .php .phtml .php3&lt;br /&gt;
&lt;br /&gt;
==Declaring PHP==&lt;br /&gt;
If a PHP file is requested by a web user, Apache parses and interprets the file.  Any code declared between PHP tags is then executed and the total HTML output is sent back to the user. PHP code can be declared in a script file in any of the following ways, with the first being the most common:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 php?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script language=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You may see the following shortcut syntax for declaring php, but it may not work unless the shortcut syntax is configured for your server, as such we generally recommend sticking with one of the first ways of declaring php.&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
  PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, edit a new PHP file named ''info.php'', and put these lines in the file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Put the file in the web directory for your user account (remember that this is ''~username/.html/'' if you completed the first Module).  Then access the web page by going to ''http://server/~username/info.php''. You will see something similar to the picture below:&lt;br /&gt;
&lt;br /&gt;
[[Image:Php_info.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you look at the HTML code, you will see quite a lot of HTML output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;DTD/xhtml1-transitional.dtd&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&lt;br /&gt;
 body {background-color: #ffffff; color: #000000;}&lt;br /&gt;
 body, td, th, h1, h2 {font-family: sans-serif;}&lt;br /&gt;
 pre {margin: 0px; font-family: monospace;}&lt;br /&gt;
 a:link {color: #000099; text-decoration: none; background-color: #ffffff;}&lt;br /&gt;
 a:hover {text-decoration: underline;}&lt;br /&gt;
 table {border-collapse: collapse;}&lt;br /&gt;
 .center {text-align: center;}&lt;br /&gt;
 .center table { margin-left: auto; margin-right: auto; text-align: left;}&lt;br /&gt;
 .center th { text-align: center !important; }&lt;br /&gt;
 td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}&lt;br /&gt;
 h1 {font-size: 150%;}&lt;br /&gt;
 h2 {font-size: 125%;}&lt;br /&gt;
 .p {text-align: left;}&lt;br /&gt;
 .e {background-color: #ccccff; font-weight: bold; color: #000000;}&lt;br /&gt;
 .h {background-color: #9999cc; font-weight: bold; color: #000000;}&lt;br /&gt;
 .v {background-color: #cccccc; color: #000000;}&lt;br /&gt;
 .vr {background-color: #cccccc; text-align: right; color: #000000;}&lt;br /&gt;
 img {float: right; border: 0px;}&lt;br /&gt;
 hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}&lt;br /&gt;
 &amp;lt;/style&amp;gt;&lt;br /&gt;
 &amp;lt;title&amp;gt;phpinfo()&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
 &amp;lt;body&amp;gt;&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;table border=&amp;quot;0&amp;quot; cellpadding=&amp;quot;3&amp;quot; width=&amp;quot;600&amp;quot;&amp;gt;&lt;br /&gt;
 ....&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, none of that HTML was in the ''info.php'' file.  Instead, the ''phpinfo()'' line is a built-in PHP function that produces all of that HTML.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Variables require ''$'' at the beginning of their names (regardless of the purpose, be it setting, be it accessing). You don't need to specify the type of the variable; PHP will use the variable as the correct type based on how it is used in the code.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   #this is an integer&lt;br /&gt;
   $i=5;&lt;br /&gt;
   #this is a string&lt;br /&gt;
   $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in many other languages, statements end with a '';''.  To actually produce HTML output, you simply print the HTML from the PHP code.  The simplest way is to use the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
If you use a variable inside of a string, the value of the variable will be put in the string (you might also look at the ''echo'' function):&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   $i=5;&lt;br /&gt;
   print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will generate the string&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
If you create a file with just the above code, you will notice that your browser will only receive the above line. That is, there is no ''html'' tag or anything else. Thus, PHP files are normally a mix of HTML code and PHP code.&lt;br /&gt;
Most PHP scripts, then, look more like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code that the browser will receive is then:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  The value of i is 5&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can have any number of PHP segments and HTML segments in a PHP file.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?php $i=5; ?&amp;gt;&lt;br /&gt;
The value of i is &amp;lt;?php  print &amp;quot;$i&amp;quot;; ?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, whatever you print out will be interpreted as HTML by the client's browser, so you can (and will) use HTML tags in the output you produce.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?php print &amp;quot;This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt; ?&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in the following at the user's browser:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which would then render ''test'' in bold.&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
&lt;br /&gt;
Declaring arrays is very easy in PHP. Either you can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
$student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
$student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
$student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or alternatively&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student=array(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you can access the contents with an index into the array, as normal:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find the size of an array with ''count'' function, e.g., ''count($student)''&lt;br /&gt;
&lt;br /&gt;
In PHP, you can also have associative arrays, which are arrays whose members can be accessible with string indices. Here is an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$test[&amp;quot;test1&amp;quot;]=1;&lt;br /&gt;
$test[&amp;quot;test2&amp;quot;]=2;&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;test1=&amp;quot;,$test[test1],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
echo &amp;quot;test2=&amp;quot;,$test[test2],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access array keys through ''array_keys'' function.&lt;br /&gt;
===Strings: Differences Between Java and PHP===&lt;br /&gt;
&lt;br /&gt;
In php, unlike in java, you can enclose strings in single quotes or double quotes&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
The following is a valid string, just like in java.&lt;br /&gt;
 //A valid string in php and in java.&lt;br /&gt;
 $str=&amp;quot;hey jeff!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And in PHP, the following is also a valid string&lt;br /&gt;
 //A valid string in PHP, but invalid in java.&lt;br /&gt;
 $str='hey jeff!' &lt;br /&gt;
&lt;br /&gt;
However, there is a caveat when using the single quotes. If we want to then use MORE single quotes within the string We have to escape them&lt;br /&gt;
To yield the string&lt;br /&gt;
''I'm Jeff's string''&lt;br /&gt;
&lt;br /&gt;
we could use either of the following definitions:&lt;br /&gt;
&lt;br /&gt;
 $str=&amp;quot;I'm Jeff's string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 $str='I\'m Jeff\'s string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if/elseif/else'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   some statements&lt;br /&gt;
 }&lt;br /&gt;
 elseif( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   more statements&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
    yet more statements&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string == &amp;quot;this is my string&amp;quot;) {&lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such thing as global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
PHP has ''for'' and ''while'' loops, similar to Java and C:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, the following code will print the sum of numbers from 1 to 10.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 echo &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The advantage of loops  becomes more obvious when you have to create a long table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Number&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Square&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
for($i=0;$i&amp;lt;100;$i++) {&lt;br /&gt;
echo &amp;quot;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;$i &amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;quot;,$i*$i,&amp;quot;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a table with two columns, each row contains a number and its square. &lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt normal control flow through a loop.  ''break'' jumps out of the loop entirely, while ''continue'' jumps to the end of the current loop iteration and flow resumes with the next loop iteration.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
PHP functions are identified with the keyword ''function'' followed by a function name and argument list for the function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
function name($arg1 [= constant], $arg2 [= constant], ...) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   function add($x,$y) {&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
   }&lt;br /&gt;
   echo &amp;quot;Sum of 2+5=&amp;quot;,add(2,5); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If an argument has a default value, it can be specified with the argument on the list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
      print &amp;quot;hello $name how are you today?&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
   helloMsg(&amp;quot;Alice&amp;quot;);&lt;br /&gt;
   helloMsg();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 hello Alice how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
 hello there how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Passing Variables ==&lt;br /&gt;
Input values can be passed to a script either through the URL request line or through data submitted by a form. Built in arrays $_GET['var'] and $_POST['var'] return the value of ''var'' depending on the access method. As we will see in the next section, values can also be passed through session variables. Finally, you can use cookies to store and retrieve values. &lt;br /&gt;
&lt;br /&gt;
Note that upon submitting a form, the page specified in the form tag's action attribute is loaded (with variables sent via post or get).&lt;br /&gt;
&lt;br /&gt;
===Get: Passing Variables via URL===&lt;br /&gt;
If you want to pass values in a URL, the format is ''http://yourserver/yourphpfile.php?var1=value1&amp;amp;var2=value2.....''&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code would be used to print the previous hello message according to a name given in the URL:&lt;br /&gt;
&lt;br /&gt;
greeting.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $person= $_GET['name'];&lt;br /&gt;
 helloMsg($person);&lt;br /&gt;
 function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
     print &amp;quot;hello $name how are you today?&amp;lt;/br&amp;gt;&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you access the URL like this:  ''greeting.php?name=Alice'', it will show ''hello Alice how are you today?''. However, if you access it with ''greeting.php'', it will print ''hello how are you today?''. Note that it does not use the default value ''there'' since ''$name'' is now a string (in this case, an empty string) and so there is argument being being passed to ''helloMsg''.&lt;br /&gt;
&lt;br /&gt;
===POST: Passing Variables via Form===&lt;br /&gt;
In PHP one method for passing variables from page to page is by sending it to the other page via posting. The way post works in&lt;br /&gt;
PHP is that PHP retrieves the variable names and values from a (slightly modified) html form and sends an those variables in the associative array named $_POST to the page specified in the FORM tag's ''action'' attribute. &lt;br /&gt;
&lt;br /&gt;
Note that receiving the data from a form is very similar to getting it from the URL. Consider the following form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;FORM action=&amp;quot;info.php&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;P&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;firstnamelabel&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;firstname&amp;quot; id=&amp;quot;firstnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;lastnamelabel&amp;quot;&amp;gt;Last name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;lastname&amp;quot; id=&amp;quot;lastnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;birthyearlabel&amp;quot;&amp;gt;Birth Year: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;birthyear&amp;quot; id=&amp;quot;birthyearlabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Male&amp;quot;&amp;gt; Male&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Female&amp;quot;&amp;gt; Female&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;submit&amp;quot; value=&amp;quot;Send&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;reset&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;/P&amp;gt;&lt;br /&gt;
&amp;lt;/FORM&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This posts the form data to ''info.php'', with inputs ''firstname'', ''lastname'', ''birthyear'', and ''gender''.&lt;br /&gt;
The values passed through the form for each input can be accessed in ''info.php'' with the $_POST array.&lt;br /&gt;
*&amp;lt;label&amp;gt; : The label tag just provides a text label for the form inputs.&lt;br /&gt;
*&amp;lt;input ''type=&amp;quot;input_type_here&amp;quot;''&amp;gt; : The input tag provides various means of inputting information into the form, there are various types of inputs such as text, radio, submit, reset, etc. &lt;br /&gt;
*&amp;lt;input ... ''name=&amp;quot;input_name&amp;quot;''&amp;gt; : &lt;br /&gt;
*&amp;lt;label ''for=&amp;quot;identifying_string&amp;quot;''&amp;gt; and &amp;lt;INPUT ... ''id=&amp;quot;identifying_string&amp;quot;'': The optional ''for'' attribute of the label tag associates the label with what we'll call an &amp;quot;identifying string&amp;quot;, which should match the string specified in the corresponding ''id'' portion of the input tag. If both of these attributes are in place, then upon clicking on the label, the corresponding input will be selected.&lt;br /&gt;
&lt;br /&gt;
info.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 echo  &amp;quot;Personal Data:&amp;quot;, $_POST['firstname'], &amp;quot; &amp;quot;, $_POST['lastname'], &amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 $birthyear=$_POST['birthyear'];&lt;br /&gt;
 echo  &amp;quot;Birth year:&amp;quot;, $_birthyear, &amp;quot; (Age:&amp;quot; , 2007-$birthyear, &amp;quot;)&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 echo &amp;quot;Gender: &amp;quot;, $_POST['gender'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For further help with using HTML forms with PHP, we recommend [http://www.tizag.com/phpT/forms.php this tutorial]&lt;br /&gt;
===Self-Submitting Forms===&lt;br /&gt;
Oftentimes we don't want to pass form values to a different page, but rather to the same page as contains the form so that we can process the information and display output on that same page. The way we do this is to specify that the form should submit to $_SERVER['PHP_SELF'] and not say, other_page.php.&lt;br /&gt;
Note that you can make self-submitting forms that use either the get or post method.&lt;br /&gt;
&lt;br /&gt;
The following example simply takes the data input from a form and, via posting, echoes it back to the user as bold text on the same page:&lt;br /&gt;
&lt;br /&gt;
bold_printer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Bold Printer&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action= &amp;lt;?php echo $_SERVER['PHP_SELF'] ?&amp;gt; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;label for=&amp;quot;text_field&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;user_text&amp;quot; id=&amp;quot;text_field&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Print in Bold&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    if(isset($_POST['user_text']))&lt;br /&gt;
    echo &amp;quot;&amp;lt;b&amp;gt;&amp;quot;, $_POST['user_text'], &amp;quot;&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Session Variables==&lt;br /&gt;
PHP provides a global array, ''$_SESSION'', for values that are available globally throughout a ''session''. The values will stay there until either you remove them in a PHP script, the user's browser is closed, or their session expires (you can tune the timeout period in the PHP configuration files).&lt;br /&gt;
&lt;br /&gt;
In order to access session values, in each php file, you should first initiate the session with the ''session_start()'' function call. After that, when you access a value with ''$_SESSION'', it will become a session values. For example:&lt;br /&gt;
&lt;br /&gt;
setsessionvars.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$_SESSION['SESSION']=42;&lt;br /&gt;
$_SESSION['username']=&amp;quot;ArthurDent&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This sets two variables, ''SESSION'' and ''username'' to be accessible by other pages in this session. In another page, you can access them like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
session_start();&lt;br /&gt;
if (!isset($_SESSION['SESSION'])) {&lt;br /&gt;
  echo &amp;quot;error session is not registered&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else echo &amp;quot;username=&amp;quot;,$_SESSION['username'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also remove a session variable from the session with unset command:&lt;br /&gt;
&lt;br /&gt;
 unset($_SESSION['SESSION']);&lt;br /&gt;
&lt;br /&gt;
==Redirect the browser to a different page==&lt;br /&gt;
&lt;br /&gt;
It is often useful to redirect the user's browser to a different webpage, for example, after processing inputs from a form.  Here is a simple example that shows how to do this, based on whether or not the ''LOGIN'' session variable is set:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
if(isset($_SESSION[&amp;quot;LOGIN&amp;quot;])){&lt;br /&gt;
	Header(&amp;quot;Location:/welcome.php&amp;quot;);&lt;br /&gt;
}else{&lt;br /&gt;
	Header(&amp;quot;Location:/login.php&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that anytime you call the ''Header'' function, it is adding information to the header section in the HTML object that is being requested.  As such, all ''Header'' calls should go before any other HTML output in the PHP script.&lt;br /&gt;
&lt;br /&gt;
==Sending a file to the Browser==&lt;br /&gt;
&lt;br /&gt;
Another useful function to know is the ''readfile'' function.  It takes a file on the webserver's file system and sends it to the web browser.  A simple example is given below, but note that you'll probably need to specify addition header fields in order to have the web browser download the file correctly. &lt;br /&gt;
For those of you who have used previous versions of PHP, note that mime_content_type($path) is now deprecated, use finfo_file instead&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$file=$_GET['file'];&lt;br /&gt;
$name = $_SESSION[&amp;quot;UNAME&amp;quot;];&lt;br /&gt;
$path=&amp;quot;/home/uploads/$name/$file&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension&lt;br /&gt;
if (!$finfo) {&lt;br /&gt;
 	echo &amp;quot;Opening fileinfo database failed&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$mime=finfo_file($finfo, $path);&lt;br /&gt;
finfo_close($finfo);&lt;br /&gt;
&lt;br /&gt;
header('content-type:'. $mime);&lt;br /&gt;
readfile($path);    &lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uploading files with PHP==&lt;br /&gt;
&lt;br /&gt;
This section describes one way to upload files with PHP.  First, you need an HTML form that contains an input with type ''file'':&lt;br /&gt;
&lt;br /&gt;
transfer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;uploader.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
Choose a file to upload: &amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you use a ''file'' input in a form, PHP automatically adds an entry in the ''$_FILES'' array with information about the file. For example, transfer.php called the file ''uploadedfile'', so $_FILES['uploadedfile'] refers to file that the user selected in the form.  The name of the original file is $_FILES['uploadedfile']['name'].   When you upload a file, it is put in a temporary location by the web server. This temporary name is $_FILES['uploadedfile']['tmp_name']. In PHP, you can move a file from its original location to a new location by calling a built-in function, ''move_uploaded_file'', which takes an argument for the source (temporary location) and destination location. Note that relative paths in the destination location are relative to the location of the PHP script being executed.&lt;br /&gt;
&lt;br /&gt;
uploader.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$target_path = &amp;quot;uploads/&amp;quot;;&lt;br /&gt;
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);&lt;br /&gt;
&lt;br /&gt;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {&lt;br /&gt;
    echo &amp;quot;The file &amp;quot;.  basename( $_FILES['uploadedfile']['name']) . &amp;quot; has been uploaded&amp;quot;;&lt;br /&gt;
} else{&lt;br /&gt;
    echo &amp;quot;There was an error uploading the file, please try again!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Eclipse ==&lt;br /&gt;
&lt;br /&gt;
For those of you who like programming with IDEs, Eclipse has support for PHP.  See:&lt;br /&gt;
http://www.eclipse.org.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=248</id>
		<title>PHP Original</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=248"/>
		<updated>2010-07-26T15:51:17Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Redirect the browser to a different page */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= PHP5 =&lt;br /&gt;
PHP is a server-side language. When a web request for PHP file comes in, the web server processes the PHP file to produce HTML output.  That is, the main function of most PHP scripts is to dynamically create HTML content.  PHP5 support for Apache is provided by mod_php5. In Ubuntu, you can provide PHP5 support by installing ''php5''. This will install the apache2 module and the php5 command line interpreter (useful for debugging). PHP files may have different extensions, but ''.php'' is the most common.  In the Apache configuration, you can specify which extensions are going to be treated as PHP scripts by adding the extensions to the list in the ''/etc/httpd/conf.d/php.conf'' file: &lt;br /&gt;
&lt;br /&gt;
  AddType text/html .php .phtml .php3&lt;br /&gt;
&lt;br /&gt;
==Declaring PHP==&lt;br /&gt;
If a PHP file is requested by a web user, Apache parses and interprets the file.  Any code declared between PHP tags is then executed and the total HTML output is sent back to the user. PHP code can be declared in a script file in any of the following ways, with the first being the most common:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 php?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script language=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You may see the following shortcut syntax for declaring php, but it may not work unless the shortcut syntax is configured for your server, as such we generally recommend sticking with one of the first ways of declaring php.&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
  PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, edit a new PHP file named ''info.php'', and put these lines in the file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Put the file in the web directory for your user account (remember that this is ''~username/.html/'' if you completed the first Module).  Then access the web page by going to ''http://server/~username/info.php''. You will see something similar to the picture below:&lt;br /&gt;
&lt;br /&gt;
[[Image:Php_info.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you look at the HTML code, you will see quite a lot of HTML output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;DTD/xhtml1-transitional.dtd&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&lt;br /&gt;
 body {background-color: #ffffff; color: #000000;}&lt;br /&gt;
 body, td, th, h1, h2 {font-family: sans-serif;}&lt;br /&gt;
 pre {margin: 0px; font-family: monospace;}&lt;br /&gt;
 a:link {color: #000099; text-decoration: none; background-color: #ffffff;}&lt;br /&gt;
 a:hover {text-decoration: underline;}&lt;br /&gt;
 table {border-collapse: collapse;}&lt;br /&gt;
 .center {text-align: center;}&lt;br /&gt;
 .center table { margin-left: auto; margin-right: auto; text-align: left;}&lt;br /&gt;
 .center th { text-align: center !important; }&lt;br /&gt;
 td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}&lt;br /&gt;
 h1 {font-size: 150%;}&lt;br /&gt;
 h2 {font-size: 125%;}&lt;br /&gt;
 .p {text-align: left;}&lt;br /&gt;
 .e {background-color: #ccccff; font-weight: bold; color: #000000;}&lt;br /&gt;
 .h {background-color: #9999cc; font-weight: bold; color: #000000;}&lt;br /&gt;
 .v {background-color: #cccccc; color: #000000;}&lt;br /&gt;
 .vr {background-color: #cccccc; text-align: right; color: #000000;}&lt;br /&gt;
 img {float: right; border: 0px;}&lt;br /&gt;
 hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}&lt;br /&gt;
 &amp;lt;/style&amp;gt;&lt;br /&gt;
 &amp;lt;title&amp;gt;phpinfo()&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
 &amp;lt;body&amp;gt;&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;table border=&amp;quot;0&amp;quot; cellpadding=&amp;quot;3&amp;quot; width=&amp;quot;600&amp;quot;&amp;gt;&lt;br /&gt;
 ....&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, none of that HTML was in the ''info.php'' file.  Instead, the ''phpinfo()'' line is a built-in PHP function that produces all of that HTML.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Variables require ''$'' at the beginning of their names (regardless of the purpose, be it setting, be it accessing). You don't need to specify the type of the variable; PHP will use the variable as the correct type based on how it is used in the code.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   #this is an integer&lt;br /&gt;
   $i=5;&lt;br /&gt;
   #this is a string&lt;br /&gt;
   $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in many other languages, statements end with a '';''.  To actually produce HTML output, you simply print the HTML from the PHP code.  The simplest way is to use the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
If you use a variable inside of a string, the value of the variable will be put in the string (you might also look at the ''echo'' function):&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   $i=5;&lt;br /&gt;
   print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will generate the string&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
If you create a file with just the above code, you will notice that your browser will only receive the above line. That is, there is no ''html'' tag or anything else. Thus, PHP files are normally a mix of HTML code and PHP code.&lt;br /&gt;
Most PHP scripts, then, look more like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code that the browser will receive is then:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  The value of i is 5&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can have any number of PHP segments and HTML segments in a PHP file.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?php $i=5; ?&amp;gt;&lt;br /&gt;
The value of i is &amp;lt;?php  print &amp;quot;$i&amp;quot;; ?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, whatever you print out will be interpreted as HTML by the client's browser, so you can (and will) use HTML tags in the output you produce.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?php print &amp;quot;This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt; ?&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in the following at the user's browser:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which would then render ''test'' in bold.&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
&lt;br /&gt;
Declaring arrays is very easy in PHP. Either you can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
$student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
$student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
$student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or alternatively&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student=array(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you can access the contents with an index into the array, as normal:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find the size of an array with ''count'' function, e.g., ''count($student)''&lt;br /&gt;
&lt;br /&gt;
In PHP, you can also have associative arrays, which are arrays whose members can be accessible with string indices. Here is an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$test[&amp;quot;test1&amp;quot;]=1;&lt;br /&gt;
$test[&amp;quot;test2&amp;quot;]=2;&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;test1=&amp;quot;,$test[test1],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
echo &amp;quot;test2=&amp;quot;,$test[test2],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access array keys through ''array_keys'' function.&lt;br /&gt;
===Strings: Differences Between Java and PHP===&lt;br /&gt;
&lt;br /&gt;
In php, unlike in java, you can enclose strings in single quotes or double quotes&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
The following is a valid string, just like in java.&lt;br /&gt;
 //A valid string in php and in java.&lt;br /&gt;
 $str=&amp;quot;hey jeff!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And in PHP, the following is also a valid string&lt;br /&gt;
 //A valid string in PHP, but invalid in java.&lt;br /&gt;
 $str='hey jeff!' &lt;br /&gt;
&lt;br /&gt;
However, there is a caveat when using the single quotes. If we want to then use MORE single quotes within the string We have to escape them&lt;br /&gt;
To yield the string&lt;br /&gt;
''I'm Jeff's string''&lt;br /&gt;
&lt;br /&gt;
we could use either of the following definitions:&lt;br /&gt;
&lt;br /&gt;
 $str=&amp;quot;I'm Jeff's string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 $str='I\'m Jeff\'s string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if/elseif/else'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   some statements&lt;br /&gt;
 }&lt;br /&gt;
 elseif( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   more statements&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
    yet more statements&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string == &amp;quot;this is my string&amp;quot;) {&lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such thing as global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
PHP has ''for'' and ''while'' loops, similar to Java and C:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, the following code will print the sum of numbers from 1 to 10.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 echo &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The advantage of loops  becomes more obvious when you have to create a long table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Number&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Square&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
for($i=0;$i&amp;lt;100;$i++) {&lt;br /&gt;
echo &amp;quot;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;$i &amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;quot;,$i*$i,&amp;quot;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a table with two columns, each row contains a number and its square. &lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt normal control flow through a loop.  ''break'' jumps out of the loop entirely, while ''continue'' jumps to the end of the current loop iteration and flow resumes with the next loop iteration.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
PHP functions are identified with the keyword ''function'' followed by a function name and argument list for the function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
function name($arg1 [= constant], $arg2 [= constant], ...) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   function add($x,$y) {&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
   }&lt;br /&gt;
   echo &amp;quot;Sum of 2+5=&amp;quot;,add(2,5); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If an argument has a default value, it can be specified with the argument on the list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
      print &amp;quot;hello $name how are you today?&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
   helloMsg(&amp;quot;Alice&amp;quot;);&lt;br /&gt;
   helloMsg();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 hello Alice how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
 hello there how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Passing Variables ==&lt;br /&gt;
Input values can be passed to a script either through the URL request line or through data submitted by a form. Built in arrays $_GET['var'] and $_POST['var'] return the value of ''var'' depending on the access method. As we will see in the next section, values can also be passed through session variables. Finally, you can use cookies to store and retrieve values. &lt;br /&gt;
&lt;br /&gt;
Note that upon submitting a form, the page specified in the form tag's action attribute is loaded (with variables sent via post or get).&lt;br /&gt;
&lt;br /&gt;
===Get: Passing Variables via URL===&lt;br /&gt;
If you want to pass values in a URL, the format is ''http://yourserver/yourphpfile.php?var1=value1&amp;amp;var2=value2.....''&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code would be used to print the previous hello message according to a name given in the URL:&lt;br /&gt;
&lt;br /&gt;
greeting.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $person= $_GET['name'];&lt;br /&gt;
 helloMsg($person);&lt;br /&gt;
 function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
     print &amp;quot;hello $name how are you today?&amp;lt;/br&amp;gt;&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you access the URL like this:  ''greeting.php?name=Alice'', it will show ''hello Alice how are you today?''. However, if you access it with ''greeting.php'', it will print ''hello how are you today?''. Note that it does not use the default value ''there'' since ''$name'' is now a string (in this case, an empty string) and so there is argument being being passed to ''helloMsg''.&lt;br /&gt;
&lt;br /&gt;
===POST: Passing Variables via Form===&lt;br /&gt;
In PHP one method for passing variables from page to page is by sending it to the other page via posting. The way post works in&lt;br /&gt;
PHP is that PHP retrieves the variable names and values from a (slightly modified) html form and sends an those variables in the associative array named $_POST to the page specified in the FORM tag's ''action'' attribute. &lt;br /&gt;
&lt;br /&gt;
Note that receiving the data from a form is very similar to getting it from the URL. Consider the following form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;FORM action=&amp;quot;info.php&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;P&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;firstnamelabel&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;firstname&amp;quot; id=&amp;quot;firstnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;lastnamelabel&amp;quot;&amp;gt;Last name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;lastname&amp;quot; id=&amp;quot;lastnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;birthyearlabel&amp;quot;&amp;gt;Birth Year: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;birthyear&amp;quot; id=&amp;quot;birthyearlabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Male&amp;quot;&amp;gt; Male&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Female&amp;quot;&amp;gt; Female&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;submit&amp;quot; value=&amp;quot;Send&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;reset&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;/P&amp;gt;&lt;br /&gt;
&amp;lt;/FORM&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This posts the form data to ''info.php'', with inputs ''firstname'', ''lastname'', ''birthyear'', and ''gender''.&lt;br /&gt;
The values passed through the form for each input can be accessed in ''info.php'' with the $_POST array.&lt;br /&gt;
*&amp;lt;label&amp;gt; : The label tag just provides a text label for the form inputs.&lt;br /&gt;
*&amp;lt;input ''type=&amp;quot;input_type_here&amp;quot;''&amp;gt; : The input tag provides various means of inputting information into the form, there are various types of inputs such as text, radio, submit, reset, etc. &lt;br /&gt;
*&amp;lt;input ... ''name=&amp;quot;input_name&amp;quot;''&amp;gt; : &lt;br /&gt;
*&amp;lt;label ''for=&amp;quot;identifying_string&amp;quot;''&amp;gt; and &amp;lt;INPUT ... ''id=&amp;quot;identifying_string&amp;quot;'': The optional ''for'' attribute of the label tag associates the label with what we'll call an &amp;quot;identifying string&amp;quot;, which should match the string specified in the corresponding ''id'' portion of the input tag. If both of these attributes are in place, then upon clicking on the label, the corresponding input will be selected.&lt;br /&gt;
&lt;br /&gt;
info.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 echo  &amp;quot;Personal Data:&amp;quot;, $_POST['firstname'], &amp;quot; &amp;quot;, $_POST['lastname'], &amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 $birthyear=$_POST['birthyear'];&lt;br /&gt;
 echo  &amp;quot;Birth year:&amp;quot;, $_birthyear, &amp;quot; (Age:&amp;quot; , 2007-$birthyear, &amp;quot;)&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 echo &amp;quot;Gender: &amp;quot;, $_POST['gender'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For further help with using HTML forms with PHP, we recommend [http://www.tizag.com/phpT/forms.php this tutorial]&lt;br /&gt;
===Self-Submitting Forms===&lt;br /&gt;
Oftentimes we don't want to pass form values to a different page, but rather to the same page as contains the form so that we can process the information and display output on that same page. The way we do this is to specify that the form should submit to $_SERVER['PHP_SELF'] and not say, other_page.php.&lt;br /&gt;
Note that you can make self-submitting forms that use either the get or post method.&lt;br /&gt;
&lt;br /&gt;
The following example simply takes the data input from a form and, via posting, echoes it back to the user as bold text on the same page:&lt;br /&gt;
&lt;br /&gt;
bold_printer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Bold Printer&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action= &amp;lt;?php echo $_SERVER['PHP_SELF'] ?&amp;gt; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;label for=&amp;quot;text_field&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;user_text&amp;quot; id=&amp;quot;text_field&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Print in Bold&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    if(isset($_POST['user_text']))&lt;br /&gt;
    echo &amp;quot;&amp;lt;b&amp;gt;&amp;quot;, $_POST['user_text'], &amp;quot;&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Session Variables==&lt;br /&gt;
PHP provides a global array, ''$_SESSION'', for values that are available globally throughout a ''session''. The values will stay there until either you remove them in a PHP script, the user's browser is closed, or their session expires (you can tune the timeout period in the PHP configuration files).&lt;br /&gt;
&lt;br /&gt;
In order to access session values, in each php file, you should first initiate the session with the ''session_start()'' function call. After that, when you access a value with ''$_SESSION'', it will become a session values. For example:&lt;br /&gt;
&lt;br /&gt;
setsessionvars.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$_SESSION['SESSION']=42;&lt;br /&gt;
$_SESSION['username']=&amp;quot;ArthurDent&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This sets two variables, ''SESSION'' and ''username'' to be accessible by other pages in this session. In another page, you can access them like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
session_start();&lt;br /&gt;
if (!isset($_SESSION['SESSION'])) {&lt;br /&gt;
  echo &amp;quot;error session is not registered&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else echo &amp;quot;username=&amp;quot;,$_SESSION['username'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also remove a session variable from the session with unset command:&lt;br /&gt;
&lt;br /&gt;
 unset($_SESSION['SESSION']);&lt;br /&gt;
&lt;br /&gt;
==Redirect the browser to a different page==&lt;br /&gt;
&lt;br /&gt;
It is often useful to redirect the user's browser to a different webpage, for example, after processing inputs from a form.  Here is a simple example that shows how to do this, based on whether or not the ''LOGIN'' session variable is set:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
if(isset($_SESSION[&amp;quot;LOGIN&amp;quot;])){&lt;br /&gt;
	Header(&amp;quot;Location:/welcome.php&amp;quot;);&lt;br /&gt;
}else{&lt;br /&gt;
	Header(&amp;quot;Location:/login.php&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that anytime you call the ''Header'' function, it is adding information to the header section in the HTML object that is being requested.  As such, all ''Header'' calls should go before any other HTML output in the PHP script.&lt;br /&gt;
&lt;br /&gt;
==Sending a file to the Browser==&lt;br /&gt;
&lt;br /&gt;
Another useful function to know is the ''readfile'' function.  It takes a file on the webserver's file system and sends it to the web browser.  A simple example is given below, but note that you'll probably need to specify addition header fields in order to have the web browser download the file correctly. &lt;br /&gt;
For those of you who have used previous versions of PHP, note that mime_content_type($path) is now deprecated, use finfo_file instead&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$file=$_GET['file'];&lt;br /&gt;
$name = $_SESSION[&amp;quot;UNAME&amp;quot;];&lt;br /&gt;
$path=&amp;quot;/home/uploads/$name/$file&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension&lt;br /&gt;
if (!$finfo) {&lt;br /&gt;
 	echo &amp;quot;Opening fileinfo database failed&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$mime=finfo_file($finfo, $path);&lt;br /&gt;
finfo_close($finfo);&lt;br /&gt;
&lt;br /&gt;
header('content-type:'. $mime);&lt;br /&gt;
readfile($path);    &lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uploading files with PHP==&lt;br /&gt;
&lt;br /&gt;
This section describes one way to upload files with PHP.  First, you need an HTML form that contains an input with type ''file'':&lt;br /&gt;
&lt;br /&gt;
transfer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;uploader.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
Choose a file to upload: &amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you use a ''file'' input in a form, PHP automatically adds an entry in the ''$_FILES'' array with information about the file. For example, transfer.php called the file ''uploadedfile'', so $_FILES['uploadedfile'] refers to file that the user selected in the form.  The name of the original file is $_FILES['uploadedfile']['name'].   When you upload a file, it is put in a temporary location by the web server. This temporary name is $_FILES['uploadedfile']['tmp_name']. In PHP, you can move a file from its original location to a new location by calling a built-in function, ''move_uploaded_file'', which takes an argument for the source (temporary location) and destination location. Note that relative paths in the destination location are relative to the location of the PHP script being executed.&lt;br /&gt;
&lt;br /&gt;
uploader.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$target_path = &amp;quot;uploads/&amp;quot;;&lt;br /&gt;
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);&lt;br /&gt;
&lt;br /&gt;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {&lt;br /&gt;
    echo &amp;quot;The file &amp;quot;.  basename( $_FILES['uploadedfile']['name']) . &amp;quot; has been uploaded&amp;quot;;&lt;br /&gt;
} else{&lt;br /&gt;
    echo &amp;quot;There was an error uploading the file, please try again!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Eclipse ==&lt;br /&gt;
&lt;br /&gt;
For those of you who like programming with IDEs, Eclipse has support for PHP.  See:&lt;br /&gt;
http://www.eclipse.org.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=247</id>
		<title>PHP Original</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=247"/>
		<updated>2010-07-26T15:51:06Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Session Variables */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= PHP5 =&lt;br /&gt;
PHP is a server-side language. When a web request for PHP file comes in, the web server processes the PHP file to produce HTML output.  That is, the main function of most PHP scripts is to dynamically create HTML content.  PHP5 support for Apache is provided by mod_php5. In Ubuntu, you can provide PHP5 support by installing ''php5''. This will install the apache2 module and the php5 command line interpreter (useful for debugging). PHP files may have different extensions, but ''.php'' is the most common.  In the Apache configuration, you can specify which extensions are going to be treated as PHP scripts by adding the extensions to the list in the ''/etc/httpd/conf.d/php.conf'' file: &lt;br /&gt;
&lt;br /&gt;
  AddType text/html .php .phtml .php3&lt;br /&gt;
&lt;br /&gt;
==Declaring PHP==&lt;br /&gt;
If a PHP file is requested by a web user, Apache parses and interprets the file.  Any code declared between PHP tags is then executed and the total HTML output is sent back to the user. PHP code can be declared in a script file in any of the following ways, with the first being the most common:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 php?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script language=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You may see the following shortcut syntax for declaring php, but it may not work unless the shortcut syntax is configured for your server, as such we generally recommend sticking with one of the first ways of declaring php.&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
  PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, edit a new PHP file named ''info.php'', and put these lines in the file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Put the file in the web directory for your user account (remember that this is ''~username/.html/'' if you completed the first Module).  Then access the web page by going to ''http://server/~username/info.php''. You will see something similar to the picture below:&lt;br /&gt;
&lt;br /&gt;
[[Image:Php_info.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you look at the HTML code, you will see quite a lot of HTML output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;DTD/xhtml1-transitional.dtd&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&lt;br /&gt;
 body {background-color: #ffffff; color: #000000;}&lt;br /&gt;
 body, td, th, h1, h2 {font-family: sans-serif;}&lt;br /&gt;
 pre {margin: 0px; font-family: monospace;}&lt;br /&gt;
 a:link {color: #000099; text-decoration: none; background-color: #ffffff;}&lt;br /&gt;
 a:hover {text-decoration: underline;}&lt;br /&gt;
 table {border-collapse: collapse;}&lt;br /&gt;
 .center {text-align: center;}&lt;br /&gt;
 .center table { margin-left: auto; margin-right: auto; text-align: left;}&lt;br /&gt;
 .center th { text-align: center !important; }&lt;br /&gt;
 td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}&lt;br /&gt;
 h1 {font-size: 150%;}&lt;br /&gt;
 h2 {font-size: 125%;}&lt;br /&gt;
 .p {text-align: left;}&lt;br /&gt;
 .e {background-color: #ccccff; font-weight: bold; color: #000000;}&lt;br /&gt;
 .h {background-color: #9999cc; font-weight: bold; color: #000000;}&lt;br /&gt;
 .v {background-color: #cccccc; color: #000000;}&lt;br /&gt;
 .vr {background-color: #cccccc; text-align: right; color: #000000;}&lt;br /&gt;
 img {float: right; border: 0px;}&lt;br /&gt;
 hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}&lt;br /&gt;
 &amp;lt;/style&amp;gt;&lt;br /&gt;
 &amp;lt;title&amp;gt;phpinfo()&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
 &amp;lt;body&amp;gt;&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;table border=&amp;quot;0&amp;quot; cellpadding=&amp;quot;3&amp;quot; width=&amp;quot;600&amp;quot;&amp;gt;&lt;br /&gt;
 ....&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, none of that HTML was in the ''info.php'' file.  Instead, the ''phpinfo()'' line is a built-in PHP function that produces all of that HTML.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Variables require ''$'' at the beginning of their names (regardless of the purpose, be it setting, be it accessing). You don't need to specify the type of the variable; PHP will use the variable as the correct type based on how it is used in the code.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   #this is an integer&lt;br /&gt;
   $i=5;&lt;br /&gt;
   #this is a string&lt;br /&gt;
   $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in many other languages, statements end with a '';''.  To actually produce HTML output, you simply print the HTML from the PHP code.  The simplest way is to use the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
If you use a variable inside of a string, the value of the variable will be put in the string (you might also look at the ''echo'' function):&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   $i=5;&lt;br /&gt;
   print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will generate the string&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
If you create a file with just the above code, you will notice that your browser will only receive the above line. That is, there is no ''html'' tag or anything else. Thus, PHP files are normally a mix of HTML code and PHP code.&lt;br /&gt;
Most PHP scripts, then, look more like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code that the browser will receive is then:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  The value of i is 5&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can have any number of PHP segments and HTML segments in a PHP file.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?php $i=5; ?&amp;gt;&lt;br /&gt;
The value of i is &amp;lt;?php  print &amp;quot;$i&amp;quot;; ?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, whatever you print out will be interpreted as HTML by the client's browser, so you can (and will) use HTML tags in the output you produce.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?php print &amp;quot;This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt; ?&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in the following at the user's browser:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which would then render ''test'' in bold.&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
&lt;br /&gt;
Declaring arrays is very easy in PHP. Either you can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
$student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
$student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
$student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or alternatively&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student=array(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you can access the contents with an index into the array, as normal:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find the size of an array with ''count'' function, e.g., ''count($student)''&lt;br /&gt;
&lt;br /&gt;
In PHP, you can also have associative arrays, which are arrays whose members can be accessible with string indices. Here is an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$test[&amp;quot;test1&amp;quot;]=1;&lt;br /&gt;
$test[&amp;quot;test2&amp;quot;]=2;&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;test1=&amp;quot;,$test[test1],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
echo &amp;quot;test2=&amp;quot;,$test[test2],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access array keys through ''array_keys'' function.&lt;br /&gt;
===Strings: Differences Between Java and PHP===&lt;br /&gt;
&lt;br /&gt;
In php, unlike in java, you can enclose strings in single quotes or double quotes&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
The following is a valid string, just like in java.&lt;br /&gt;
 //A valid string in php and in java.&lt;br /&gt;
 $str=&amp;quot;hey jeff!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And in PHP, the following is also a valid string&lt;br /&gt;
 //A valid string in PHP, but invalid in java.&lt;br /&gt;
 $str='hey jeff!' &lt;br /&gt;
&lt;br /&gt;
However, there is a caveat when using the single quotes. If we want to then use MORE single quotes within the string We have to escape them&lt;br /&gt;
To yield the string&lt;br /&gt;
''I'm Jeff's string''&lt;br /&gt;
&lt;br /&gt;
we could use either of the following definitions:&lt;br /&gt;
&lt;br /&gt;
 $str=&amp;quot;I'm Jeff's string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 $str='I\'m Jeff\'s string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if/elseif/else'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   some statements&lt;br /&gt;
 }&lt;br /&gt;
 elseif( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   more statements&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
    yet more statements&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string == &amp;quot;this is my string&amp;quot;) {&lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such thing as global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
PHP has ''for'' and ''while'' loops, similar to Java and C:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, the following code will print the sum of numbers from 1 to 10.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 echo &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The advantage of loops  becomes more obvious when you have to create a long table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Number&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Square&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
for($i=0;$i&amp;lt;100;$i++) {&lt;br /&gt;
echo &amp;quot;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;$i &amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;quot;,$i*$i,&amp;quot;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a table with two columns, each row contains a number and its square. &lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt normal control flow through a loop.  ''break'' jumps out of the loop entirely, while ''continue'' jumps to the end of the current loop iteration and flow resumes with the next loop iteration.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
PHP functions are identified with the keyword ''function'' followed by a function name and argument list for the function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
function name($arg1 [= constant], $arg2 [= constant], ...) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   function add($x,$y) {&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
   }&lt;br /&gt;
   echo &amp;quot;Sum of 2+5=&amp;quot;,add(2,5); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If an argument has a default value, it can be specified with the argument on the list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
      print &amp;quot;hello $name how are you today?&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
   helloMsg(&amp;quot;Alice&amp;quot;);&lt;br /&gt;
   helloMsg();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 hello Alice how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
 hello there how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Passing Variables ==&lt;br /&gt;
Input values can be passed to a script either through the URL request line or through data submitted by a form. Built in arrays $_GET['var'] and $_POST['var'] return the value of ''var'' depending on the access method. As we will see in the next section, values can also be passed through session variables. Finally, you can use cookies to store and retrieve values. &lt;br /&gt;
&lt;br /&gt;
Note that upon submitting a form, the page specified in the form tag's action attribute is loaded (with variables sent via post or get).&lt;br /&gt;
&lt;br /&gt;
===Get: Passing Variables via URL===&lt;br /&gt;
If you want to pass values in a URL, the format is ''http://yourserver/yourphpfile.php?var1=value1&amp;amp;var2=value2.....''&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code would be used to print the previous hello message according to a name given in the URL:&lt;br /&gt;
&lt;br /&gt;
greeting.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $person= $_GET['name'];&lt;br /&gt;
 helloMsg($person);&lt;br /&gt;
 function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
     print &amp;quot;hello $name how are you today?&amp;lt;/br&amp;gt;&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you access the URL like this:  ''greeting.php?name=Alice'', it will show ''hello Alice how are you today?''. However, if you access it with ''greeting.php'', it will print ''hello how are you today?''. Note that it does not use the default value ''there'' since ''$name'' is now a string (in this case, an empty string) and so there is argument being being passed to ''helloMsg''.&lt;br /&gt;
&lt;br /&gt;
===POST: Passing Variables via Form===&lt;br /&gt;
In PHP one method for passing variables from page to page is by sending it to the other page via posting. The way post works in&lt;br /&gt;
PHP is that PHP retrieves the variable names and values from a (slightly modified) html form and sends an those variables in the associative array named $_POST to the page specified in the FORM tag's ''action'' attribute. &lt;br /&gt;
&lt;br /&gt;
Note that receiving the data from a form is very similar to getting it from the URL. Consider the following form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;FORM action=&amp;quot;info.php&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;P&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;firstnamelabel&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;firstname&amp;quot; id=&amp;quot;firstnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;lastnamelabel&amp;quot;&amp;gt;Last name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;lastname&amp;quot; id=&amp;quot;lastnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;birthyearlabel&amp;quot;&amp;gt;Birth Year: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;birthyear&amp;quot; id=&amp;quot;birthyearlabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Male&amp;quot;&amp;gt; Male&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Female&amp;quot;&amp;gt; Female&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;submit&amp;quot; value=&amp;quot;Send&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;reset&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;/P&amp;gt;&lt;br /&gt;
&amp;lt;/FORM&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This posts the form data to ''info.php'', with inputs ''firstname'', ''lastname'', ''birthyear'', and ''gender''.&lt;br /&gt;
The values passed through the form for each input can be accessed in ''info.php'' with the $_POST array.&lt;br /&gt;
*&amp;lt;label&amp;gt; : The label tag just provides a text label for the form inputs.&lt;br /&gt;
*&amp;lt;input ''type=&amp;quot;input_type_here&amp;quot;''&amp;gt; : The input tag provides various means of inputting information into the form, there are various types of inputs such as text, radio, submit, reset, etc. &lt;br /&gt;
*&amp;lt;input ... ''name=&amp;quot;input_name&amp;quot;''&amp;gt; : &lt;br /&gt;
*&amp;lt;label ''for=&amp;quot;identifying_string&amp;quot;''&amp;gt; and &amp;lt;INPUT ... ''id=&amp;quot;identifying_string&amp;quot;'': The optional ''for'' attribute of the label tag associates the label with what we'll call an &amp;quot;identifying string&amp;quot;, which should match the string specified in the corresponding ''id'' portion of the input tag. If both of these attributes are in place, then upon clicking on the label, the corresponding input will be selected.&lt;br /&gt;
&lt;br /&gt;
info.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 echo  &amp;quot;Personal Data:&amp;quot;, $_POST['firstname'], &amp;quot; &amp;quot;, $_POST['lastname'], &amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 $birthyear=$_POST['birthyear'];&lt;br /&gt;
 echo  &amp;quot;Birth year:&amp;quot;, $_birthyear, &amp;quot; (Age:&amp;quot; , 2007-$birthyear, &amp;quot;)&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 echo &amp;quot;Gender: &amp;quot;, $_POST['gender'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For further help with using HTML forms with PHP, we recommend [http://www.tizag.com/phpT/forms.php this tutorial]&lt;br /&gt;
===Self-Submitting Forms===&lt;br /&gt;
Oftentimes we don't want to pass form values to a different page, but rather to the same page as contains the form so that we can process the information and display output on that same page. The way we do this is to specify that the form should submit to $_SERVER['PHP_SELF'] and not say, other_page.php.&lt;br /&gt;
Note that you can make self-submitting forms that use either the get or post method.&lt;br /&gt;
&lt;br /&gt;
The following example simply takes the data input from a form and, via posting, echoes it back to the user as bold text on the same page:&lt;br /&gt;
&lt;br /&gt;
bold_printer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Bold Printer&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action= &amp;lt;?php echo $_SERVER['PHP_SELF'] ?&amp;gt; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;label for=&amp;quot;text_field&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;user_text&amp;quot; id=&amp;quot;text_field&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Print in Bold&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    if(isset($_POST['user_text']))&lt;br /&gt;
    echo &amp;quot;&amp;lt;b&amp;gt;&amp;quot;, $_POST['user_text'], &amp;quot;&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Session Variables==&lt;br /&gt;
PHP provides a global array, ''$_SESSION'', for values that are available globally throughout a ''session''. The values will stay there until either you remove them in a PHP script, the user's browser is closed, or their session expires (you can tune the timeout period in the PHP configuration files).&lt;br /&gt;
&lt;br /&gt;
In order to access session values, in each php file, you should first initiate the session with the ''session_start()'' function call. After that, when you access a value with ''$_SESSION'', it will become a session values. For example:&lt;br /&gt;
&lt;br /&gt;
setsessionvars.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$_SESSION['SESSION']=42;&lt;br /&gt;
$_SESSION['username']=&amp;quot;ArthurDent&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This sets two variables, ''SESSION'' and ''username'' to be accessible by other pages in this session. In another page, you can access them like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
session_start();&lt;br /&gt;
if (!isset($_SESSION['SESSION'])) {&lt;br /&gt;
  echo &amp;quot;error session is not registered&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else echo &amp;quot;username=&amp;quot;,$_SESSION['username'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also remove a session variable from the session with unset command:&lt;br /&gt;
&lt;br /&gt;
 unset($_SESSION['SESSION']);&lt;br /&gt;
&lt;br /&gt;
==Redirect the browser to a different page==&lt;br /&gt;
&lt;br /&gt;
It is often useful to redirect the user's browser to a different webpage, for example, after processing inputs from a form.  Here is a simple example that shows how to do this, based on whether or not the ''LOGIN'' session variable is set:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
if(isset($_SESSION[&amp;quot;LOGIN&amp;quot;])){&lt;br /&gt;
	Header(&amp;quot;Location:/welcome.php&amp;quot;);&lt;br /&gt;
}else{&lt;br /&gt;
	Header(&amp;quot;Location:/login.php&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that anytime you call the ''Header'' function, it is adding information to the header section in the HTML object that is being requested.  As such, all ''Header'' calls should go before any other HTML output in the PHP script.&lt;br /&gt;
&lt;br /&gt;
==Sending a file to the Browser==&lt;br /&gt;
&lt;br /&gt;
Another useful function to know is the ''readfile'' function.  It takes a file on the webserver's file system and sends it to the web browser.  A simple example is given below, but note that you'll probably need to specify addition header fields in order to have the web browser download the file correctly. &lt;br /&gt;
For those of you who have used previous versions of PHP, note that mime_content_type($path) is now deprecated, use finfo_file instead&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$file=$_GET['file'];&lt;br /&gt;
$name = $_SESSION[&amp;quot;UNAME&amp;quot;];&lt;br /&gt;
$path=&amp;quot;/home/uploads/$name/$file&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension&lt;br /&gt;
if (!$finfo) {&lt;br /&gt;
 	echo &amp;quot;Opening fileinfo database failed&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$mime=finfo_file($finfo, $path);&lt;br /&gt;
finfo_close($finfo);&lt;br /&gt;
&lt;br /&gt;
header('content-type:'. $mime);&lt;br /&gt;
readfile($path);    &lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uploading files with PHP==&lt;br /&gt;
&lt;br /&gt;
This section describes one way to upload files with PHP.  First, you need an HTML form that contains an input with type ''file'':&lt;br /&gt;
&lt;br /&gt;
transfer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;uploader.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
Choose a file to upload: &amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you use a ''file'' input in a form, PHP automatically adds an entry in the ''$_FILES'' array with information about the file. For example, transfer.php called the file ''uploadedfile'', so $_FILES['uploadedfile'] refers to file that the user selected in the form.  The name of the original file is $_FILES['uploadedfile']['name'].   When you upload a file, it is put in a temporary location by the web server. This temporary name is $_FILES['uploadedfile']['tmp_name']. In PHP, you can move a file from its original location to a new location by calling a built-in function, ''move_uploaded_file'', which takes an argument for the source (temporary location) and destination location. Note that relative paths in the destination location are relative to the location of the PHP script being executed.&lt;br /&gt;
&lt;br /&gt;
uploader.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$target_path = &amp;quot;uploads/&amp;quot;;&lt;br /&gt;
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);&lt;br /&gt;
&lt;br /&gt;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {&lt;br /&gt;
    echo &amp;quot;The file &amp;quot;.  basename( $_FILES['uploadedfile']['name']) . &amp;quot; has been uploaded&amp;quot;;&lt;br /&gt;
} else{&lt;br /&gt;
    echo &amp;quot;There was an error uploading the file, please try again!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Eclipse ==&lt;br /&gt;
&lt;br /&gt;
For those of you who like programming with IDEs, Eclipse has support for PHP.  See:&lt;br /&gt;
http://www.eclipse.org.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=246</id>
		<title>PHP Original</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=246"/>
		<updated>2010-07-26T15:50:40Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Variables and Arrays */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= PHP5 =&lt;br /&gt;
PHP is a server-side language. When a web request for PHP file comes in, the web server processes the PHP file to produce HTML output.  That is, the main function of most PHP scripts is to dynamically create HTML content.  PHP5 support for Apache is provided by mod_php5. In Ubuntu, you can provide PHP5 support by installing ''php5''. This will install the apache2 module and the php5 command line interpreter (useful for debugging). PHP files may have different extensions, but ''.php'' is the most common.  In the Apache configuration, you can specify which extensions are going to be treated as PHP scripts by adding the extensions to the list in the ''/etc/httpd/conf.d/php.conf'' file: &lt;br /&gt;
&lt;br /&gt;
  AddType text/html .php .phtml .php3&lt;br /&gt;
&lt;br /&gt;
==Declaring PHP==&lt;br /&gt;
If a PHP file is requested by a web user, Apache parses and interprets the file.  Any code declared between PHP tags is then executed and the total HTML output is sent back to the user. PHP code can be declared in a script file in any of the following ways, with the first being the most common:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 php?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script language=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You may see the following shortcut syntax for declaring php, but it may not work unless the shortcut syntax is configured for your server, as such we generally recommend sticking with one of the first ways of declaring php.&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
  PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, edit a new PHP file named ''info.php'', and put these lines in the file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Put the file in the web directory for your user account (remember that this is ''~username/.html/'' if you completed the first Module).  Then access the web page by going to ''http://server/~username/info.php''. You will see something similar to the picture below:&lt;br /&gt;
&lt;br /&gt;
[[Image:Php_info.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you look at the HTML code, you will see quite a lot of HTML output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;DTD/xhtml1-transitional.dtd&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&lt;br /&gt;
 body {background-color: #ffffff; color: #000000;}&lt;br /&gt;
 body, td, th, h1, h2 {font-family: sans-serif;}&lt;br /&gt;
 pre {margin: 0px; font-family: monospace;}&lt;br /&gt;
 a:link {color: #000099; text-decoration: none; background-color: #ffffff;}&lt;br /&gt;
 a:hover {text-decoration: underline;}&lt;br /&gt;
 table {border-collapse: collapse;}&lt;br /&gt;
 .center {text-align: center;}&lt;br /&gt;
 .center table { margin-left: auto; margin-right: auto; text-align: left;}&lt;br /&gt;
 .center th { text-align: center !important; }&lt;br /&gt;
 td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}&lt;br /&gt;
 h1 {font-size: 150%;}&lt;br /&gt;
 h2 {font-size: 125%;}&lt;br /&gt;
 .p {text-align: left;}&lt;br /&gt;
 .e {background-color: #ccccff; font-weight: bold; color: #000000;}&lt;br /&gt;
 .h {background-color: #9999cc; font-weight: bold; color: #000000;}&lt;br /&gt;
 .v {background-color: #cccccc; color: #000000;}&lt;br /&gt;
 .vr {background-color: #cccccc; text-align: right; color: #000000;}&lt;br /&gt;
 img {float: right; border: 0px;}&lt;br /&gt;
 hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}&lt;br /&gt;
 &amp;lt;/style&amp;gt;&lt;br /&gt;
 &amp;lt;title&amp;gt;phpinfo()&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
 &amp;lt;body&amp;gt;&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;table border=&amp;quot;0&amp;quot; cellpadding=&amp;quot;3&amp;quot; width=&amp;quot;600&amp;quot;&amp;gt;&lt;br /&gt;
 ....&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, none of that HTML was in the ''info.php'' file.  Instead, the ''phpinfo()'' line is a built-in PHP function that produces all of that HTML.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Variables require ''$'' at the beginning of their names (regardless of the purpose, be it setting, be it accessing). You don't need to specify the type of the variable; PHP will use the variable as the correct type based on how it is used in the code.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   #this is an integer&lt;br /&gt;
   $i=5;&lt;br /&gt;
   #this is a string&lt;br /&gt;
   $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in many other languages, statements end with a '';''.  To actually produce HTML output, you simply print the HTML from the PHP code.  The simplest way is to use the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
If you use a variable inside of a string, the value of the variable will be put in the string (you might also look at the ''echo'' function):&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   $i=5;&lt;br /&gt;
   print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will generate the string&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
If you create a file with just the above code, you will notice that your browser will only receive the above line. That is, there is no ''html'' tag or anything else. Thus, PHP files are normally a mix of HTML code and PHP code.&lt;br /&gt;
Most PHP scripts, then, look more like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code that the browser will receive is then:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  The value of i is 5&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can have any number of PHP segments and HTML segments in a PHP file.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?php $i=5; ?&amp;gt;&lt;br /&gt;
The value of i is &amp;lt;?php  print &amp;quot;$i&amp;quot;; ?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, whatever you print out will be interpreted as HTML by the client's browser, so you can (and will) use HTML tags in the output you produce.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?php print &amp;quot;This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt; ?&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in the following at the user's browser:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which would then render ''test'' in bold.&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
&lt;br /&gt;
Declaring arrays is very easy in PHP. Either you can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
$student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
$student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
$student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or alternatively&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student=array(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you can access the contents with an index into the array, as normal:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find the size of an array with ''count'' function, e.g., ''count($student)''&lt;br /&gt;
&lt;br /&gt;
In PHP, you can also have associative arrays, which are arrays whose members can be accessible with string indices. Here is an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$test[&amp;quot;test1&amp;quot;]=1;&lt;br /&gt;
$test[&amp;quot;test2&amp;quot;]=2;&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;test1=&amp;quot;,$test[test1],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
echo &amp;quot;test2=&amp;quot;,$test[test2],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access array keys through ''array_keys'' function.&lt;br /&gt;
===Strings: Differences Between Java and PHP===&lt;br /&gt;
&lt;br /&gt;
In php, unlike in java, you can enclose strings in single quotes or double quotes&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
The following is a valid string, just like in java.&lt;br /&gt;
 //A valid string in php and in java.&lt;br /&gt;
 $str=&amp;quot;hey jeff!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And in PHP, the following is also a valid string&lt;br /&gt;
 //A valid string in PHP, but invalid in java.&lt;br /&gt;
 $str='hey jeff!' &lt;br /&gt;
&lt;br /&gt;
However, there is a caveat when using the single quotes. If we want to then use MORE single quotes within the string We have to escape them&lt;br /&gt;
To yield the string&lt;br /&gt;
''I'm Jeff's string''&lt;br /&gt;
&lt;br /&gt;
we could use either of the following definitions:&lt;br /&gt;
&lt;br /&gt;
 $str=&amp;quot;I'm Jeff's string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 $str='I\'m Jeff\'s string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if/elseif/else'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   some statements&lt;br /&gt;
 }&lt;br /&gt;
 elseif( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   more statements&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
    yet more statements&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string == &amp;quot;this is my string&amp;quot;) {&lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such thing as global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
PHP has ''for'' and ''while'' loops, similar to Java and C:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, the following code will print the sum of numbers from 1 to 10.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 echo &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The advantage of loops  becomes more obvious when you have to create a long table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Number&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Square&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
for($i=0;$i&amp;lt;100;$i++) {&lt;br /&gt;
echo &amp;quot;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;$i &amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;quot;,$i*$i,&amp;quot;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a table with two columns, each row contains a number and its square. &lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt normal control flow through a loop.  ''break'' jumps out of the loop entirely, while ''continue'' jumps to the end of the current loop iteration and flow resumes with the next loop iteration.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
PHP functions are identified with the keyword ''function'' followed by a function name and argument list for the function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
function name($arg1 [= constant], $arg2 [= constant], ...) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   function add($x,$y) {&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
   }&lt;br /&gt;
   echo &amp;quot;Sum of 2+5=&amp;quot;,add(2,5); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If an argument has a default value, it can be specified with the argument on the list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
      print &amp;quot;hello $name how are you today?&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
   helloMsg(&amp;quot;Alice&amp;quot;);&lt;br /&gt;
   helloMsg();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 hello Alice how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
 hello there how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Passing Variables ==&lt;br /&gt;
Input values can be passed to a script either through the URL request line or through data submitted by a form. Built in arrays $_GET['var'] and $_POST['var'] return the value of ''var'' depending on the access method. As we will see in the next section, values can also be passed through session variables. Finally, you can use cookies to store and retrieve values. &lt;br /&gt;
&lt;br /&gt;
Note that upon submitting a form, the page specified in the form tag's action attribute is loaded (with variables sent via post or get).&lt;br /&gt;
&lt;br /&gt;
===Get: Passing Variables via URL===&lt;br /&gt;
If you want to pass values in a URL, the format is ''http://yourserver/yourphpfile.php?var1=value1&amp;amp;var2=value2.....''&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code would be used to print the previous hello message according to a name given in the URL:&lt;br /&gt;
&lt;br /&gt;
greeting.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $person= $_GET['name'];&lt;br /&gt;
 helloMsg($person);&lt;br /&gt;
 function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
     print &amp;quot;hello $name how are you today?&amp;lt;/br&amp;gt;&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you access the URL like this:  ''greeting.php?name=Alice'', it will show ''hello Alice how are you today?''. However, if you access it with ''greeting.php'', it will print ''hello how are you today?''. Note that it does not use the default value ''there'' since ''$name'' is now a string (in this case, an empty string) and so there is argument being being passed to ''helloMsg''.&lt;br /&gt;
&lt;br /&gt;
===POST: Passing Variables via Form===&lt;br /&gt;
In PHP one method for passing variables from page to page is by sending it to the other page via posting. The way post works in&lt;br /&gt;
PHP is that PHP retrieves the variable names and values from a (slightly modified) html form and sends an those variables in the associative array named $_POST to the page specified in the FORM tag's ''action'' attribute. &lt;br /&gt;
&lt;br /&gt;
Note that receiving the data from a form is very similar to getting it from the URL. Consider the following form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;FORM action=&amp;quot;info.php&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;P&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;firstnamelabel&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;firstname&amp;quot; id=&amp;quot;firstnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;lastnamelabel&amp;quot;&amp;gt;Last name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;lastname&amp;quot; id=&amp;quot;lastnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;birthyearlabel&amp;quot;&amp;gt;Birth Year: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;birthyear&amp;quot; id=&amp;quot;birthyearlabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Male&amp;quot;&amp;gt; Male&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Female&amp;quot;&amp;gt; Female&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;submit&amp;quot; value=&amp;quot;Send&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;reset&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;/P&amp;gt;&lt;br /&gt;
&amp;lt;/FORM&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This posts the form data to ''info.php'', with inputs ''firstname'', ''lastname'', ''birthyear'', and ''gender''.&lt;br /&gt;
The values passed through the form for each input can be accessed in ''info.php'' with the $_POST array.&lt;br /&gt;
*&amp;lt;label&amp;gt; : The label tag just provides a text label for the form inputs.&lt;br /&gt;
*&amp;lt;input ''type=&amp;quot;input_type_here&amp;quot;''&amp;gt; : The input tag provides various means of inputting information into the form, there are various types of inputs such as text, radio, submit, reset, etc. &lt;br /&gt;
*&amp;lt;input ... ''name=&amp;quot;input_name&amp;quot;''&amp;gt; : &lt;br /&gt;
*&amp;lt;label ''for=&amp;quot;identifying_string&amp;quot;''&amp;gt; and &amp;lt;INPUT ... ''id=&amp;quot;identifying_string&amp;quot;'': The optional ''for'' attribute of the label tag associates the label with what we'll call an &amp;quot;identifying string&amp;quot;, which should match the string specified in the corresponding ''id'' portion of the input tag. If both of these attributes are in place, then upon clicking on the label, the corresponding input will be selected.&lt;br /&gt;
&lt;br /&gt;
info.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 echo  &amp;quot;Personal Data:&amp;quot;, $_POST['firstname'], &amp;quot; &amp;quot;, $_POST['lastname'], &amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 $birthyear=$_POST['birthyear'];&lt;br /&gt;
 echo  &amp;quot;Birth year:&amp;quot;, $_birthyear, &amp;quot; (Age:&amp;quot; , 2007-$birthyear, &amp;quot;)&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 echo &amp;quot;Gender: &amp;quot;, $_POST['gender'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For further help with using HTML forms with PHP, we recommend [http://www.tizag.com/phpT/forms.php this tutorial]&lt;br /&gt;
===Self-Submitting Forms===&lt;br /&gt;
Oftentimes we don't want to pass form values to a different page, but rather to the same page as contains the form so that we can process the information and display output on that same page. The way we do this is to specify that the form should submit to $_SERVER['PHP_SELF'] and not say, other_page.php.&lt;br /&gt;
Note that you can make self-submitting forms that use either the get or post method.&lt;br /&gt;
&lt;br /&gt;
The following example simply takes the data input from a form and, via posting, echoes it back to the user as bold text on the same page:&lt;br /&gt;
&lt;br /&gt;
bold_printer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Bold Printer&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action= &amp;lt;?php echo $_SERVER['PHP_SELF'] ?&amp;gt; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;label for=&amp;quot;text_field&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;user_text&amp;quot; id=&amp;quot;text_field&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Print in Bold&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    if(isset($_POST['user_text']))&lt;br /&gt;
    echo &amp;quot;&amp;lt;b&amp;gt;&amp;quot;, $_POST['user_text'], &amp;quot;&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Session Variables==&lt;br /&gt;
PHP provides a global array, ''$_SESSION'', for values that are available globally throughout a ''session''. The values will stay there until either you remove them in a PHP script, the user's browser is closed, or their session expires (you can tune the timeout period in the PHP configuration files).&lt;br /&gt;
&lt;br /&gt;
In order to access session values, in each php file, you should first initiate the session with the ''session_start()'' function call. After that, when you access a value with ''$_SESSION'', it will become a session values. For example:&lt;br /&gt;
&lt;br /&gt;
setsessionvars.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$_SESSION['SESSION']=42;&lt;br /&gt;
$_SESSION['username']=&amp;quot;ArthurDent&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This sets two variables, ''SESSION'' and ''username'' to be accessible by other pages in this session. In another page, you can access them like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
if (!isset($_SESSION['SESSION'])) {&lt;br /&gt;
  echo &amp;quot;error session is not registered&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else echo &amp;quot;username=&amp;quot;,$_SESSION['username'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also remove a session variable from the session with unset command:&lt;br /&gt;
&lt;br /&gt;
 unset($_SESSION['SESSION']);&lt;br /&gt;
&lt;br /&gt;
==Redirect the browser to a different page==&lt;br /&gt;
&lt;br /&gt;
It is often useful to redirect the user's browser to a different webpage, for example, after processing inputs from a form.  Here is a simple example that shows how to do this, based on whether or not the ''LOGIN'' session variable is set:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
if(isset($_SESSION[&amp;quot;LOGIN&amp;quot;])){&lt;br /&gt;
	Header(&amp;quot;Location:/welcome.php&amp;quot;);&lt;br /&gt;
}else{&lt;br /&gt;
	Header(&amp;quot;Location:/login.php&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that anytime you call the ''Header'' function, it is adding information to the header section in the HTML object that is being requested.  As such, all ''Header'' calls should go before any other HTML output in the PHP script.&lt;br /&gt;
&lt;br /&gt;
==Sending a file to the Browser==&lt;br /&gt;
&lt;br /&gt;
Another useful function to know is the ''readfile'' function.  It takes a file on the webserver's file system and sends it to the web browser.  A simple example is given below, but note that you'll probably need to specify addition header fields in order to have the web browser download the file correctly. &lt;br /&gt;
For those of you who have used previous versions of PHP, note that mime_content_type($path) is now deprecated, use finfo_file instead&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$file=$_GET['file'];&lt;br /&gt;
$name = $_SESSION[&amp;quot;UNAME&amp;quot;];&lt;br /&gt;
$path=&amp;quot;/home/uploads/$name/$file&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension&lt;br /&gt;
if (!$finfo) {&lt;br /&gt;
 	echo &amp;quot;Opening fileinfo database failed&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$mime=finfo_file($finfo, $path);&lt;br /&gt;
finfo_close($finfo);&lt;br /&gt;
&lt;br /&gt;
header('content-type:'. $mime);&lt;br /&gt;
readfile($path);    &lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uploading files with PHP==&lt;br /&gt;
&lt;br /&gt;
This section describes one way to upload files with PHP.  First, you need an HTML form that contains an input with type ''file'':&lt;br /&gt;
&lt;br /&gt;
transfer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;uploader.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
Choose a file to upload: &amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you use a ''file'' input in a form, PHP automatically adds an entry in the ''$_FILES'' array with information about the file. For example, transfer.php called the file ''uploadedfile'', so $_FILES['uploadedfile'] refers to file that the user selected in the form.  The name of the original file is $_FILES['uploadedfile']['name'].   When you upload a file, it is put in a temporary location by the web server. This temporary name is $_FILES['uploadedfile']['tmp_name']. In PHP, you can move a file from its original location to a new location by calling a built-in function, ''move_uploaded_file'', which takes an argument for the source (temporary location) and destination location. Note that relative paths in the destination location are relative to the location of the PHP script being executed.&lt;br /&gt;
&lt;br /&gt;
uploader.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$target_path = &amp;quot;uploads/&amp;quot;;&lt;br /&gt;
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);&lt;br /&gt;
&lt;br /&gt;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {&lt;br /&gt;
    echo &amp;quot;The file &amp;quot;.  basename( $_FILES['uploadedfile']['name']) . &amp;quot; has been uploaded&amp;quot;;&lt;br /&gt;
} else{&lt;br /&gt;
    echo &amp;quot;There was an error uploading the file, please try again!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Eclipse ==&lt;br /&gt;
&lt;br /&gt;
For those of you who like programming with IDEs, Eclipse has support for PHP.  See:&lt;br /&gt;
http://www.eclipse.org.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=245</id>
		<title>PHP Original</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=245"/>
		<updated>2010-07-26T15:49:35Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Declaring PHP */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= PHP5 =&lt;br /&gt;
PHP is a server-side language. When a web request for PHP file comes in, the web server processes the PHP file to produce HTML output.  That is, the main function of most PHP scripts is to dynamically create HTML content.  PHP5 support for Apache is provided by mod_php5. In Ubuntu, you can provide PHP5 support by installing ''php5''. This will install the apache2 module and the php5 command line interpreter (useful for debugging). PHP files may have different extensions, but ''.php'' is the most common.  In the Apache configuration, you can specify which extensions are going to be treated as PHP scripts by adding the extensions to the list in the ''/etc/httpd/conf.d/php.conf'' file: &lt;br /&gt;
&lt;br /&gt;
  AddType text/html .php .phtml .php3&lt;br /&gt;
&lt;br /&gt;
==Declaring PHP==&lt;br /&gt;
If a PHP file is requested by a web user, Apache parses and interprets the file.  Any code declared between PHP tags is then executed and the total HTML output is sent back to the user. PHP code can be declared in a script file in any of the following ways, with the first being the most common:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 php?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script language=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You may see the following shortcut syntax for declaring php, but it may not work unless the shortcut syntax is configured for your server, as such we generally recommend sticking with one of the first ways of declaring php.&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
  PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, edit a new PHP file named ''info.php'', and put these lines in the file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Put the file in the web directory for your user account (remember that this is ''~username/.html/'' if you completed the first Module).  Then access the web page by going to ''http://server/~username/info.php''. You will see something similar to the picture below:&lt;br /&gt;
&lt;br /&gt;
[[Image:Php_info.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you look at the HTML code, you will see quite a lot of HTML output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;DTD/xhtml1-transitional.dtd&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&lt;br /&gt;
 body {background-color: #ffffff; color: #000000;}&lt;br /&gt;
 body, td, th, h1, h2 {font-family: sans-serif;}&lt;br /&gt;
 pre {margin: 0px; font-family: monospace;}&lt;br /&gt;
 a:link {color: #000099; text-decoration: none; background-color: #ffffff;}&lt;br /&gt;
 a:hover {text-decoration: underline;}&lt;br /&gt;
 table {border-collapse: collapse;}&lt;br /&gt;
 .center {text-align: center;}&lt;br /&gt;
 .center table { margin-left: auto; margin-right: auto; text-align: left;}&lt;br /&gt;
 .center th { text-align: center !important; }&lt;br /&gt;
 td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}&lt;br /&gt;
 h1 {font-size: 150%;}&lt;br /&gt;
 h2 {font-size: 125%;}&lt;br /&gt;
 .p {text-align: left;}&lt;br /&gt;
 .e {background-color: #ccccff; font-weight: bold; color: #000000;}&lt;br /&gt;
 .h {background-color: #9999cc; font-weight: bold; color: #000000;}&lt;br /&gt;
 .v {background-color: #cccccc; color: #000000;}&lt;br /&gt;
 .vr {background-color: #cccccc; text-align: right; color: #000000;}&lt;br /&gt;
 img {float: right; border: 0px;}&lt;br /&gt;
 hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}&lt;br /&gt;
 &amp;lt;/style&amp;gt;&lt;br /&gt;
 &amp;lt;title&amp;gt;phpinfo()&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
 &amp;lt;body&amp;gt;&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;table border=&amp;quot;0&amp;quot; cellpadding=&amp;quot;3&amp;quot; width=&amp;quot;600&amp;quot;&amp;gt;&lt;br /&gt;
 ....&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, none of that HTML was in the ''info.php'' file.  Instead, the ''phpinfo()'' line is a built-in PHP function that produces all of that HTML.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Variables require ''$'' at the beginning of their names (regardless of the purpose, be it setting, be it accessing). You don't need to specify the type of the variable; PHP will use the variable as the correct type based on how it is used in the code.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   #this is an integer&lt;br /&gt;
   $i=5;&lt;br /&gt;
   #this is a string&lt;br /&gt;
   $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in many other languages, statements end with a '';''.  To actually produce HTML output, you simply print the HTML from the PHP code.  The simplest way is to use the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
If you use a variable inside of a string, the value of the variable will be put in the string (you might also look at the ''echo'' function):&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   $i=5;&lt;br /&gt;
   print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will generate the string&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
If you create a file with just the above code, you will notice that your browser will only receive the above line. That is, there is no ''html'' tag or anything else. Thus, PHP files are normally a mix of HTML code and PHP code.&lt;br /&gt;
Most PHP scripts, then, look more like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code that the browser will receive is then:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  The value of i is 5&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can have any number of PHP segments and HTML segments in a PHP file.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;? $i=5; ?&amp;gt;&lt;br /&gt;
The value of i is &amp;lt;?  print &amp;quot;$i&amp;quot;; ?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, whatever you print out will be interpreted as HTML by the client's browser, so you can (and will) use HTML tags in the output you produce.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;? print &amp;quot;This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt; ?&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in the following at the user's browser:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which would then render ''test'' in bold.&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
&lt;br /&gt;
Declaring arrays is very easy in PHP. Either you can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
$student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
$student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
$student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or alternatively&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student=array(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you can access the contents with an index into the array, as normal:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find the size of an array with ''count'' function, e.g., ''count($student)''&lt;br /&gt;
&lt;br /&gt;
In PHP, you can also have associative arrays, which are arrays whose members can be accessible with string indices. Here is an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
$test[&amp;quot;test1&amp;quot;]=1;&lt;br /&gt;
$test[&amp;quot;test2&amp;quot;]=2;&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;test1=&amp;quot;,$test[test1],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
echo &amp;quot;test2=&amp;quot;,$test[test2],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access array keys through ''array_keys'' function.&lt;br /&gt;
===Strings: Differences Between Java and PHP===&lt;br /&gt;
&lt;br /&gt;
In php, unlike in java, you can enclose strings in single quotes or double quotes&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
The following is a valid string, just like in java.&lt;br /&gt;
 //A valid string in php and in java.&lt;br /&gt;
 $str=&amp;quot;hey jeff!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And in PHP, the following is also a valid string&lt;br /&gt;
 //A valid string in PHP, but invalid in java.&lt;br /&gt;
 $str='hey jeff!' &lt;br /&gt;
&lt;br /&gt;
However, there is a caveat when using the single quotes. If we want to then use MORE single quotes within the string We have to escape them&lt;br /&gt;
To yield the string&lt;br /&gt;
''I'm Jeff's string''&lt;br /&gt;
&lt;br /&gt;
we could use either of the following definitions:&lt;br /&gt;
&lt;br /&gt;
 $str=&amp;quot;I'm Jeff's string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 $str='I\'m Jeff\'s string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if/elseif/else'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   some statements&lt;br /&gt;
 }&lt;br /&gt;
 elseif( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   more statements&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
    yet more statements&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string == &amp;quot;this is my string&amp;quot;) {&lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such thing as global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
PHP has ''for'' and ''while'' loops, similar to Java and C:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, the following code will print the sum of numbers from 1 to 10.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 echo &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The advantage of loops  becomes more obvious when you have to create a long table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Number&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Square&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
for($i=0;$i&amp;lt;100;$i++) {&lt;br /&gt;
echo &amp;quot;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;$i &amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;quot;,$i*$i,&amp;quot;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a table with two columns, each row contains a number and its square. &lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt normal control flow through a loop.  ''break'' jumps out of the loop entirely, while ''continue'' jumps to the end of the current loop iteration and flow resumes with the next loop iteration.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
PHP functions are identified with the keyword ''function'' followed by a function name and argument list for the function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
function name($arg1 [= constant], $arg2 [= constant], ...) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   function add($x,$y) {&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
   }&lt;br /&gt;
   echo &amp;quot;Sum of 2+5=&amp;quot;,add(2,5); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If an argument has a default value, it can be specified with the argument on the list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
      print &amp;quot;hello $name how are you today?&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
   helloMsg(&amp;quot;Alice&amp;quot;);&lt;br /&gt;
   helloMsg();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 hello Alice how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
 hello there how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Passing Variables ==&lt;br /&gt;
Input values can be passed to a script either through the URL request line or through data submitted by a form. Built in arrays $_GET['var'] and $_POST['var'] return the value of ''var'' depending on the access method. As we will see in the next section, values can also be passed through session variables. Finally, you can use cookies to store and retrieve values. &lt;br /&gt;
&lt;br /&gt;
Note that upon submitting a form, the page specified in the form tag's action attribute is loaded (with variables sent via post or get).&lt;br /&gt;
&lt;br /&gt;
===Get: Passing Variables via URL===&lt;br /&gt;
If you want to pass values in a URL, the format is ''http://yourserver/yourphpfile.php?var1=value1&amp;amp;var2=value2.....''&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code would be used to print the previous hello message according to a name given in the URL:&lt;br /&gt;
&lt;br /&gt;
greeting.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $person= $_GET['name'];&lt;br /&gt;
 helloMsg($person);&lt;br /&gt;
 function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
     print &amp;quot;hello $name how are you today?&amp;lt;/br&amp;gt;&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you access the URL like this:  ''greeting.php?name=Alice'', it will show ''hello Alice how are you today?''. However, if you access it with ''greeting.php'', it will print ''hello how are you today?''. Note that it does not use the default value ''there'' since ''$name'' is now a string (in this case, an empty string) and so there is argument being being passed to ''helloMsg''.&lt;br /&gt;
&lt;br /&gt;
===POST: Passing Variables via Form===&lt;br /&gt;
In PHP one method for passing variables from page to page is by sending it to the other page via posting. The way post works in&lt;br /&gt;
PHP is that PHP retrieves the variable names and values from a (slightly modified) html form and sends an those variables in the associative array named $_POST to the page specified in the FORM tag's ''action'' attribute. &lt;br /&gt;
&lt;br /&gt;
Note that receiving the data from a form is very similar to getting it from the URL. Consider the following form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;FORM action=&amp;quot;info.php&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;P&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;firstnamelabel&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;firstname&amp;quot; id=&amp;quot;firstnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;lastnamelabel&amp;quot;&amp;gt;Last name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;lastname&amp;quot; id=&amp;quot;lastnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;birthyearlabel&amp;quot;&amp;gt;Birth Year: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;birthyear&amp;quot; id=&amp;quot;birthyearlabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Male&amp;quot;&amp;gt; Male&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Female&amp;quot;&amp;gt; Female&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;submit&amp;quot; value=&amp;quot;Send&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;reset&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;/P&amp;gt;&lt;br /&gt;
&amp;lt;/FORM&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This posts the form data to ''info.php'', with inputs ''firstname'', ''lastname'', ''birthyear'', and ''gender''.&lt;br /&gt;
The values passed through the form for each input can be accessed in ''info.php'' with the $_POST array.&lt;br /&gt;
*&amp;lt;label&amp;gt; : The label tag just provides a text label for the form inputs.&lt;br /&gt;
*&amp;lt;input ''type=&amp;quot;input_type_here&amp;quot;''&amp;gt; : The input tag provides various means of inputting information into the form, there are various types of inputs such as text, radio, submit, reset, etc. &lt;br /&gt;
*&amp;lt;input ... ''name=&amp;quot;input_name&amp;quot;''&amp;gt; : &lt;br /&gt;
*&amp;lt;label ''for=&amp;quot;identifying_string&amp;quot;''&amp;gt; and &amp;lt;INPUT ... ''id=&amp;quot;identifying_string&amp;quot;'': The optional ''for'' attribute of the label tag associates the label with what we'll call an &amp;quot;identifying string&amp;quot;, which should match the string specified in the corresponding ''id'' portion of the input tag. If both of these attributes are in place, then upon clicking on the label, the corresponding input will be selected.&lt;br /&gt;
&lt;br /&gt;
info.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 echo  &amp;quot;Personal Data:&amp;quot;, $_POST['firstname'], &amp;quot; &amp;quot;, $_POST['lastname'], &amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 $birthyear=$_POST['birthyear'];&lt;br /&gt;
 echo  &amp;quot;Birth year:&amp;quot;, $_birthyear, &amp;quot; (Age:&amp;quot; , 2007-$birthyear, &amp;quot;)&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 echo &amp;quot;Gender: &amp;quot;, $_POST['gender'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For further help with using HTML forms with PHP, we recommend [http://www.tizag.com/phpT/forms.php this tutorial]&lt;br /&gt;
===Self-Submitting Forms===&lt;br /&gt;
Oftentimes we don't want to pass form values to a different page, but rather to the same page as contains the form so that we can process the information and display output on that same page. The way we do this is to specify that the form should submit to $_SERVER['PHP_SELF'] and not say, other_page.php.&lt;br /&gt;
Note that you can make self-submitting forms that use either the get or post method.&lt;br /&gt;
&lt;br /&gt;
The following example simply takes the data input from a form and, via posting, echoes it back to the user as bold text on the same page:&lt;br /&gt;
&lt;br /&gt;
bold_printer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Bold Printer&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action= &amp;lt;?php echo $_SERVER['PHP_SELF'] ?&amp;gt; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;label for=&amp;quot;text_field&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;user_text&amp;quot; id=&amp;quot;text_field&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Print in Bold&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    if(isset($_POST['user_text']))&lt;br /&gt;
    echo &amp;quot;&amp;lt;b&amp;gt;&amp;quot;, $_POST['user_text'], &amp;quot;&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Session Variables==&lt;br /&gt;
PHP provides a global array, ''$_SESSION'', for values that are available globally throughout a ''session''. The values will stay there until either you remove them in a PHP script, the user's browser is closed, or their session expires (you can tune the timeout period in the PHP configuration files).&lt;br /&gt;
&lt;br /&gt;
In order to access session values, in each php file, you should first initiate the session with the ''session_start()'' function call. After that, when you access a value with ''$_SESSION'', it will become a session values. For example:&lt;br /&gt;
&lt;br /&gt;
setsessionvars.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$_SESSION['SESSION']=42;&lt;br /&gt;
$_SESSION['username']=&amp;quot;ArthurDent&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This sets two variables, ''SESSION'' and ''username'' to be accessible by other pages in this session. In another page, you can access them like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
if (!isset($_SESSION['SESSION'])) {&lt;br /&gt;
  echo &amp;quot;error session is not registered&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else echo &amp;quot;username=&amp;quot;,$_SESSION['username'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also remove a session variable from the session with unset command:&lt;br /&gt;
&lt;br /&gt;
 unset($_SESSION['SESSION']);&lt;br /&gt;
&lt;br /&gt;
==Redirect the browser to a different page==&lt;br /&gt;
&lt;br /&gt;
It is often useful to redirect the user's browser to a different webpage, for example, after processing inputs from a form.  Here is a simple example that shows how to do this, based on whether or not the ''LOGIN'' session variable is set:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
if(isset($_SESSION[&amp;quot;LOGIN&amp;quot;])){&lt;br /&gt;
	Header(&amp;quot;Location:/welcome.php&amp;quot;);&lt;br /&gt;
}else{&lt;br /&gt;
	Header(&amp;quot;Location:/login.php&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that anytime you call the ''Header'' function, it is adding information to the header section in the HTML object that is being requested.  As such, all ''Header'' calls should go before any other HTML output in the PHP script.&lt;br /&gt;
&lt;br /&gt;
==Sending a file to the Browser==&lt;br /&gt;
&lt;br /&gt;
Another useful function to know is the ''readfile'' function.  It takes a file on the webserver's file system and sends it to the web browser.  A simple example is given below, but note that you'll probably need to specify addition header fields in order to have the web browser download the file correctly. &lt;br /&gt;
For those of you who have used previous versions of PHP, note that mime_content_type($path) is now deprecated, use finfo_file instead&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$file=$_GET['file'];&lt;br /&gt;
$name = $_SESSION[&amp;quot;UNAME&amp;quot;];&lt;br /&gt;
$path=&amp;quot;/home/uploads/$name/$file&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension&lt;br /&gt;
if (!$finfo) {&lt;br /&gt;
 	echo &amp;quot;Opening fileinfo database failed&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$mime=finfo_file($finfo, $path);&lt;br /&gt;
finfo_close($finfo);&lt;br /&gt;
&lt;br /&gt;
header('content-type:'. $mime);&lt;br /&gt;
readfile($path);    &lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uploading files with PHP==&lt;br /&gt;
&lt;br /&gt;
This section describes one way to upload files with PHP.  First, you need an HTML form that contains an input with type ''file'':&lt;br /&gt;
&lt;br /&gt;
transfer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;uploader.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
Choose a file to upload: &amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you use a ''file'' input in a form, PHP automatically adds an entry in the ''$_FILES'' array with information about the file. For example, transfer.php called the file ''uploadedfile'', so $_FILES['uploadedfile'] refers to file that the user selected in the form.  The name of the original file is $_FILES['uploadedfile']['name'].   When you upload a file, it is put in a temporary location by the web server. This temporary name is $_FILES['uploadedfile']['tmp_name']. In PHP, you can move a file from its original location to a new location by calling a built-in function, ''move_uploaded_file'', which takes an argument for the source (temporary location) and destination location. Note that relative paths in the destination location are relative to the location of the PHP script being executed.&lt;br /&gt;
&lt;br /&gt;
uploader.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$target_path = &amp;quot;uploads/&amp;quot;;&lt;br /&gt;
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);&lt;br /&gt;
&lt;br /&gt;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {&lt;br /&gt;
    echo &amp;quot;The file &amp;quot;.  basename( $_FILES['uploadedfile']['name']) . &amp;quot; has been uploaded&amp;quot;;&lt;br /&gt;
} else{&lt;br /&gt;
    echo &amp;quot;There was an error uploading the file, please try again!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Eclipse ==&lt;br /&gt;
&lt;br /&gt;
For those of you who like programming with IDEs, Eclipse has support for PHP.  See:&lt;br /&gt;
http://www.eclipse.org.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=244</id>
		<title>PHP Original</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=244"/>
		<updated>2010-07-23T20:17:15Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Session Variables */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= PHP5 =&lt;br /&gt;
PHP is a server-side language. When a web request for PHP file comes in, the web server processes the PHP file to produce HTML output.  That is, the main function of most PHP scripts is to dynamically create HTML content.  PHP5 support for Apache is provided by mod_php5. In Ubuntu, you can provide PHP5 support by installing ''php5''. This will install the apache2 module and the php5 command line interpreter (useful for debugging). PHP files may have different extensions, but ''.php'' is the most common.  In the Apache configuration, you can specify which extensions are going to be treated as PHP scripts by adding the extensions to the list in the ''/etc/httpd/conf.d/php.conf'' file: &lt;br /&gt;
&lt;br /&gt;
  AddType text/html .php .phtml .php3&lt;br /&gt;
&lt;br /&gt;
==Declaring PHP==&lt;br /&gt;
If a PHP file is requested by a web user, Apache parses and interprets the file.  Any code declared between PHP tags is then executed and the total HTML output is sent back to the user. PHP code can be declared in a script file in any of the following ways, with the first being the most common:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 php?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script language=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, edit a new PHP file named ''info.php'', and put these lines in the file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Put the file in the web directory for your user account (remember that this is ''~username/.html/'' if you completed the first Module).  Then access the web page by going to ''http://server/~username/info.php''. You will see something similar to the picture below:&lt;br /&gt;
&lt;br /&gt;
[[Image:Php_info.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you look at the HTML code, you will see quite a lot of HTML output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;DTD/xhtml1-transitional.dtd&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&lt;br /&gt;
 body {background-color: #ffffff; color: #000000;}&lt;br /&gt;
 body, td, th, h1, h2 {font-family: sans-serif;}&lt;br /&gt;
 pre {margin: 0px; font-family: monospace;}&lt;br /&gt;
 a:link {color: #000099; text-decoration: none; background-color: #ffffff;}&lt;br /&gt;
 a:hover {text-decoration: underline;}&lt;br /&gt;
 table {border-collapse: collapse;}&lt;br /&gt;
 .center {text-align: center;}&lt;br /&gt;
 .center table { margin-left: auto; margin-right: auto; text-align: left;}&lt;br /&gt;
 .center th { text-align: center !important; }&lt;br /&gt;
 td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}&lt;br /&gt;
 h1 {font-size: 150%;}&lt;br /&gt;
 h2 {font-size: 125%;}&lt;br /&gt;
 .p {text-align: left;}&lt;br /&gt;
 .e {background-color: #ccccff; font-weight: bold; color: #000000;}&lt;br /&gt;
 .h {background-color: #9999cc; font-weight: bold; color: #000000;}&lt;br /&gt;
 .v {background-color: #cccccc; color: #000000;}&lt;br /&gt;
 .vr {background-color: #cccccc; text-align: right; color: #000000;}&lt;br /&gt;
 img {float: right; border: 0px;}&lt;br /&gt;
 hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}&lt;br /&gt;
 &amp;lt;/style&amp;gt;&lt;br /&gt;
 &amp;lt;title&amp;gt;phpinfo()&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
 &amp;lt;body&amp;gt;&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;table border=&amp;quot;0&amp;quot; cellpadding=&amp;quot;3&amp;quot; width=&amp;quot;600&amp;quot;&amp;gt;&lt;br /&gt;
 ....&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, none of that HTML was in the ''info.php'' file.  Instead, the ''phpinfo()'' line is a built-in PHP function that produces all of that HTML.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Variables require ''$'' at the beginning of their names (regardless of the purpose, be it setting, be it accessing). You don't need to specify the type of the variable; PHP will use the variable as the correct type based on how it is used in the code.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   #this is an integer&lt;br /&gt;
   $i=5;&lt;br /&gt;
   #this is a string&lt;br /&gt;
   $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in many other languages, statements end with a '';''.  To actually produce HTML output, you simply print the HTML from the PHP code.  The simplest way is to use the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
If you use a variable inside of a string, the value of the variable will be put in the string (you might also look at the ''echo'' function):&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   $i=5;&lt;br /&gt;
   print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will generate the string&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
If you create a file with just the above code, you will notice that your browser will only receive the above line. That is, there is no ''html'' tag or anything else. Thus, PHP files are normally a mix of HTML code and PHP code.&lt;br /&gt;
Most PHP scripts, then, look more like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code that the browser will receive is then:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  The value of i is 5&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can have any number of PHP segments and HTML segments in a PHP file.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;? $i=5; ?&amp;gt;&lt;br /&gt;
The value of i is &amp;lt;?  print &amp;quot;$i&amp;quot;; ?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, whatever you print out will be interpreted as HTML by the client's browser, so you can (and will) use HTML tags in the output you produce.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;? print &amp;quot;This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt; ?&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in the following at the user's browser:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which would then render ''test'' in bold.&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
&lt;br /&gt;
Declaring arrays is very easy in PHP. Either you can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
$student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
$student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
$student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or alternatively&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student=array(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you can access the contents with an index into the array, as normal:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find the size of an array with ''count'' function, e.g., ''count($student)''&lt;br /&gt;
&lt;br /&gt;
In PHP, you can also have associative arrays, which are arrays whose members can be accessible with string indices. Here is an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
$test[&amp;quot;test1&amp;quot;]=1;&lt;br /&gt;
$test[&amp;quot;test2&amp;quot;]=2;&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;test1=&amp;quot;,$test[test1],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
echo &amp;quot;test2=&amp;quot;,$test[test2],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access array keys through ''array_keys'' function.&lt;br /&gt;
===Strings: Differences Between Java and PHP===&lt;br /&gt;
&lt;br /&gt;
In php, unlike in java, you can enclose strings in single quotes or double quotes&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
The following is a valid string, just like in java.&lt;br /&gt;
 //A valid string in php and in java.&lt;br /&gt;
 $str=&amp;quot;hey jeff!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And in PHP, the following is also a valid string&lt;br /&gt;
 //A valid string in PHP, but invalid in java.&lt;br /&gt;
 $str='hey jeff!' &lt;br /&gt;
&lt;br /&gt;
However, there is a caveat when using the single quotes. If we want to then use MORE single quotes within the string We have to escape them&lt;br /&gt;
To yield the string&lt;br /&gt;
''I'm Jeff's string''&lt;br /&gt;
&lt;br /&gt;
we could use either of the following definitions:&lt;br /&gt;
&lt;br /&gt;
 $str=&amp;quot;I'm Jeff's string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 $str='I\'m Jeff\'s string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if/elseif/else'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   some statements&lt;br /&gt;
 }&lt;br /&gt;
 elseif( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   more statements&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
    yet more statements&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string == &amp;quot;this is my string&amp;quot;) {&lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such thing as global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
PHP has ''for'' and ''while'' loops, similar to Java and C:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, the following code will print the sum of numbers from 1 to 10.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 echo &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The advantage of loops  becomes more obvious when you have to create a long table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Number&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Square&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
for($i=0;$i&amp;lt;100;$i++) {&lt;br /&gt;
echo &amp;quot;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;$i &amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;quot;,$i*$i,&amp;quot;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a table with two columns, each row contains a number and its square. &lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt normal control flow through a loop.  ''break'' jumps out of the loop entirely, while ''continue'' jumps to the end of the current loop iteration and flow resumes with the next loop iteration.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
PHP functions are identified with the keyword ''function'' followed by a function name and argument list for the function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
function name($arg1 [= constant], $arg2 [= constant], ...) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   function add($x,$y) {&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
   }&lt;br /&gt;
   echo &amp;quot;Sum of 2+5=&amp;quot;,add(2,5); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If an argument has a default value, it can be specified with the argument on the list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
      print &amp;quot;hello $name how are you today?&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
   helloMsg(&amp;quot;Alice&amp;quot;);&lt;br /&gt;
   helloMsg();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 hello Alice how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
 hello there how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Passing Variables ==&lt;br /&gt;
Input values can be passed to a script either through the URL request line or through data submitted by a form. Built in arrays $_GET['var'] and $_POST['var'] return the value of ''var'' depending on the access method. As we will see in the next section, values can also be passed through session variables. Finally, you can use cookies to store and retrieve values. &lt;br /&gt;
&lt;br /&gt;
Note that upon submitting a form, the page specified in the form tag's action attribute is loaded (with variables sent via post or get).&lt;br /&gt;
&lt;br /&gt;
===Get: Passing Variables via URL===&lt;br /&gt;
If you want to pass values in a URL, the format is ''http://yourserver/yourphpfile.php?var1=value1&amp;amp;var2=value2.....''&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code would be used to print the previous hello message according to a name given in the URL:&lt;br /&gt;
&lt;br /&gt;
greeting.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $person= $_GET['name'];&lt;br /&gt;
 helloMsg($person);&lt;br /&gt;
 function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
     print &amp;quot;hello $name how are you today?&amp;lt;/br&amp;gt;&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you access the URL like this:  ''greeting.php?name=Alice'', it will show ''hello Alice how are you today?''. However, if you access it with ''greeting.php'', it will print ''hello how are you today?''. Note that it does not use the default value ''there'' since ''$name'' is now a string (in this case, an empty string) and so there is argument being being passed to ''helloMsg''.&lt;br /&gt;
&lt;br /&gt;
===POST: Passing Variables via Form===&lt;br /&gt;
In PHP one method for passing variables from page to page is by sending it to the other page via posting. The way post works in&lt;br /&gt;
PHP is that PHP retrieves the variable names and values from a (slightly modified) html form and sends an those variables in the associative array named $_POST to the page specified in the FORM tag's ''action'' attribute. &lt;br /&gt;
&lt;br /&gt;
Note that receiving the data from a form is very similar to getting it from the URL. Consider the following form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;FORM action=&amp;quot;info.php&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;P&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;firstnamelabel&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;firstname&amp;quot; id=&amp;quot;firstnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;lastnamelabel&amp;quot;&amp;gt;Last name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;lastname&amp;quot; id=&amp;quot;lastnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;birthyearlabel&amp;quot;&amp;gt;Birth Year: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;birthyear&amp;quot; id=&amp;quot;birthyearlabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Male&amp;quot;&amp;gt; Male&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Female&amp;quot;&amp;gt; Female&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;submit&amp;quot; value=&amp;quot;Send&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;reset&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;/P&amp;gt;&lt;br /&gt;
&amp;lt;/FORM&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This posts the form data to ''info.php'', with inputs ''firstname'', ''lastname'', ''birthyear'', and ''gender''.&lt;br /&gt;
The values passed through the form for each input can be accessed in ''info.php'' with the $_POST array.&lt;br /&gt;
*&amp;lt;label&amp;gt; : The label tag just provides a text label for the form inputs.&lt;br /&gt;
*&amp;lt;input ''type=&amp;quot;input_type_here&amp;quot;''&amp;gt; : The input tag provides various means of inputting information into the form, there are various types of inputs such as text, radio, submit, reset, etc. &lt;br /&gt;
*&amp;lt;input ... ''name=&amp;quot;input_name&amp;quot;''&amp;gt; : &lt;br /&gt;
*&amp;lt;label ''for=&amp;quot;identifying_string&amp;quot;''&amp;gt; and &amp;lt;INPUT ... ''id=&amp;quot;identifying_string&amp;quot;'': The optional ''for'' attribute of the label tag associates the label with what we'll call an &amp;quot;identifying string&amp;quot;, which should match the string specified in the corresponding ''id'' portion of the input tag. If both of these attributes are in place, then upon clicking on the label, the corresponding input will be selected.&lt;br /&gt;
&lt;br /&gt;
info.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 echo  &amp;quot;Personal Data:&amp;quot;, $_POST['firstname'], &amp;quot; &amp;quot;, $_POST['lastname'], &amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 $birthyear=$_POST['birthyear'];&lt;br /&gt;
 echo  &amp;quot;Birth year:&amp;quot;, $_birthyear, &amp;quot; (Age:&amp;quot; , 2007-$birthyear, &amp;quot;)&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 echo &amp;quot;Gender: &amp;quot;, $_POST['gender'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For further help with using HTML forms with PHP, we recommend [http://www.tizag.com/phpT/forms.php this tutorial]&lt;br /&gt;
===Self-Submitting Forms===&lt;br /&gt;
Oftentimes we don't want to pass form values to a different page, but rather to the same page as contains the form so that we can process the information and display output on that same page. The way we do this is to specify that the form should submit to $_SERVER['PHP_SELF'] and not say, other_page.php.&lt;br /&gt;
Note that you can make self-submitting forms that use either the get or post method.&lt;br /&gt;
&lt;br /&gt;
The following example simply takes the data input from a form and, via posting, echoes it back to the user as bold text on the same page:&lt;br /&gt;
&lt;br /&gt;
bold_printer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Bold Printer&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action= &amp;lt;?php echo $_SERVER['PHP_SELF'] ?&amp;gt; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;label for=&amp;quot;text_field&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;user_text&amp;quot; id=&amp;quot;text_field&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Print in Bold&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    if(isset($_POST['user_text']))&lt;br /&gt;
    echo &amp;quot;&amp;lt;b&amp;gt;&amp;quot;, $_POST['user_text'], &amp;quot;&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Session Variables==&lt;br /&gt;
PHP provides a global array, ''$_SESSION'', for values that are available globally throughout a ''session''. The values will stay there until either you remove them in a PHP script, the user's browser is closed, or their session expires (you can tune the timeout period in the PHP configuration files).&lt;br /&gt;
&lt;br /&gt;
In order to access session values, in each php file, you should first initiate the session with the ''session_start()'' function call. After that, when you access a value with ''$_SESSION'', it will become a session values. For example:&lt;br /&gt;
&lt;br /&gt;
setsessionvars.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$_SESSION['SESSION']=42;&lt;br /&gt;
$_SESSION['username']=&amp;quot;ArthurDent&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This sets two variables, ''SESSION'' and ''username'' to be accessible by other pages in this session. In another page, you can access them like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
if (!isset($_SESSION['SESSION'])) {&lt;br /&gt;
  echo &amp;quot;error session is not registered&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else echo &amp;quot;username=&amp;quot;,$_SESSION['username'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also remove a session variable from the session with unset command:&lt;br /&gt;
&lt;br /&gt;
 unset($_SESSION['SESSION']);&lt;br /&gt;
&lt;br /&gt;
==Redirect the browser to a different page==&lt;br /&gt;
&lt;br /&gt;
It is often useful to redirect the user's browser to a different webpage, for example, after processing inputs from a form.  Here is a simple example that shows how to do this, based on whether or not the ''LOGIN'' session variable is set:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
if(isset($_SESSION[&amp;quot;LOGIN&amp;quot;])){&lt;br /&gt;
	Header(&amp;quot;Location:/welcome.php&amp;quot;);&lt;br /&gt;
}else{&lt;br /&gt;
	Header(&amp;quot;Location:/login.php&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that anytime you call the ''Header'' function, it is adding information to the header section in the HTML object that is being requested.  As such, all ''Header'' calls should go before any other HTML output in the PHP script.&lt;br /&gt;
&lt;br /&gt;
==Sending a file to the Browser==&lt;br /&gt;
&lt;br /&gt;
Another useful function to know is the ''readfile'' function.  It takes a file on the webserver's file system and sends it to the web browser.  A simple example is given below, but note that you'll probably need to specify addition header fields in order to have the web browser download the file correctly. &lt;br /&gt;
For those of you who have used previous versions of PHP, note that mime_content_type($path) is now deprecated, use finfo_file instead&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$file=$_GET['file'];&lt;br /&gt;
$name = $_SESSION[&amp;quot;UNAME&amp;quot;];&lt;br /&gt;
$path=&amp;quot;/home/uploads/$name/$file&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension&lt;br /&gt;
if (!$finfo) {&lt;br /&gt;
 	echo &amp;quot;Opening fileinfo database failed&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$mime=finfo_file($finfo, $path);&lt;br /&gt;
finfo_close($finfo);&lt;br /&gt;
&lt;br /&gt;
header('content-type:'. $mime);&lt;br /&gt;
readfile($path);    &lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uploading files with PHP==&lt;br /&gt;
&lt;br /&gt;
This section describes one way to upload files with PHP.  First, you need an HTML form that contains an input with type ''file'':&lt;br /&gt;
&lt;br /&gt;
transfer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;uploader.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
Choose a file to upload: &amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you use a ''file'' input in a form, PHP automatically adds an entry in the ''$_FILES'' array with information about the file. For example, transfer.php called the file ''uploadedfile'', so $_FILES['uploadedfile'] refers to file that the user selected in the form.  The name of the original file is $_FILES['uploadedfile']['name'].   When you upload a file, it is put in a temporary location by the web server. This temporary name is $_FILES['uploadedfile']['tmp_name']. In PHP, you can move a file from its original location to a new location by calling a built-in function, ''move_uploaded_file'', which takes an argument for the source (temporary location) and destination location. Note that relative paths in the destination location are relative to the location of the PHP script being executed.&lt;br /&gt;
&lt;br /&gt;
uploader.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$target_path = &amp;quot;uploads/&amp;quot;;&lt;br /&gt;
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);&lt;br /&gt;
&lt;br /&gt;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {&lt;br /&gt;
    echo &amp;quot;The file &amp;quot;.  basename( $_FILES['uploadedfile']['name']) . &amp;quot; has been uploaded&amp;quot;;&lt;br /&gt;
} else{&lt;br /&gt;
    echo &amp;quot;There was an error uploading the file, please try again!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Eclipse ==&lt;br /&gt;
&lt;br /&gt;
For those of you who like programming with IDEs, Eclipse has support for PHP.  See:&lt;br /&gt;
http://www.eclipse.org.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=243</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=243"/>
		<updated>2010-07-23T18:56:00Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Joins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig --add mysqld&lt;br /&gt;
 sudo /sbin/chkconfig mysqld on&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is the following: &lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to USERNAME@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER USER_NAME@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to boss@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO david@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO TODD@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
&lt;br /&gt;
===Granting the Ability to Grant: With Grant Option===&lt;br /&gt;
Additionally, in order to allow the user to grant other users privileges you must add ''WITH GRANT OPTION'' to your grant statement. For example:&lt;br /&gt;
 GRANT SELECT on powerplant.reactions, TO homer@'powerplant.springfield.gov' WITH GRANT OPTION;&lt;br /&gt;
The above command not only allows user ''homer@powerplant.springfield.gov'' to ''SELECT'' from the powerplant database's reactions table, but also allows him to grant other users the ability to select on those databases.&lt;br /&gt;
&lt;br /&gt;
i.e. Homer can issue the following command:&lt;br /&gt;
 GRANT SELECT ON powerplant.reactions TO moe@'moestavern';&lt;br /&gt;
&lt;br /&gt;
===A Note on Privileges===&lt;br /&gt;
Privileges in MySQL are highly flexible in terms of specificity, that is to say that you can grant a user specific privileges such as SELECT on a database, table, or column level. Note that only the SELECT, INSERT, and UPDATE privileges are available at the column level, if this seems confusing to you, take a moment to think about why this might be. For instance, why shouldn't there exist a column-level DROP privilege?&lt;br /&gt;
&lt;br /&gt;
The flexibility allows a database administrator (you) to give other users access only to the minimal level of privileges that they require, in accordance with the [http://en.wikipedia.org/wiki/Principle_of_least_privilege principal of least privilege].&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it. For this Command to work, you must issue the following command to grant the FILE privilege to your user&lt;br /&gt;
 GRANT FILE on *.* to 'user'@'host';&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword. When loading data from the local machine you do not need to grant your user the FILE privilege, think about why this might be the case.&lt;br /&gt;
Hint: what is going on behind the scenes when you load the data in from a local machine when your database is hosted on a different machine? &lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt'&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.w3schools.com/sql/sql_join.asp here] for a good explanation of how to use joins (see the next 4 chapters for explanations of inner, left, right and full joins).&lt;br /&gt;
&lt;br /&gt;
====Aggregate Functions====&lt;br /&gt;
The following is a brief overview of aggregate functions, however it is highly recommended that you go over [http://www.learn-mysql-tutorial.com/Aggregation.cfm this tutorial].&lt;br /&gt;
Aggregate functions are functions which take multiple data entries in one column or group and return a single value.&lt;br /&gt;
MySQL's aggregate functions include AVG (average), STD (standard deviation), SUM, MAX, MIN, and COUNT.&lt;br /&gt;
Aggregate functions are often, if not necessarily, used in conjunction with the GROUP BY clause which uses a field in the table to define the groups (sets) on which to apply the aggregate function.&lt;br /&gt;
The HAVING clause is used to filter the aggregate data and is similar in function to the where clause.&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys]&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=242</id>
		<title>PHP Original</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=242"/>
		<updated>2010-07-22T20:35:05Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Passing Variables */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= PHP5 =&lt;br /&gt;
PHP is a server-side language. When a web request for PHP file comes in, the web server processes the PHP file to produce HTML output.  That is, the main function of most PHP scripts is to dynamically create HTML content.  PHP5 support for Apache is provided by mod_php5. In Ubuntu, you can provide PHP5 support by installing ''php5''. This will install the apache2 module and the php5 command line interpreter (useful for debugging). PHP files may have different extensions, but ''.php'' is the most common.  In the Apache configuration, you can specify which extensions are going to be treated as PHP scripts by adding the extensions to the list in the ''/etc/httpd/conf.d/php.conf'' file: &lt;br /&gt;
&lt;br /&gt;
  AddType text/html .php .phtml .php3&lt;br /&gt;
&lt;br /&gt;
==Declaring PHP==&lt;br /&gt;
If a PHP file is requested by a web user, Apache parses and interprets the file.  Any code declared between PHP tags is then executed and the total HTML output is sent back to the user. PHP code can be declared in a script file in any of the following ways, with the first being the most common:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 php?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script language=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, edit a new PHP file named ''info.php'', and put these lines in the file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Put the file in the web directory for your user account (remember that this is ''~username/.html/'' if you completed the first Module).  Then access the web page by going to ''http://server/~username/info.php''. You will see something similar to the picture below:&lt;br /&gt;
&lt;br /&gt;
[[Image:Php_info.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you look at the HTML code, you will see quite a lot of HTML output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;DTD/xhtml1-transitional.dtd&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&lt;br /&gt;
 body {background-color: #ffffff; color: #000000;}&lt;br /&gt;
 body, td, th, h1, h2 {font-family: sans-serif;}&lt;br /&gt;
 pre {margin: 0px; font-family: monospace;}&lt;br /&gt;
 a:link {color: #000099; text-decoration: none; background-color: #ffffff;}&lt;br /&gt;
 a:hover {text-decoration: underline;}&lt;br /&gt;
 table {border-collapse: collapse;}&lt;br /&gt;
 .center {text-align: center;}&lt;br /&gt;
 .center table { margin-left: auto; margin-right: auto; text-align: left;}&lt;br /&gt;
 .center th { text-align: center !important; }&lt;br /&gt;
 td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}&lt;br /&gt;
 h1 {font-size: 150%;}&lt;br /&gt;
 h2 {font-size: 125%;}&lt;br /&gt;
 .p {text-align: left;}&lt;br /&gt;
 .e {background-color: #ccccff; font-weight: bold; color: #000000;}&lt;br /&gt;
 .h {background-color: #9999cc; font-weight: bold; color: #000000;}&lt;br /&gt;
 .v {background-color: #cccccc; color: #000000;}&lt;br /&gt;
 .vr {background-color: #cccccc; text-align: right; color: #000000;}&lt;br /&gt;
 img {float: right; border: 0px;}&lt;br /&gt;
 hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}&lt;br /&gt;
 &amp;lt;/style&amp;gt;&lt;br /&gt;
 &amp;lt;title&amp;gt;phpinfo()&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
 &amp;lt;body&amp;gt;&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;table border=&amp;quot;0&amp;quot; cellpadding=&amp;quot;3&amp;quot; width=&amp;quot;600&amp;quot;&amp;gt;&lt;br /&gt;
 ....&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, none of that HTML was in the ''info.php'' file.  Instead, the ''phpinfo()'' line is a built-in PHP function that produces all of that HTML.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Variables require ''$'' at the beginning of their names (regardless of the purpose, be it setting, be it accessing). You don't need to specify the type of the variable; PHP will use the variable as the correct type based on how it is used in the code.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   #this is an integer&lt;br /&gt;
   $i=5;&lt;br /&gt;
   #this is a string&lt;br /&gt;
   $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in many other languages, statements end with a '';''.  To actually produce HTML output, you simply print the HTML from the PHP code.  The simplest way is to use the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
If you use a variable inside of a string, the value of the variable will be put in the string (you might also look at the ''echo'' function):&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   $i=5;&lt;br /&gt;
   print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will generate the string&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
If you create a file with just the above code, you will notice that your browser will only receive the above line. That is, there is no ''html'' tag or anything else. Thus, PHP files are normally a mix of HTML code and PHP code.&lt;br /&gt;
Most PHP scripts, then, look more like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code that the browser will receive is then:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  The value of i is 5&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can have any number of PHP segments and HTML segments in a PHP file.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;? $i=5; ?&amp;gt;&lt;br /&gt;
The value of i is &amp;lt;?  print &amp;quot;$i&amp;quot;; ?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, whatever you print out will be interpreted as HTML by the client's browser, so you can (and will) use HTML tags in the output you produce.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;? print &amp;quot;This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt; ?&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in the following at the user's browser:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which would then render ''test'' in bold.&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
&lt;br /&gt;
Declaring arrays is very easy in PHP. Either you can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
$student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
$student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
$student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or alternatively&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student=array(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you can access the contents with an index into the array, as normal:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find the size of an array with ''count'' function, e.g., ''count($student)''&lt;br /&gt;
&lt;br /&gt;
In PHP, you can also have associative arrays, which are arrays whose members can be accessible with string indices. Here is an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
$test[&amp;quot;test1&amp;quot;]=1;&lt;br /&gt;
$test[&amp;quot;test2&amp;quot;]=2;&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;test1=&amp;quot;,$test[test1],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
echo &amp;quot;test2=&amp;quot;,$test[test2],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access array keys through ''array_keys'' function.&lt;br /&gt;
===Strings: Differences Between Java and PHP===&lt;br /&gt;
&lt;br /&gt;
In php, unlike in java, you can enclose strings in single quotes or double quotes&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
The following is a valid string, just like in java.&lt;br /&gt;
 //A valid string in php and in java.&lt;br /&gt;
 $str=&amp;quot;hey jeff!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And in PHP, the following is also a valid string&lt;br /&gt;
 //A valid string in PHP, but invalid in java.&lt;br /&gt;
 $str='hey jeff!' &lt;br /&gt;
&lt;br /&gt;
However, there is a caveat when using the single quotes. If we want to then use MORE single quotes within the string We have to escape them&lt;br /&gt;
To yield the string&lt;br /&gt;
''I'm Jeff's string''&lt;br /&gt;
&lt;br /&gt;
we could use either of the following definitions:&lt;br /&gt;
&lt;br /&gt;
 $str=&amp;quot;I'm Jeff's string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 $str='I\'m Jeff\'s string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if/elseif/else'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   some statements&lt;br /&gt;
 }&lt;br /&gt;
 elseif( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   more statements&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
    yet more statements&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string == &amp;quot;this is my string&amp;quot;) {&lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such thing as global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
PHP has ''for'' and ''while'' loops, similar to Java and C:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, the following code will print the sum of numbers from 1 to 10.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 echo &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The advantage of loops  becomes more obvious when you have to create a long table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Number&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Square&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
for($i=0;$i&amp;lt;100;$i++) {&lt;br /&gt;
echo &amp;quot;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;$i &amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;quot;,$i*$i,&amp;quot;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a table with two columns, each row contains a number and its square. &lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt normal control flow through a loop.  ''break'' jumps out of the loop entirely, while ''continue'' jumps to the end of the current loop iteration and flow resumes with the next loop iteration.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
PHP functions are identified with the keyword ''function'' followed by a function name and argument list for the function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
function name($arg1 [= constant], $arg2 [= constant], ...) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   function add($x,$y) {&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
   }&lt;br /&gt;
   echo &amp;quot;Sum of 2+5=&amp;quot;,add(2,5); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If an argument has a default value, it can be specified with the argument on the list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
      print &amp;quot;hello $name how are you today?&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
   helloMsg(&amp;quot;Alice&amp;quot;);&lt;br /&gt;
   helloMsg();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 hello Alice how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
 hello there how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Passing Variables ==&lt;br /&gt;
Input values can be passed to a script either through the URL request line or through data submitted by a form. Built in arrays $_GET['var'] and $_POST['var'] return the value of ''var'' depending on the access method. As we will see in the next section, values can also be passed through session variables. Finally, you can use cookies to store and retrieve values. &lt;br /&gt;
&lt;br /&gt;
Note that upon submitting a form, the page specified in the form tag's action attribute is loaded (with variables sent via post or get).&lt;br /&gt;
&lt;br /&gt;
===Get: Passing Variables via URL===&lt;br /&gt;
If you want to pass values in a URL, the format is ''http://yourserver/yourphpfile.php?var1=value1&amp;amp;var2=value2.....''&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code would be used to print the previous hello message according to a name given in the URL:&lt;br /&gt;
&lt;br /&gt;
greeting.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $person= $_GET['name'];&lt;br /&gt;
 helloMsg($person);&lt;br /&gt;
 function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
     print &amp;quot;hello $name how are you today?&amp;lt;/br&amp;gt;&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you access the URL like this:  ''greeting.php?name=Alice'', it will show ''hello Alice how are you today?''. However, if you access it with ''greeting.php'', it will print ''hello how are you today?''. Note that it does not use the default value ''there'' since ''$name'' is now a string (in this case, an empty string) and so there is argument being being passed to ''helloMsg''.&lt;br /&gt;
&lt;br /&gt;
===POST: Passing Variables via Form===&lt;br /&gt;
In PHP one method for passing variables from page to page is by sending it to the other page via posting. The way post works in&lt;br /&gt;
PHP is that PHP retrieves the variable names and values from a (slightly modified) html form and sends an those variables in the associative array named $_POST to the page specified in the FORM tag's ''action'' attribute. &lt;br /&gt;
&lt;br /&gt;
Note that receiving the data from a form is very similar to getting it from the URL. Consider the following form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;FORM action=&amp;quot;info.php&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;P&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;firstnamelabel&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;firstname&amp;quot; id=&amp;quot;firstnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;lastnamelabel&amp;quot;&amp;gt;Last name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;lastname&amp;quot; id=&amp;quot;lastnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;birthyearlabel&amp;quot;&amp;gt;Birth Year: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;birthyear&amp;quot; id=&amp;quot;birthyearlabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Male&amp;quot;&amp;gt; Male&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Female&amp;quot;&amp;gt; Female&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;submit&amp;quot; value=&amp;quot;Send&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;reset&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;/P&amp;gt;&lt;br /&gt;
&amp;lt;/FORM&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This posts the form data to ''info.php'', with inputs ''firstname'', ''lastname'', ''birthyear'', and ''gender''.&lt;br /&gt;
The values passed through the form for each input can be accessed in ''info.php'' with the $_POST array.&lt;br /&gt;
*&amp;lt;label&amp;gt; : The label tag just provides a text label for the form inputs.&lt;br /&gt;
*&amp;lt;input ''type=&amp;quot;input_type_here&amp;quot;''&amp;gt; : The input tag provides various means of inputting information into the form, there are various types of inputs such as text, radio, submit, reset, etc. &lt;br /&gt;
*&amp;lt;input ... ''name=&amp;quot;input_name&amp;quot;''&amp;gt; : &lt;br /&gt;
*&amp;lt;label ''for=&amp;quot;identifying_string&amp;quot;''&amp;gt; and &amp;lt;INPUT ... ''id=&amp;quot;identifying_string&amp;quot;'': The optional ''for'' attribute of the label tag associates the label with what we'll call an &amp;quot;identifying string&amp;quot;, which should match the string specified in the corresponding ''id'' portion of the input tag. If both of these attributes are in place, then upon clicking on the label, the corresponding input will be selected.&lt;br /&gt;
&lt;br /&gt;
info.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 echo  &amp;quot;Personal Data:&amp;quot;, $_POST['firstname'], &amp;quot; &amp;quot;, $_POST['lastname'], &amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 $birthyear=$_POST['birthyear'];&lt;br /&gt;
 echo  &amp;quot;Birth year:&amp;quot;, $_birthyear, &amp;quot; (Age:&amp;quot; , 2007-$birthyear, &amp;quot;)&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 echo &amp;quot;Gender: &amp;quot;, $_POST['gender'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For further help with using HTML forms with PHP, we recommend [http://www.tizag.com/phpT/forms.php this tutorial]&lt;br /&gt;
===Self-Submitting Forms===&lt;br /&gt;
Oftentimes we don't want to pass form values to a different page, but rather to the same page as contains the form so that we can process the information and display output on that same page. The way we do this is to specify that the form should submit to $_SERVER['PHP_SELF'] and not say, other_page.php.&lt;br /&gt;
Note that you can make self-submitting forms that use either the get or post method.&lt;br /&gt;
&lt;br /&gt;
The following example simply takes the data input from a form and, via posting, echoes it back to the user as bold text on the same page:&lt;br /&gt;
&lt;br /&gt;
bold_printer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Bold Printer&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action= &amp;lt;?php echo $_SERVER['PHP_SELF'] ?&amp;gt; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;label for=&amp;quot;text_field&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;user_text&amp;quot; id=&amp;quot;text_field&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Print in Bold&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    if(isset($_POST['user_text']))&lt;br /&gt;
    echo &amp;quot;&amp;lt;b&amp;gt;&amp;quot;, $_POST['user_text'], &amp;quot;&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Session Variables==&lt;br /&gt;
PHP provides a global array, ''$_SESSION'', for values that are available globally throughout a ''session''. The values will stay there until either you remove them in a PHP script, the user's browser is closed, or their session expires (you can tune the timeout period in the PHP configuration files).&lt;br /&gt;
&lt;br /&gt;
In order to access session values, in each php file you should first initiate the session with the ''session_start()'' function call. After that, when you access a value with ''$_SESSION'', it will become a session values. For example:&lt;br /&gt;
&lt;br /&gt;
setsessionvars.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$_SESSION['SESSION']=42;&lt;br /&gt;
$_SESSION['username']=&amp;quot;ArthurDent&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This sets two variables, ''SESSION'' and ''username'' to be accessible by other pages in this session. In another page, you can access them like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
if (!isset($_SESSION['SESSION'])) {&lt;br /&gt;
  echo &amp;quot;error session is not registered&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else echo &amp;quot;username=&amp;quot;,$_SESSION['username'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also remove a session variable from the session with unset command:&lt;br /&gt;
&lt;br /&gt;
 unset($_SESSION['SESSION']);&lt;br /&gt;
&lt;br /&gt;
==Redirect the browser to a different page==&lt;br /&gt;
&lt;br /&gt;
It is often useful to redirect the user's browser to a different webpage, for example, after processing inputs from a form.  Here is a simple example that shows how to do this, based on whether or not the ''LOGIN'' session variable is set:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
if(isset($_SESSION[&amp;quot;LOGIN&amp;quot;])){&lt;br /&gt;
	Header(&amp;quot;Location:/welcome.php&amp;quot;);&lt;br /&gt;
}else{&lt;br /&gt;
	Header(&amp;quot;Location:/login.php&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that anytime you call the ''Header'' function, it is adding information to the header section in the HTML object that is being requested.  As such, all ''Header'' calls should go before any other HTML output in the PHP script.&lt;br /&gt;
&lt;br /&gt;
==Sending a file to the Browser==&lt;br /&gt;
&lt;br /&gt;
Another useful function to know is the ''readfile'' function.  It takes a file on the webserver's file system and sends it to the web browser.  A simple example is given below, but note that you'll probably need to specify addition header fields in order to have the web browser download the file correctly. &lt;br /&gt;
For those of you who have used previous versions of PHP, note that mime_content_type($path) is now deprecated, use finfo_file instead&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$file=$_GET['file'];&lt;br /&gt;
$name = $_SESSION[&amp;quot;UNAME&amp;quot;];&lt;br /&gt;
$path=&amp;quot;/home/uploads/$name/$file&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension&lt;br /&gt;
if (!$finfo) {&lt;br /&gt;
 	echo &amp;quot;Opening fileinfo database failed&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$mime=finfo_file($finfo, $path);&lt;br /&gt;
finfo_close($finfo);&lt;br /&gt;
&lt;br /&gt;
header('content-type:'. $mime);&lt;br /&gt;
readfile($path);    &lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uploading files with PHP==&lt;br /&gt;
&lt;br /&gt;
This section describes one way to upload files with PHP.  First, you need an HTML form that contains an input with type ''file'':&lt;br /&gt;
&lt;br /&gt;
transfer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;uploader.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
Choose a file to upload: &amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you use a ''file'' input in a form, PHP automatically adds an entry in the ''$_FILES'' array with information about the file. For example, transfer.php called the file ''uploadedfile'', so $_FILES['uploadedfile'] refers to file that the user selected in the form.  The name of the original file is $_FILES['uploadedfile']['name'].   When you upload a file, it is put in a temporary location by the web server. This temporary name is $_FILES['uploadedfile']['tmp_name']. In PHP, you can move a file from its original location to a new location by calling a built-in function, ''move_uploaded_file'', which takes an argument for the source (temporary location) and destination location. Note that relative paths in the destination location are relative to the location of the PHP script being executed.&lt;br /&gt;
&lt;br /&gt;
uploader.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$target_path = &amp;quot;uploads/&amp;quot;;&lt;br /&gt;
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);&lt;br /&gt;
&lt;br /&gt;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {&lt;br /&gt;
    echo &amp;quot;The file &amp;quot;.  basename( $_FILES['uploadedfile']['name']) . &amp;quot; has been uploaded&amp;quot;;&lt;br /&gt;
} else{&lt;br /&gt;
    echo &amp;quot;There was an error uploading the file, please try again!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Eclipse ==&lt;br /&gt;
&lt;br /&gt;
For those of you who like programming with IDEs, Eclipse has support for PHP.  See:&lt;br /&gt;
http://www.eclipse.org.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=241</id>
		<title>PHP Original</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=241"/>
		<updated>2010-07-22T20:34:51Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Passing Variables */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= PHP5 =&lt;br /&gt;
PHP is a server-side language. When a web request for PHP file comes in, the web server processes the PHP file to produce HTML output.  That is, the main function of most PHP scripts is to dynamically create HTML content.  PHP5 support for Apache is provided by mod_php5. In Ubuntu, you can provide PHP5 support by installing ''php5''. This will install the apache2 module and the php5 command line interpreter (useful for debugging). PHP files may have different extensions, but ''.php'' is the most common.  In the Apache configuration, you can specify which extensions are going to be treated as PHP scripts by adding the extensions to the list in the ''/etc/httpd/conf.d/php.conf'' file: &lt;br /&gt;
&lt;br /&gt;
  AddType text/html .php .phtml .php3&lt;br /&gt;
&lt;br /&gt;
==Declaring PHP==&lt;br /&gt;
If a PHP file is requested by a web user, Apache parses and interprets the file.  Any code declared between PHP tags is then executed and the total HTML output is sent back to the user. PHP code can be declared in a script file in any of the following ways, with the first being the most common:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 php?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script language=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, edit a new PHP file named ''info.php'', and put these lines in the file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Put the file in the web directory for your user account (remember that this is ''~username/.html/'' if you completed the first Module).  Then access the web page by going to ''http://server/~username/info.php''. You will see something similar to the picture below:&lt;br /&gt;
&lt;br /&gt;
[[Image:Php_info.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you look at the HTML code, you will see quite a lot of HTML output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;DTD/xhtml1-transitional.dtd&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&lt;br /&gt;
 body {background-color: #ffffff; color: #000000;}&lt;br /&gt;
 body, td, th, h1, h2 {font-family: sans-serif;}&lt;br /&gt;
 pre {margin: 0px; font-family: monospace;}&lt;br /&gt;
 a:link {color: #000099; text-decoration: none; background-color: #ffffff;}&lt;br /&gt;
 a:hover {text-decoration: underline;}&lt;br /&gt;
 table {border-collapse: collapse;}&lt;br /&gt;
 .center {text-align: center;}&lt;br /&gt;
 .center table { margin-left: auto; margin-right: auto; text-align: left;}&lt;br /&gt;
 .center th { text-align: center !important; }&lt;br /&gt;
 td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}&lt;br /&gt;
 h1 {font-size: 150%;}&lt;br /&gt;
 h2 {font-size: 125%;}&lt;br /&gt;
 .p {text-align: left;}&lt;br /&gt;
 .e {background-color: #ccccff; font-weight: bold; color: #000000;}&lt;br /&gt;
 .h {background-color: #9999cc; font-weight: bold; color: #000000;}&lt;br /&gt;
 .v {background-color: #cccccc; color: #000000;}&lt;br /&gt;
 .vr {background-color: #cccccc; text-align: right; color: #000000;}&lt;br /&gt;
 img {float: right; border: 0px;}&lt;br /&gt;
 hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}&lt;br /&gt;
 &amp;lt;/style&amp;gt;&lt;br /&gt;
 &amp;lt;title&amp;gt;phpinfo()&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
 &amp;lt;body&amp;gt;&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;table border=&amp;quot;0&amp;quot; cellpadding=&amp;quot;3&amp;quot; width=&amp;quot;600&amp;quot;&amp;gt;&lt;br /&gt;
 ....&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, none of that HTML was in the ''info.php'' file.  Instead, the ''phpinfo()'' line is a built-in PHP function that produces all of that HTML.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Variables require ''$'' at the beginning of their names (regardless of the purpose, be it setting, be it accessing). You don't need to specify the type of the variable; PHP will use the variable as the correct type based on how it is used in the code.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   #this is an integer&lt;br /&gt;
   $i=5;&lt;br /&gt;
   #this is a string&lt;br /&gt;
   $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in many other languages, statements end with a '';''.  To actually produce HTML output, you simply print the HTML from the PHP code.  The simplest way is to use the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
If you use a variable inside of a string, the value of the variable will be put in the string (you might also look at the ''echo'' function):&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   $i=5;&lt;br /&gt;
   print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will generate the string&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
If you create a file with just the above code, you will notice that your browser will only receive the above line. That is, there is no ''html'' tag or anything else. Thus, PHP files are normally a mix of HTML code and PHP code.&lt;br /&gt;
Most PHP scripts, then, look more like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code that the browser will receive is then:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  The value of i is 5&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can have any number of PHP segments and HTML segments in a PHP file.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;? $i=5; ?&amp;gt;&lt;br /&gt;
The value of i is &amp;lt;?  print &amp;quot;$i&amp;quot;; ?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, whatever you print out will be interpreted as HTML by the client's browser, so you can (and will) use HTML tags in the output you produce.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;? print &amp;quot;This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt; ?&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in the following at the user's browser:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which would then render ''test'' in bold.&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
&lt;br /&gt;
Declaring arrays is very easy in PHP. Either you can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
$student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
$student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
$student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or alternatively&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student=array(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you can access the contents with an index into the array, as normal:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find the size of an array with ''count'' function, e.g., ''count($student)''&lt;br /&gt;
&lt;br /&gt;
In PHP, you can also have associative arrays, which are arrays whose members can be accessible with string indices. Here is an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
$test[&amp;quot;test1&amp;quot;]=1;&lt;br /&gt;
$test[&amp;quot;test2&amp;quot;]=2;&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;test1=&amp;quot;,$test[test1],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
echo &amp;quot;test2=&amp;quot;,$test[test2],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access array keys through ''array_keys'' function.&lt;br /&gt;
===Strings: Differences Between Java and PHP===&lt;br /&gt;
&lt;br /&gt;
In php, unlike in java, you can enclose strings in single quotes or double quotes&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
The following is a valid string, just like in java.&lt;br /&gt;
 //A valid string in php and in java.&lt;br /&gt;
 $str=&amp;quot;hey jeff!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And in PHP, the following is also a valid string&lt;br /&gt;
 //A valid string in PHP, but invalid in java.&lt;br /&gt;
 $str='hey jeff!' &lt;br /&gt;
&lt;br /&gt;
However, there is a caveat when using the single quotes. If we want to then use MORE single quotes within the string We have to escape them&lt;br /&gt;
To yield the string&lt;br /&gt;
''I'm Jeff's string''&lt;br /&gt;
&lt;br /&gt;
we could use either of the following definitions:&lt;br /&gt;
&lt;br /&gt;
 $str=&amp;quot;I'm Jeff's string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 $str='I\'m Jeff\'s string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if/elseif/else'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   some statements&lt;br /&gt;
 }&lt;br /&gt;
 elseif( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   more statements&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
    yet more statements&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string == &amp;quot;this is my string&amp;quot;) {&lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such thing as global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
PHP has ''for'' and ''while'' loops, similar to Java and C:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, the following code will print the sum of numbers from 1 to 10.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 echo &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The advantage of loops  becomes more obvious when you have to create a long table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Number&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Square&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
for($i=0;$i&amp;lt;100;$i++) {&lt;br /&gt;
echo &amp;quot;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;$i &amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;quot;,$i*$i,&amp;quot;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a table with two columns, each row contains a number and its square. &lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt normal control flow through a loop.  ''break'' jumps out of the loop entirely, while ''continue'' jumps to the end of the current loop iteration and flow resumes with the next loop iteration.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
PHP functions are identified with the keyword ''function'' followed by a function name and argument list for the function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
function name($arg1 [= constant], $arg2 [= constant], ...) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   function add($x,$y) {&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
   }&lt;br /&gt;
   echo &amp;quot;Sum of 2+5=&amp;quot;,add(2,5); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If an argument has a default value, it can be specified with the argument on the list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
      print &amp;quot;hello $name how are you today?&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
   helloMsg(&amp;quot;Alice&amp;quot;);&lt;br /&gt;
   helloMsg();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 hello Alice how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
 hello there how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Passing Variables ==&lt;br /&gt;
Input values can be passed to a script either through the URL request line or through data submitted by a form. Built in arrays $_GET['var'] and $_POST['var'] return the value of ''var'' depending on the access method. As we will see in the next section, values can also be passed through session variables. Finally, you can use cookies to store and retrieve values. Note that upon submitting a form, the page specified in the form tag's action attribute is loaded (with variables sent via post or get).&lt;br /&gt;
&lt;br /&gt;
===Get: Passing Variables via URL===&lt;br /&gt;
If you want to pass values in a URL, the format is ''http://yourserver/yourphpfile.php?var1=value1&amp;amp;var2=value2.....''&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code would be used to print the previous hello message according to a name given in the URL:&lt;br /&gt;
&lt;br /&gt;
greeting.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $person= $_GET['name'];&lt;br /&gt;
 helloMsg($person);&lt;br /&gt;
 function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
     print &amp;quot;hello $name how are you today?&amp;lt;/br&amp;gt;&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you access the URL like this:  ''greeting.php?name=Alice'', it will show ''hello Alice how are you today?''. However, if you access it with ''greeting.php'', it will print ''hello how are you today?''. Note that it does not use the default value ''there'' since ''$name'' is now a string (in this case, an empty string) and so there is argument being being passed to ''helloMsg''.&lt;br /&gt;
&lt;br /&gt;
===POST: Passing Variables via Form===&lt;br /&gt;
In PHP one method for passing variables from page to page is by sending it to the other page via posting. The way post works in&lt;br /&gt;
PHP is that PHP retrieves the variable names and values from a (slightly modified) html form and sends an those variables in the associative array named $_POST to the page specified in the FORM tag's ''action'' attribute. &lt;br /&gt;
&lt;br /&gt;
Note that receiving the data from a form is very similar to getting it from the URL. Consider the following form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;FORM action=&amp;quot;info.php&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;P&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;firstnamelabel&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;firstname&amp;quot; id=&amp;quot;firstnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;lastnamelabel&amp;quot;&amp;gt;Last name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;lastname&amp;quot; id=&amp;quot;lastnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;birthyearlabel&amp;quot;&amp;gt;Birth Year: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;birthyear&amp;quot; id=&amp;quot;birthyearlabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Male&amp;quot;&amp;gt; Male&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Female&amp;quot;&amp;gt; Female&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;submit&amp;quot; value=&amp;quot;Send&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;reset&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;/P&amp;gt;&lt;br /&gt;
&amp;lt;/FORM&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This posts the form data to ''info.php'', with inputs ''firstname'', ''lastname'', ''birthyear'', and ''gender''.&lt;br /&gt;
The values passed through the form for each input can be accessed in ''info.php'' with the $_POST array.&lt;br /&gt;
*&amp;lt;label&amp;gt; : The label tag just provides a text label for the form inputs.&lt;br /&gt;
*&amp;lt;input ''type=&amp;quot;input_type_here&amp;quot;''&amp;gt; : The input tag provides various means of inputting information into the form, there are various types of inputs such as text, radio, submit, reset, etc. &lt;br /&gt;
*&amp;lt;input ... ''name=&amp;quot;input_name&amp;quot;''&amp;gt; : &lt;br /&gt;
*&amp;lt;label ''for=&amp;quot;identifying_string&amp;quot;''&amp;gt; and &amp;lt;INPUT ... ''id=&amp;quot;identifying_string&amp;quot;'': The optional ''for'' attribute of the label tag associates the label with what we'll call an &amp;quot;identifying string&amp;quot;, which should match the string specified in the corresponding ''id'' portion of the input tag. If both of these attributes are in place, then upon clicking on the label, the corresponding input will be selected.&lt;br /&gt;
&lt;br /&gt;
info.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 echo  &amp;quot;Personal Data:&amp;quot;, $_POST['firstname'], &amp;quot; &amp;quot;, $_POST['lastname'], &amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 $birthyear=$_POST['birthyear'];&lt;br /&gt;
 echo  &amp;quot;Birth year:&amp;quot;, $_birthyear, &amp;quot; (Age:&amp;quot; , 2007-$birthyear, &amp;quot;)&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 echo &amp;quot;Gender: &amp;quot;, $_POST['gender'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For further help with using HTML forms with PHP, we recommend [http://www.tizag.com/phpT/forms.php this tutorial]&lt;br /&gt;
===Self-Submitting Forms===&lt;br /&gt;
Oftentimes we don't want to pass form values to a different page, but rather to the same page as contains the form so that we can process the information and display output on that same page. The way we do this is to specify that the form should submit to $_SERVER['PHP_SELF'] and not say, other_page.php.&lt;br /&gt;
Note that you can make self-submitting forms that use either the get or post method.&lt;br /&gt;
&lt;br /&gt;
The following example simply takes the data input from a form and, via posting, echoes it back to the user as bold text on the same page:&lt;br /&gt;
&lt;br /&gt;
bold_printer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Bold Printer&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action= &amp;lt;?php echo $_SERVER['PHP_SELF'] ?&amp;gt; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;label for=&amp;quot;text_field&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;user_text&amp;quot; id=&amp;quot;text_field&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Print in Bold&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    if(isset($_POST['user_text']))&lt;br /&gt;
    echo &amp;quot;&amp;lt;b&amp;gt;&amp;quot;, $_POST['user_text'], &amp;quot;&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Session Variables==&lt;br /&gt;
PHP provides a global array, ''$_SESSION'', for values that are available globally throughout a ''session''. The values will stay there until either you remove them in a PHP script, the user's browser is closed, or their session expires (you can tune the timeout period in the PHP configuration files).&lt;br /&gt;
&lt;br /&gt;
In order to access session values, in each php file you should first initiate the session with the ''session_start()'' function call. After that, when you access a value with ''$_SESSION'', it will become a session values. For example:&lt;br /&gt;
&lt;br /&gt;
setsessionvars.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$_SESSION['SESSION']=42;&lt;br /&gt;
$_SESSION['username']=&amp;quot;ArthurDent&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This sets two variables, ''SESSION'' and ''username'' to be accessible by other pages in this session. In another page, you can access them like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
if (!isset($_SESSION['SESSION'])) {&lt;br /&gt;
  echo &amp;quot;error session is not registered&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else echo &amp;quot;username=&amp;quot;,$_SESSION['username'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also remove a session variable from the session with unset command:&lt;br /&gt;
&lt;br /&gt;
 unset($_SESSION['SESSION']);&lt;br /&gt;
&lt;br /&gt;
==Redirect the browser to a different page==&lt;br /&gt;
&lt;br /&gt;
It is often useful to redirect the user's browser to a different webpage, for example, after processing inputs from a form.  Here is a simple example that shows how to do this, based on whether or not the ''LOGIN'' session variable is set:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
if(isset($_SESSION[&amp;quot;LOGIN&amp;quot;])){&lt;br /&gt;
	Header(&amp;quot;Location:/welcome.php&amp;quot;);&lt;br /&gt;
}else{&lt;br /&gt;
	Header(&amp;quot;Location:/login.php&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that anytime you call the ''Header'' function, it is adding information to the header section in the HTML object that is being requested.  As such, all ''Header'' calls should go before any other HTML output in the PHP script.&lt;br /&gt;
&lt;br /&gt;
==Sending a file to the Browser==&lt;br /&gt;
&lt;br /&gt;
Another useful function to know is the ''readfile'' function.  It takes a file on the webserver's file system and sends it to the web browser.  A simple example is given below, but note that you'll probably need to specify addition header fields in order to have the web browser download the file correctly. &lt;br /&gt;
For those of you who have used previous versions of PHP, note that mime_content_type($path) is now deprecated, use finfo_file instead&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$file=$_GET['file'];&lt;br /&gt;
$name = $_SESSION[&amp;quot;UNAME&amp;quot;];&lt;br /&gt;
$path=&amp;quot;/home/uploads/$name/$file&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension&lt;br /&gt;
if (!$finfo) {&lt;br /&gt;
 	echo &amp;quot;Opening fileinfo database failed&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$mime=finfo_file($finfo, $path);&lt;br /&gt;
finfo_close($finfo);&lt;br /&gt;
&lt;br /&gt;
header('content-type:'. $mime);&lt;br /&gt;
readfile($path);    &lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uploading files with PHP==&lt;br /&gt;
&lt;br /&gt;
This section describes one way to upload files with PHP.  First, you need an HTML form that contains an input with type ''file'':&lt;br /&gt;
&lt;br /&gt;
transfer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;uploader.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
Choose a file to upload: &amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you use a ''file'' input in a form, PHP automatically adds an entry in the ''$_FILES'' array with information about the file. For example, transfer.php called the file ''uploadedfile'', so $_FILES['uploadedfile'] refers to file that the user selected in the form.  The name of the original file is $_FILES['uploadedfile']['name'].   When you upload a file, it is put in a temporary location by the web server. This temporary name is $_FILES['uploadedfile']['tmp_name']. In PHP, you can move a file from its original location to a new location by calling a built-in function, ''move_uploaded_file'', which takes an argument for the source (temporary location) and destination location. Note that relative paths in the destination location are relative to the location of the PHP script being executed.&lt;br /&gt;
&lt;br /&gt;
uploader.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$target_path = &amp;quot;uploads/&amp;quot;;&lt;br /&gt;
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);&lt;br /&gt;
&lt;br /&gt;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {&lt;br /&gt;
    echo &amp;quot;The file &amp;quot;.  basename( $_FILES['uploadedfile']['name']) . &amp;quot; has been uploaded&amp;quot;;&lt;br /&gt;
} else{&lt;br /&gt;
    echo &amp;quot;There was an error uploading the file, please try again!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Eclipse ==&lt;br /&gt;
&lt;br /&gt;
For those of you who like programming with IDEs, Eclipse has support for PHP.  See:&lt;br /&gt;
http://www.eclipse.org.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=240</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=240"/>
		<updated>2010-07-22T19:27:28Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Retrieving Existing Data */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig --add mysqld&lt;br /&gt;
 sudo /sbin/chkconfig mysqld on&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is the following: &lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to USERNAME@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER USER_NAME@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to boss@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO david@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO TODD@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
&lt;br /&gt;
===Granting the Ability to Grant: With Grant Option===&lt;br /&gt;
Additionally, in order to allow the user to grant other users privileges you must add ''WITH GRANT OPTION'' to your grant statement. For example:&lt;br /&gt;
 GRANT SELECT on powerplant.reactions, TO homer@'powerplant.springfield.gov' WITH GRANT OPTION;&lt;br /&gt;
The above command not only allows user ''homer@powerplant.springfield.gov'' to ''SELECT'' from the powerplant database's reactions table, but also allows him to grant other users the ability to select on those databases.&lt;br /&gt;
&lt;br /&gt;
i.e. Homer can issue the following command:&lt;br /&gt;
 GRANT SELECT ON powerplant.reactions TO moe@'moestavern';&lt;br /&gt;
&lt;br /&gt;
===A Note on Privileges===&lt;br /&gt;
Privileges in MySQL are highly flexible in terms of specificity, that is to say that you can grant a user specific privileges such as SELECT on a database, table, or column level. Note that only the SELECT, INSERT, and UPDATE privileges are available at the column level, if this seems confusing to you, take a moment to think about why this might be. For instance, why shouldn't there exist a column-level DROP privilege?&lt;br /&gt;
&lt;br /&gt;
The flexibility allows a database administrator (you) to give other users access only to the minimal level of privileges that they require, in accordance with the [http://en.wikipedia.org/wiki/Principle_of_least_privilege principal of least privilege].&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it. For this Command to work, you must issue the following command to grant the FILE privilege to your user&lt;br /&gt;
 GRANT FILE on *.* to 'user'@'host';&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword. When loading data from the local machine you do not need to grant your user the FILE privilege, think about why this might be the case.&lt;br /&gt;
Hint: what is going on behind the scenes when you load the data in from a local machine when your database is hosted on a different machine? &lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt'&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.w3schools.com/sql/sql_join.asp here] for a good explanation of how to use joins (note, see the next 4 chapters for explanations of inner, left, right and full joins).&lt;br /&gt;
====Aggregate Functions====&lt;br /&gt;
The following is a brief overview of aggregate functions, however it is highly recommended that you go over [http://www.learn-mysql-tutorial.com/Aggregation.cfm this tutorial].&lt;br /&gt;
Aggregate functions are functions which take multiple data entries in one column or group and return a single value.&lt;br /&gt;
MySQL's aggregate functions include AVG (average), STD (standard deviation), SUM, MAX, MIN, and COUNT.&lt;br /&gt;
Aggregate functions are often, if not necessarily, used in conjunction with the GROUP BY clause which uses a field in the table to define the groups (sets) on which to apply the aggregate function.&lt;br /&gt;
The HAVING clause is used to filter the aggregate data and is similar in function to the where clause.&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys]&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=239</id>
		<title>PHP Original</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=239"/>
		<updated>2010-07-22T16:23:51Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Session Variables */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= PHP5 =&lt;br /&gt;
PHP is a server-side language. When a web request for PHP file comes in, the web server processes the PHP file to produce HTML output.  That is, the main function of most PHP scripts is to dynamically create HTML content.  PHP5 support for Apache is provided by mod_php5. In Ubuntu, you can provide PHP5 support by installing ''php5''. This will install the apache2 module and the php5 command line interpreter (useful for debugging). PHP files may have different extensions, but ''.php'' is the most common.  In the Apache configuration, you can specify which extensions are going to be treated as PHP scripts by adding the extensions to the list in the ''/etc/httpd/conf.d/php.conf'' file: &lt;br /&gt;
&lt;br /&gt;
  AddType text/html .php .phtml .php3&lt;br /&gt;
&lt;br /&gt;
==Declaring PHP==&lt;br /&gt;
If a PHP file is requested by a web user, Apache parses and interprets the file.  Any code declared between PHP tags is then executed and the total HTML output is sent back to the user. PHP code can be declared in a script file in any of the following ways, with the first being the most common:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 php?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script language=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, edit a new PHP file named ''info.php'', and put these lines in the file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Put the file in the web directory for your user account (remember that this is ''~username/.html/'' if you completed the first Module).  Then access the web page by going to ''http://server/~username/info.php''. You will see something similar to the picture below:&lt;br /&gt;
&lt;br /&gt;
[[Image:Php_info.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you look at the HTML code, you will see quite a lot of HTML output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;DTD/xhtml1-transitional.dtd&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&lt;br /&gt;
 body {background-color: #ffffff; color: #000000;}&lt;br /&gt;
 body, td, th, h1, h2 {font-family: sans-serif;}&lt;br /&gt;
 pre {margin: 0px; font-family: monospace;}&lt;br /&gt;
 a:link {color: #000099; text-decoration: none; background-color: #ffffff;}&lt;br /&gt;
 a:hover {text-decoration: underline;}&lt;br /&gt;
 table {border-collapse: collapse;}&lt;br /&gt;
 .center {text-align: center;}&lt;br /&gt;
 .center table { margin-left: auto; margin-right: auto; text-align: left;}&lt;br /&gt;
 .center th { text-align: center !important; }&lt;br /&gt;
 td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}&lt;br /&gt;
 h1 {font-size: 150%;}&lt;br /&gt;
 h2 {font-size: 125%;}&lt;br /&gt;
 .p {text-align: left;}&lt;br /&gt;
 .e {background-color: #ccccff; font-weight: bold; color: #000000;}&lt;br /&gt;
 .h {background-color: #9999cc; font-weight: bold; color: #000000;}&lt;br /&gt;
 .v {background-color: #cccccc; color: #000000;}&lt;br /&gt;
 .vr {background-color: #cccccc; text-align: right; color: #000000;}&lt;br /&gt;
 img {float: right; border: 0px;}&lt;br /&gt;
 hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}&lt;br /&gt;
 &amp;lt;/style&amp;gt;&lt;br /&gt;
 &amp;lt;title&amp;gt;phpinfo()&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
 &amp;lt;body&amp;gt;&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;table border=&amp;quot;0&amp;quot; cellpadding=&amp;quot;3&amp;quot; width=&amp;quot;600&amp;quot;&amp;gt;&lt;br /&gt;
 ....&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, none of that HTML was in the ''info.php'' file.  Instead, the ''phpinfo()'' line is a built-in PHP function that produces all of that HTML.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Variables require ''$'' at the beginning of their names (regardless of the purpose, be it setting, be it accessing). You don't need to specify the type of the variable; PHP will use the variable as the correct type based on how it is used in the code.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   #this is an integer&lt;br /&gt;
   $i=5;&lt;br /&gt;
   #this is a string&lt;br /&gt;
   $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in many other languages, statements end with a '';''.  To actually produce HTML output, you simply print the HTML from the PHP code.  The simplest way is to use the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
If you use a variable inside of a string, the value of the variable will be put in the string (you might also look at the ''echo'' function):&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   $i=5;&lt;br /&gt;
   print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will generate the string&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
If you create a file with just the above code, you will notice that your browser will only receive the above line. That is, there is no ''html'' tag or anything else. Thus, PHP files are normally a mix of HTML code and PHP code.&lt;br /&gt;
Most PHP scripts, then, look more like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code that the browser will receive is then:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  The value of i is 5&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can have any number of PHP segments and HTML segments in a PHP file.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;? $i=5; ?&amp;gt;&lt;br /&gt;
The value of i is &amp;lt;?  print &amp;quot;$i&amp;quot;; ?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, whatever you print out will be interpreted as HTML by the client's browser, so you can (and will) use HTML tags in the output you produce.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;? print &amp;quot;This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt; ?&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in the following at the user's browser:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which would then render ''test'' in bold.&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
&lt;br /&gt;
Declaring arrays is very easy in PHP. Either you can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
$student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
$student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
$student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or alternatively&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student=array(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you can access the contents with an index into the array, as normal:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find the size of an array with ''count'' function, e.g., ''count($student)''&lt;br /&gt;
&lt;br /&gt;
In PHP, you can also have associative arrays, which are arrays whose members can be accessible with string indices. Here is an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
$test[&amp;quot;test1&amp;quot;]=1;&lt;br /&gt;
$test[&amp;quot;test2&amp;quot;]=2;&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;test1=&amp;quot;,$test[test1],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
echo &amp;quot;test2=&amp;quot;,$test[test2],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access array keys through ''array_keys'' function.&lt;br /&gt;
===Strings: Differences Between Java and PHP===&lt;br /&gt;
&lt;br /&gt;
In php, unlike in java, you can enclose strings in single quotes or double quotes&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
The following is a valid string, just like in java.&lt;br /&gt;
 //A valid string in php and in java.&lt;br /&gt;
 $str=&amp;quot;hey jeff!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And in PHP, the following is also a valid string&lt;br /&gt;
 //A valid string in PHP, but invalid in java.&lt;br /&gt;
 $str='hey jeff!' &lt;br /&gt;
&lt;br /&gt;
However, there is a caveat when using the single quotes. If we want to then use MORE single quotes within the string We have to escape them&lt;br /&gt;
To yield the string&lt;br /&gt;
''I'm Jeff's string''&lt;br /&gt;
&lt;br /&gt;
we could use either of the following definitions:&lt;br /&gt;
&lt;br /&gt;
 $str=&amp;quot;I'm Jeff's string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 $str='I\'m Jeff\'s string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if/elseif/else'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   some statements&lt;br /&gt;
 }&lt;br /&gt;
 elseif( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   more statements&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
    yet more statements&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string == &amp;quot;this is my string&amp;quot;) {&lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such thing as global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
PHP has ''for'' and ''while'' loops, similar to Java and C:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, the following code will print the sum of numbers from 1 to 10.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 echo &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The advantage of loops  becomes more obvious when you have to create a long table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Number&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Square&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
for($i=0;$i&amp;lt;100;$i++) {&lt;br /&gt;
echo &amp;quot;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;$i &amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;quot;,$i*$i,&amp;quot;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a table with two columns, each row contains a number and its square. &lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt normal control flow through a loop.  ''break'' jumps out of the loop entirely, while ''continue'' jumps to the end of the current loop iteration and flow resumes with the next loop iteration.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
PHP functions are identified with the keyword ''function'' followed by a function name and argument list for the function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
function name($arg1 [= constant], $arg2 [= constant], ...) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   function add($x,$y) {&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
   }&lt;br /&gt;
   echo &amp;quot;Sum of 2+5=&amp;quot;,add(2,5); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If an argument has a default value, it can be specified with the argument on the list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
      print &amp;quot;hello $name how are you today?&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
   helloMsg(&amp;quot;Alice&amp;quot;);&lt;br /&gt;
   helloMsg();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 hello Alice how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
 hello there how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Passing Variables ==&lt;br /&gt;
Input values can be passed to a script either through the URL request line or through data submitted by a form. Built in arrays $_GET['var'] and $_POST['var'] return the value of ''var'' depending on the access method. As we will see in the next section, values can also be passed through session variables. Finally, you can use cookies to store and retrieve values.&lt;br /&gt;
&lt;br /&gt;
===Get: Passing Variables via URL===&lt;br /&gt;
If you want to pass values in a URL, the format is ''http://yourserver/yourphpfile.php?var1=value1&amp;amp;var2=value2.....''&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code would be used to print the previous hello message according to a name given in the URL:&lt;br /&gt;
&lt;br /&gt;
greeting.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $person= $_GET['name'];&lt;br /&gt;
 helloMsg($person);&lt;br /&gt;
 function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
     print &amp;quot;hello $name how are you today?&amp;lt;/br&amp;gt;&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you access the URL like this:  ''greeting.php?name=Alice'', it will show ''hello Alice how are you today?''. However, if you access it with ''greeting.php'', it will print ''hello how are you today?''. Note that it does not use the default value ''there'' since ''$name'' is now a string (in this case, an empty string) and so there is argument being being passed to ''helloMsg''.&lt;br /&gt;
&lt;br /&gt;
===POST: Passing Variables via Form===&lt;br /&gt;
In PHP one method for passing variables from page to page is by sending it to the other page via posting. The way post works in&lt;br /&gt;
PHP is that PHP retrieves the variable names and values from a (slightly modified) html form and sends an those variables in the associative array named $_POST to the page specified in the FORM tag's ''action'' attribute. &lt;br /&gt;
&lt;br /&gt;
Note that receiving the data from a form is very similar to getting it from the URL. Consider the following form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;FORM action=&amp;quot;info.php&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;P&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;firstnamelabel&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;firstname&amp;quot; id=&amp;quot;firstnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;lastnamelabel&amp;quot;&amp;gt;Last name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;lastname&amp;quot; id=&amp;quot;lastnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;birthyearlabel&amp;quot;&amp;gt;Birth Year: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;birthyear&amp;quot; id=&amp;quot;birthyearlabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Male&amp;quot;&amp;gt; Male&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Female&amp;quot;&amp;gt; Female&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;submit&amp;quot; value=&amp;quot;Send&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;reset&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;/P&amp;gt;&lt;br /&gt;
&amp;lt;/FORM&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This posts the form data to ''info.php'', with inputs ''firstname'', ''lastname'', ''birthyear'', and ''gender''.&lt;br /&gt;
The values passed through the form for each input can be accessed in ''info.php'' with the $_POST array.&lt;br /&gt;
*&amp;lt;label&amp;gt; : The label tag just provides a text label for the form inputs.&lt;br /&gt;
*&amp;lt;input ''type=&amp;quot;input_type_here&amp;quot;''&amp;gt; : The input tag provides various means of inputting information into the form, there are various types of inputs such as text, radio, submit, reset, etc. &lt;br /&gt;
*&amp;lt;input ... ''name=&amp;quot;input_name&amp;quot;''&amp;gt; : &lt;br /&gt;
*&amp;lt;label ''for=&amp;quot;identifying_string&amp;quot;''&amp;gt; and &amp;lt;INPUT ... ''id=&amp;quot;identifying_string&amp;quot;'': The optional ''for'' attribute of the label tag associates the label with what we'll call an &amp;quot;identifying string&amp;quot;, which should match the string specified in the corresponding ''id'' portion of the input tag. If both of these attributes are in place, then upon clicking on the label, the corresponding input will be selected.&lt;br /&gt;
&lt;br /&gt;
info.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 echo  &amp;quot;Personal Data:&amp;quot;, $_POST['firstname'], &amp;quot; &amp;quot;, $_POST['lastname'], &amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 $birthyear=$_POST['birthyear'];&lt;br /&gt;
 echo  &amp;quot;Birth year:&amp;quot;, $_birthyear, &amp;quot; (Age:&amp;quot; , 2007-$birthyear, &amp;quot;)&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 echo &amp;quot;Gender: &amp;quot;, $_POST['gender'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For further help with using HTML forms with PHP, we recommend [http://www.tizag.com/phpT/forms.php this tutorial]&lt;br /&gt;
===Self-Submitting Forms===&lt;br /&gt;
Oftentimes we don't want to pass form values to a different page, but rather to the same page as contains the form so that we can process the information and display output on that same page. The way we do this is to specify that the form should submit to $_SERVER['PHP_SELF'] and not say, other_page.php.&lt;br /&gt;
Note that you can make self-submitting forms that use either the get or post method.&lt;br /&gt;
&lt;br /&gt;
The following example simply takes the data input from a form and, via posting, echoes it back to the user as bold text on the same page:&lt;br /&gt;
&lt;br /&gt;
bold_printer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Bold Printer&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action= &amp;lt;?php echo $_SERVER['PHP_SELF'] ?&amp;gt; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;label for=&amp;quot;text_field&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;user_text&amp;quot; id=&amp;quot;text_field&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Print in Bold&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    if(isset($_POST['user_text']))&lt;br /&gt;
    echo &amp;quot;&amp;lt;b&amp;gt;&amp;quot;, $_POST['user_text'], &amp;quot;&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Session Variables==&lt;br /&gt;
PHP provides a global array, ''$_SESSION'', for values that are available globally throughout a ''session''. The values will stay there until either you remove them in a PHP script, the user's browser is closed, or their session expires (you can tune the timeout period in the PHP configuration files).&lt;br /&gt;
&lt;br /&gt;
In order to access session values, in each php file you should first initiate the session with the ''session_start()'' function call. After that, when you access a value with ''$_SESSION'', it will become a session values. For example:&lt;br /&gt;
&lt;br /&gt;
setsessionvars.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$_SESSION['SESSION']=42;&lt;br /&gt;
$_SESSION['username']=&amp;quot;ArthurDent&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This sets two variables, ''SESSION'' and ''username'' to be accessible by other pages in this session. In another page, you can access them like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
if (!isset($_SESSION['SESSION'])) {&lt;br /&gt;
  echo &amp;quot;error session is not registered&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else echo &amp;quot;username=&amp;quot;,$_SESSION['username'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also remove a session variable from the session with unset command:&lt;br /&gt;
&lt;br /&gt;
 unset($_SESSION['SESSION']);&lt;br /&gt;
&lt;br /&gt;
==Redirect the browser to a different page==&lt;br /&gt;
&lt;br /&gt;
It is often useful to redirect the user's browser to a different webpage, for example, after processing inputs from a form.  Here is a simple example that shows how to do this, based on whether or not the ''LOGIN'' session variable is set:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
if(isset($_SESSION[&amp;quot;LOGIN&amp;quot;])){&lt;br /&gt;
	Header(&amp;quot;Location:/welcome.php&amp;quot;);&lt;br /&gt;
}else{&lt;br /&gt;
	Header(&amp;quot;Location:/login.php&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that anytime you call the ''Header'' function, it is adding information to the header section in the HTML object that is being requested.  As such, all ''Header'' calls should go before any other HTML output in the PHP script.&lt;br /&gt;
&lt;br /&gt;
==Sending a file to the Browser==&lt;br /&gt;
&lt;br /&gt;
Another useful function to know is the ''readfile'' function.  It takes a file on the webserver's file system and sends it to the web browser.  A simple example is given below, but note that you'll probably need to specify addition header fields in order to have the web browser download the file correctly. &lt;br /&gt;
For those of you who have used previous versions of PHP, note that mime_content_type($path) is now deprecated, use finfo_file instead&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$file=$_GET['file'];&lt;br /&gt;
$name = $_SESSION[&amp;quot;UNAME&amp;quot;];&lt;br /&gt;
$path=&amp;quot;/home/uploads/$name/$file&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension&lt;br /&gt;
if (!$finfo) {&lt;br /&gt;
 	echo &amp;quot;Opening fileinfo database failed&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$mime=finfo_file($finfo, $path);&lt;br /&gt;
finfo_close($finfo);&lt;br /&gt;
&lt;br /&gt;
header('content-type:'. $mime);&lt;br /&gt;
readfile($path);    &lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uploading files with PHP==&lt;br /&gt;
&lt;br /&gt;
This section describes one way to upload files with PHP.  First, you need an HTML form that contains an input with type ''file'':&lt;br /&gt;
&lt;br /&gt;
transfer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;uploader.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
Choose a file to upload: &amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you use a ''file'' input in a form, PHP automatically adds an entry in the ''$_FILES'' array with information about the file. For example, transfer.php called the file ''uploadedfile'', so $_FILES['uploadedfile'] refers to file that the user selected in the form.  The name of the original file is $_FILES['uploadedfile']['name'].   When you upload a file, it is put in a temporary location by the web server. This temporary name is $_FILES['uploadedfile']['tmp_name']. In PHP, you can move a file from its original location to a new location by calling a built-in function, ''move_uploaded_file'', which takes an argument for the source (temporary location) and destination location. Note that relative paths in the destination location are relative to the location of the PHP script being executed.&lt;br /&gt;
&lt;br /&gt;
uploader.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$target_path = &amp;quot;uploads/&amp;quot;;&lt;br /&gt;
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);&lt;br /&gt;
&lt;br /&gt;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {&lt;br /&gt;
    echo &amp;quot;The file &amp;quot;.  basename( $_FILES['uploadedfile']['name']) . &amp;quot; has been uploaded&amp;quot;;&lt;br /&gt;
} else{&lt;br /&gt;
    echo &amp;quot;There was an error uploading the file, please try again!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Eclipse ==&lt;br /&gt;
&lt;br /&gt;
For those of you who like programming with IDEs, Eclipse has support for PHP.  See:&lt;br /&gt;
http://www.eclipse.org.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=238</id>
		<title>PHP Original</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=238"/>
		<updated>2010-07-22T16:22:41Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Self-Submitting Forms */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= PHP5 =&lt;br /&gt;
PHP is a server-side language. When a web request for PHP file comes in, the web server processes the PHP file to produce HTML output.  That is, the main function of most PHP scripts is to dynamically create HTML content.  PHP5 support for Apache is provided by mod_php5. In Ubuntu, you can provide PHP5 support by installing ''php5''. This will install the apache2 module and the php5 command line interpreter (useful for debugging). PHP files may have different extensions, but ''.php'' is the most common.  In the Apache configuration, you can specify which extensions are going to be treated as PHP scripts by adding the extensions to the list in the ''/etc/httpd/conf.d/php.conf'' file: &lt;br /&gt;
&lt;br /&gt;
  AddType text/html .php .phtml .php3&lt;br /&gt;
&lt;br /&gt;
==Declaring PHP==&lt;br /&gt;
If a PHP file is requested by a web user, Apache parses and interprets the file.  Any code declared between PHP tags is then executed and the total HTML output is sent back to the user. PHP code can be declared in a script file in any of the following ways, with the first being the most common:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 php?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script language=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, edit a new PHP file named ''info.php'', and put these lines in the file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Put the file in the web directory for your user account (remember that this is ''~username/.html/'' if you completed the first Module).  Then access the web page by going to ''http://server/~username/info.php''. You will see something similar to the picture below:&lt;br /&gt;
&lt;br /&gt;
[[Image:Php_info.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you look at the HTML code, you will see quite a lot of HTML output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;DTD/xhtml1-transitional.dtd&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&lt;br /&gt;
 body {background-color: #ffffff; color: #000000;}&lt;br /&gt;
 body, td, th, h1, h2 {font-family: sans-serif;}&lt;br /&gt;
 pre {margin: 0px; font-family: monospace;}&lt;br /&gt;
 a:link {color: #000099; text-decoration: none; background-color: #ffffff;}&lt;br /&gt;
 a:hover {text-decoration: underline;}&lt;br /&gt;
 table {border-collapse: collapse;}&lt;br /&gt;
 .center {text-align: center;}&lt;br /&gt;
 .center table { margin-left: auto; margin-right: auto; text-align: left;}&lt;br /&gt;
 .center th { text-align: center !important; }&lt;br /&gt;
 td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}&lt;br /&gt;
 h1 {font-size: 150%;}&lt;br /&gt;
 h2 {font-size: 125%;}&lt;br /&gt;
 .p {text-align: left;}&lt;br /&gt;
 .e {background-color: #ccccff; font-weight: bold; color: #000000;}&lt;br /&gt;
 .h {background-color: #9999cc; font-weight: bold; color: #000000;}&lt;br /&gt;
 .v {background-color: #cccccc; color: #000000;}&lt;br /&gt;
 .vr {background-color: #cccccc; text-align: right; color: #000000;}&lt;br /&gt;
 img {float: right; border: 0px;}&lt;br /&gt;
 hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}&lt;br /&gt;
 &amp;lt;/style&amp;gt;&lt;br /&gt;
 &amp;lt;title&amp;gt;phpinfo()&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
 &amp;lt;body&amp;gt;&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;table border=&amp;quot;0&amp;quot; cellpadding=&amp;quot;3&amp;quot; width=&amp;quot;600&amp;quot;&amp;gt;&lt;br /&gt;
 ....&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, none of that HTML was in the ''info.php'' file.  Instead, the ''phpinfo()'' line is a built-in PHP function that produces all of that HTML.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Variables require ''$'' at the beginning of their names (regardless of the purpose, be it setting, be it accessing). You don't need to specify the type of the variable; PHP will use the variable as the correct type based on how it is used in the code.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   #this is an integer&lt;br /&gt;
   $i=5;&lt;br /&gt;
   #this is a string&lt;br /&gt;
   $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in many other languages, statements end with a '';''.  To actually produce HTML output, you simply print the HTML from the PHP code.  The simplest way is to use the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
If you use a variable inside of a string, the value of the variable will be put in the string (you might also look at the ''echo'' function):&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   $i=5;&lt;br /&gt;
   print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will generate the string&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
If you create a file with just the above code, you will notice that your browser will only receive the above line. That is, there is no ''html'' tag or anything else. Thus, PHP files are normally a mix of HTML code and PHP code.&lt;br /&gt;
Most PHP scripts, then, look more like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code that the browser will receive is then:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  The value of i is 5&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can have any number of PHP segments and HTML segments in a PHP file.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;? $i=5; ?&amp;gt;&lt;br /&gt;
The value of i is &amp;lt;?  print &amp;quot;$i&amp;quot;; ?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, whatever you print out will be interpreted as HTML by the client's browser, so you can (and will) use HTML tags in the output you produce.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;? print &amp;quot;This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt; ?&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in the following at the user's browser:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which would then render ''test'' in bold.&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
&lt;br /&gt;
Declaring arrays is very easy in PHP. Either you can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
$student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
$student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
$student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or alternatively&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student=array(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you can access the contents with an index into the array, as normal:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find the size of an array with ''count'' function, e.g., ''count($student)''&lt;br /&gt;
&lt;br /&gt;
In PHP, you can also have associative arrays, which are arrays whose members can be accessible with string indices. Here is an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
$test[&amp;quot;test1&amp;quot;]=1;&lt;br /&gt;
$test[&amp;quot;test2&amp;quot;]=2;&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;test1=&amp;quot;,$test[test1],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
echo &amp;quot;test2=&amp;quot;,$test[test2],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access array keys through ''array_keys'' function.&lt;br /&gt;
===Strings: Differences Between Java and PHP===&lt;br /&gt;
&lt;br /&gt;
In php, unlike in java, you can enclose strings in single quotes or double quotes&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
The following is a valid string, just like in java.&lt;br /&gt;
 //A valid string in php and in java.&lt;br /&gt;
 $str=&amp;quot;hey jeff!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And in PHP, the following is also a valid string&lt;br /&gt;
 //A valid string in PHP, but invalid in java.&lt;br /&gt;
 $str='hey jeff!' &lt;br /&gt;
&lt;br /&gt;
However, there is a caveat when using the single quotes. If we want to then use MORE single quotes within the string We have to escape them&lt;br /&gt;
To yield the string&lt;br /&gt;
''I'm Jeff's string''&lt;br /&gt;
&lt;br /&gt;
we could use either of the following definitions:&lt;br /&gt;
&lt;br /&gt;
 $str=&amp;quot;I'm Jeff's string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 $str='I\'m Jeff\'s string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if/elseif/else'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   some statements&lt;br /&gt;
 }&lt;br /&gt;
 elseif( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   more statements&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
    yet more statements&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string == &amp;quot;this is my string&amp;quot;) {&lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such thing as global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
PHP has ''for'' and ''while'' loops, similar to Java and C:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, the following code will print the sum of numbers from 1 to 10.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 echo &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The advantage of loops  becomes more obvious when you have to create a long table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Number&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Square&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
for($i=0;$i&amp;lt;100;$i++) {&lt;br /&gt;
echo &amp;quot;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;$i &amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;quot;,$i*$i,&amp;quot;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a table with two columns, each row contains a number and its square. &lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt normal control flow through a loop.  ''break'' jumps out of the loop entirely, while ''continue'' jumps to the end of the current loop iteration and flow resumes with the next loop iteration.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
PHP functions are identified with the keyword ''function'' followed by a function name and argument list for the function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
function name($arg1 [= constant], $arg2 [= constant], ...) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   function add($x,$y) {&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
   }&lt;br /&gt;
   echo &amp;quot;Sum of 2+5=&amp;quot;,add(2,5); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If an argument has a default value, it can be specified with the argument on the list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
      print &amp;quot;hello $name how are you today?&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
   helloMsg(&amp;quot;Alice&amp;quot;);&lt;br /&gt;
   helloMsg();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 hello Alice how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
 hello there how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Passing Variables ==&lt;br /&gt;
Input values can be passed to a script either through the URL request line or through data submitted by a form. Built in arrays $_GET['var'] and $_POST['var'] return the value of ''var'' depending on the access method. As we will see in the next section, values can also be passed through session variables. Finally, you can use cookies to store and retrieve values.&lt;br /&gt;
&lt;br /&gt;
===Get: Passing Variables via URL===&lt;br /&gt;
If you want to pass values in a URL, the format is ''http://yourserver/yourphpfile.php?var1=value1&amp;amp;var2=value2.....''&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code would be used to print the previous hello message according to a name given in the URL:&lt;br /&gt;
&lt;br /&gt;
greeting.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $person= $_GET['name'];&lt;br /&gt;
 helloMsg($person);&lt;br /&gt;
 function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
     print &amp;quot;hello $name how are you today?&amp;lt;/br&amp;gt;&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you access the URL like this:  ''greeting.php?name=Alice'', it will show ''hello Alice how are you today?''. However, if you access it with ''greeting.php'', it will print ''hello how are you today?''. Note that it does not use the default value ''there'' since ''$name'' is now a string (in this case, an empty string) and so there is argument being being passed to ''helloMsg''.&lt;br /&gt;
&lt;br /&gt;
===POST: Passing Variables via Form===&lt;br /&gt;
In PHP one method for passing variables from page to page is by sending it to the other page via posting. The way post works in&lt;br /&gt;
PHP is that PHP retrieves the variable names and values from a (slightly modified) html form and sends an those variables in the associative array named $_POST to the page specified in the FORM tag's ''action'' attribute. &lt;br /&gt;
&lt;br /&gt;
Note that receiving the data from a form is very similar to getting it from the URL. Consider the following form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;FORM action=&amp;quot;info.php&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;P&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;firstnamelabel&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;firstname&amp;quot; id=&amp;quot;firstnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;lastnamelabel&amp;quot;&amp;gt;Last name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;lastname&amp;quot; id=&amp;quot;lastnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;birthyearlabel&amp;quot;&amp;gt;Birth Year: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;birthyear&amp;quot; id=&amp;quot;birthyearlabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Male&amp;quot;&amp;gt; Male&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Female&amp;quot;&amp;gt; Female&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;submit&amp;quot; value=&amp;quot;Send&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;reset&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;/P&amp;gt;&lt;br /&gt;
&amp;lt;/FORM&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This posts the form data to ''info.php'', with inputs ''firstname'', ''lastname'', ''birthyear'', and ''gender''.&lt;br /&gt;
The values passed through the form for each input can be accessed in ''info.php'' with the $_POST array.&lt;br /&gt;
*&amp;lt;label&amp;gt; : The label tag just provides a text label for the form inputs.&lt;br /&gt;
*&amp;lt;input ''type=&amp;quot;input_type_here&amp;quot;''&amp;gt; : The input tag provides various means of inputting information into the form, there are various types of inputs such as text, radio, submit, reset, etc. &lt;br /&gt;
*&amp;lt;input ... ''name=&amp;quot;input_name&amp;quot;''&amp;gt; : &lt;br /&gt;
*&amp;lt;label ''for=&amp;quot;identifying_string&amp;quot;''&amp;gt; and &amp;lt;INPUT ... ''id=&amp;quot;identifying_string&amp;quot;'': The optional ''for'' attribute of the label tag associates the label with what we'll call an &amp;quot;identifying string&amp;quot;, which should match the string specified in the corresponding ''id'' portion of the input tag. If both of these attributes are in place, then upon clicking on the label, the corresponding input will be selected.&lt;br /&gt;
&lt;br /&gt;
info.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 echo  &amp;quot;Personal Data:&amp;quot;, $_POST['firstname'], &amp;quot; &amp;quot;, $_POST['lastname'], &amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 $birthyear=$_POST['birthyear'];&lt;br /&gt;
 echo  &amp;quot;Birth year:&amp;quot;, $_birthyear, &amp;quot; (Age:&amp;quot; , 2007-$birthyear, &amp;quot;)&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 echo &amp;quot;Gender: &amp;quot;, $_POST['gender'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For further help with using HTML forms with PHP, we recommend [http://www.tizag.com/phpT/forms.php this tutorial]&lt;br /&gt;
===Self-Submitting Forms===&lt;br /&gt;
Oftentimes we don't want to pass form values to a different page, but rather to the same page as contains the form so that we can process the information and display output on that same page. The way we do this is to specify that the form should submit to $_SERVER['PHP_SELF'] and not say, other_page.php.&lt;br /&gt;
Note that you can make self-submitting forms that use either the get or post method.&lt;br /&gt;
&lt;br /&gt;
The following example simply takes the data input from a form and, via posting, echoes it back to the user as bold text on the same page:&lt;br /&gt;
&lt;br /&gt;
bold_printer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Bold Printer&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action= &amp;lt;?php echo $_SERVER['PHP_SELF'] ?&amp;gt; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;label for=&amp;quot;text_field&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;user_text&amp;quot; id=&amp;quot;text_field&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Print in Bold&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    if(isset($_POST['user_text']))&lt;br /&gt;
    echo &amp;quot;&amp;lt;b&amp;gt;&amp;quot;, $_POST['user_text'], &amp;quot;&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Session Variables==&lt;br /&gt;
PHP provides a global array, ''$_SESSION'', for values that are available globally throughout a ''session''. The values will stay there until either you remove them in a PHP script, the user's browser is closed, or their session expires (you can tune the timeout period in the PHP configuration files).&lt;br /&gt;
&lt;br /&gt;
In order to access session values, you should first initiate the session with the ''session_start()'' function call. After that, when you access a values with ''$_SESSION'', it will become a session values. For example:&lt;br /&gt;
&lt;br /&gt;
setsessionvars.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$_SESSION['SESSION']=42;&lt;br /&gt;
$_SESSION['username']=&amp;quot;ArthurDent&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This sets two variables, ''SESSION'' and ''username'' to be accessible by other pages in this session. In another page, you can access them like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
if (!isset($_SESSION['SESSION'])) {&lt;br /&gt;
  echo &amp;quot;error session is not registered&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else echo &amp;quot;username=&amp;quot;,$_SESSION['username'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also remove a session variable from the session with unset command:&lt;br /&gt;
&lt;br /&gt;
 unset($_SESSION['SESSION']);&lt;br /&gt;
&lt;br /&gt;
==Redirect the browser to a different page==&lt;br /&gt;
&lt;br /&gt;
It is often useful to redirect the user's browser to a different webpage, for example, after processing inputs from a form.  Here is a simple example that shows how to do this, based on whether or not the ''LOGIN'' session variable is set:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
if(isset($_SESSION[&amp;quot;LOGIN&amp;quot;])){&lt;br /&gt;
	Header(&amp;quot;Location:/welcome.php&amp;quot;);&lt;br /&gt;
}else{&lt;br /&gt;
	Header(&amp;quot;Location:/login.php&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that anytime you call the ''Header'' function, it is adding information to the header section in the HTML object that is being requested.  As such, all ''Header'' calls should go before any other HTML output in the PHP script.&lt;br /&gt;
&lt;br /&gt;
==Sending a file to the Browser==&lt;br /&gt;
&lt;br /&gt;
Another useful function to know is the ''readfile'' function.  It takes a file on the webserver's file system and sends it to the web browser.  A simple example is given below, but note that you'll probably need to specify addition header fields in order to have the web browser download the file correctly. &lt;br /&gt;
For those of you who have used previous versions of PHP, note that mime_content_type($path) is now deprecated, use finfo_file instead&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$file=$_GET['file'];&lt;br /&gt;
$name = $_SESSION[&amp;quot;UNAME&amp;quot;];&lt;br /&gt;
$path=&amp;quot;/home/uploads/$name/$file&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension&lt;br /&gt;
if (!$finfo) {&lt;br /&gt;
 	echo &amp;quot;Opening fileinfo database failed&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$mime=finfo_file($finfo, $path);&lt;br /&gt;
finfo_close($finfo);&lt;br /&gt;
&lt;br /&gt;
header('content-type:'. $mime);&lt;br /&gt;
readfile($path);    &lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uploading files with PHP==&lt;br /&gt;
&lt;br /&gt;
This section describes one way to upload files with PHP.  First, you need an HTML form that contains an input with type ''file'':&lt;br /&gt;
&lt;br /&gt;
transfer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;uploader.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
Choose a file to upload: &amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you use a ''file'' input in a form, PHP automatically adds an entry in the ''$_FILES'' array with information about the file. For example, transfer.php called the file ''uploadedfile'', so $_FILES['uploadedfile'] refers to file that the user selected in the form.  The name of the original file is $_FILES['uploadedfile']['name'].   When you upload a file, it is put in a temporary location by the web server. This temporary name is $_FILES['uploadedfile']['tmp_name']. In PHP, you can move a file from its original location to a new location by calling a built-in function, ''move_uploaded_file'', which takes an argument for the source (temporary location) and destination location. Note that relative paths in the destination location are relative to the location of the PHP script being executed.&lt;br /&gt;
&lt;br /&gt;
uploader.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$target_path = &amp;quot;uploads/&amp;quot;;&lt;br /&gt;
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);&lt;br /&gt;
&lt;br /&gt;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {&lt;br /&gt;
    echo &amp;quot;The file &amp;quot;.  basename( $_FILES['uploadedfile']['name']) . &amp;quot; has been uploaded&amp;quot;;&lt;br /&gt;
} else{&lt;br /&gt;
    echo &amp;quot;There was an error uploading the file, please try again!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Eclipse ==&lt;br /&gt;
&lt;br /&gt;
For those of you who like programming with IDEs, Eclipse has support for PHP.  See:&lt;br /&gt;
http://www.eclipse.org.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=237</id>
		<title>PHP Original</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=237"/>
		<updated>2010-07-22T16:22:13Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Self-Submitting Forms */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= PHP5 =&lt;br /&gt;
PHP is a server-side language. When a web request for PHP file comes in, the web server processes the PHP file to produce HTML output.  That is, the main function of most PHP scripts is to dynamically create HTML content.  PHP5 support for Apache is provided by mod_php5. In Ubuntu, you can provide PHP5 support by installing ''php5''. This will install the apache2 module and the php5 command line interpreter (useful for debugging). PHP files may have different extensions, but ''.php'' is the most common.  In the Apache configuration, you can specify which extensions are going to be treated as PHP scripts by adding the extensions to the list in the ''/etc/httpd/conf.d/php.conf'' file: &lt;br /&gt;
&lt;br /&gt;
  AddType text/html .php .phtml .php3&lt;br /&gt;
&lt;br /&gt;
==Declaring PHP==&lt;br /&gt;
If a PHP file is requested by a web user, Apache parses and interprets the file.  Any code declared between PHP tags is then executed and the total HTML output is sent back to the user. PHP code can be declared in a script file in any of the following ways, with the first being the most common:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 php?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script language=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, edit a new PHP file named ''info.php'', and put these lines in the file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Put the file in the web directory for your user account (remember that this is ''~username/.html/'' if you completed the first Module).  Then access the web page by going to ''http://server/~username/info.php''. You will see something similar to the picture below:&lt;br /&gt;
&lt;br /&gt;
[[Image:Php_info.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you look at the HTML code, you will see quite a lot of HTML output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;DTD/xhtml1-transitional.dtd&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&lt;br /&gt;
 body {background-color: #ffffff; color: #000000;}&lt;br /&gt;
 body, td, th, h1, h2 {font-family: sans-serif;}&lt;br /&gt;
 pre {margin: 0px; font-family: monospace;}&lt;br /&gt;
 a:link {color: #000099; text-decoration: none; background-color: #ffffff;}&lt;br /&gt;
 a:hover {text-decoration: underline;}&lt;br /&gt;
 table {border-collapse: collapse;}&lt;br /&gt;
 .center {text-align: center;}&lt;br /&gt;
 .center table { margin-left: auto; margin-right: auto; text-align: left;}&lt;br /&gt;
 .center th { text-align: center !important; }&lt;br /&gt;
 td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}&lt;br /&gt;
 h1 {font-size: 150%;}&lt;br /&gt;
 h2 {font-size: 125%;}&lt;br /&gt;
 .p {text-align: left;}&lt;br /&gt;
 .e {background-color: #ccccff; font-weight: bold; color: #000000;}&lt;br /&gt;
 .h {background-color: #9999cc; font-weight: bold; color: #000000;}&lt;br /&gt;
 .v {background-color: #cccccc; color: #000000;}&lt;br /&gt;
 .vr {background-color: #cccccc; text-align: right; color: #000000;}&lt;br /&gt;
 img {float: right; border: 0px;}&lt;br /&gt;
 hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}&lt;br /&gt;
 &amp;lt;/style&amp;gt;&lt;br /&gt;
 &amp;lt;title&amp;gt;phpinfo()&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
 &amp;lt;body&amp;gt;&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;table border=&amp;quot;0&amp;quot; cellpadding=&amp;quot;3&amp;quot; width=&amp;quot;600&amp;quot;&amp;gt;&lt;br /&gt;
 ....&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, none of that HTML was in the ''info.php'' file.  Instead, the ''phpinfo()'' line is a built-in PHP function that produces all of that HTML.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Variables require ''$'' at the beginning of their names (regardless of the purpose, be it setting, be it accessing). You don't need to specify the type of the variable; PHP will use the variable as the correct type based on how it is used in the code.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   #this is an integer&lt;br /&gt;
   $i=5;&lt;br /&gt;
   #this is a string&lt;br /&gt;
   $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in many other languages, statements end with a '';''.  To actually produce HTML output, you simply print the HTML from the PHP code.  The simplest way is to use the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
If you use a variable inside of a string, the value of the variable will be put in the string (you might also look at the ''echo'' function):&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   $i=5;&lt;br /&gt;
   print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will generate the string&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
If you create a file with just the above code, you will notice that your browser will only receive the above line. That is, there is no ''html'' tag or anything else. Thus, PHP files are normally a mix of HTML code and PHP code.&lt;br /&gt;
Most PHP scripts, then, look more like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code that the browser will receive is then:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  The value of i is 5&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can have any number of PHP segments and HTML segments in a PHP file.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;? $i=5; ?&amp;gt;&lt;br /&gt;
The value of i is &amp;lt;?  print &amp;quot;$i&amp;quot;; ?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, whatever you print out will be interpreted as HTML by the client's browser, so you can (and will) use HTML tags in the output you produce.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;? print &amp;quot;This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt; ?&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in the following at the user's browser:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which would then render ''test'' in bold.&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
&lt;br /&gt;
Declaring arrays is very easy in PHP. Either you can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
$student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
$student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
$student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or alternatively&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student=array(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you can access the contents with an index into the array, as normal:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find the size of an array with ''count'' function, e.g., ''count($student)''&lt;br /&gt;
&lt;br /&gt;
In PHP, you can also have associative arrays, which are arrays whose members can be accessible with string indices. Here is an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
$test[&amp;quot;test1&amp;quot;]=1;&lt;br /&gt;
$test[&amp;quot;test2&amp;quot;]=2;&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;test1=&amp;quot;,$test[test1],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
echo &amp;quot;test2=&amp;quot;,$test[test2],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access array keys through ''array_keys'' function.&lt;br /&gt;
===Strings: Differences Between Java and PHP===&lt;br /&gt;
&lt;br /&gt;
In php, unlike in java, you can enclose strings in single quotes or double quotes&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
The following is a valid string, just like in java.&lt;br /&gt;
 //A valid string in php and in java.&lt;br /&gt;
 $str=&amp;quot;hey jeff!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And in PHP, the following is also a valid string&lt;br /&gt;
 //A valid string in PHP, but invalid in java.&lt;br /&gt;
 $str='hey jeff!' &lt;br /&gt;
&lt;br /&gt;
However, there is a caveat when using the single quotes. If we want to then use MORE single quotes within the string We have to escape them&lt;br /&gt;
To yield the string&lt;br /&gt;
''I'm Jeff's string''&lt;br /&gt;
&lt;br /&gt;
we could use either of the following definitions:&lt;br /&gt;
&lt;br /&gt;
 $str=&amp;quot;I'm Jeff's string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 $str='I\'m Jeff\'s string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if/elseif/else'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   some statements&lt;br /&gt;
 }&lt;br /&gt;
 elseif( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   more statements&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
    yet more statements&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string == &amp;quot;this is my string&amp;quot;) {&lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such thing as global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
PHP has ''for'' and ''while'' loops, similar to Java and C:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, the following code will print the sum of numbers from 1 to 10.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 echo &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The advantage of loops  becomes more obvious when you have to create a long table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Number&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Square&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
for($i=0;$i&amp;lt;100;$i++) {&lt;br /&gt;
echo &amp;quot;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;$i &amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;quot;,$i*$i,&amp;quot;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a table with two columns, each row contains a number and its square. &lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt normal control flow through a loop.  ''break'' jumps out of the loop entirely, while ''continue'' jumps to the end of the current loop iteration and flow resumes with the next loop iteration.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
PHP functions are identified with the keyword ''function'' followed by a function name and argument list for the function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
function name($arg1 [= constant], $arg2 [= constant], ...) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   function add($x,$y) {&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
   }&lt;br /&gt;
   echo &amp;quot;Sum of 2+5=&amp;quot;,add(2,5); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If an argument has a default value, it can be specified with the argument on the list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
      print &amp;quot;hello $name how are you today?&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
   helloMsg(&amp;quot;Alice&amp;quot;);&lt;br /&gt;
   helloMsg();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 hello Alice how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
 hello there how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Passing Variables ==&lt;br /&gt;
Input values can be passed to a script either through the URL request line or through data submitted by a form. Built in arrays $_GET['var'] and $_POST['var'] return the value of ''var'' depending on the access method. As we will see in the next section, values can also be passed through session variables. Finally, you can use cookies to store and retrieve values.&lt;br /&gt;
&lt;br /&gt;
===Get: Passing Variables via URL===&lt;br /&gt;
If you want to pass values in a URL, the format is ''http://yourserver/yourphpfile.php?var1=value1&amp;amp;var2=value2.....''&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code would be used to print the previous hello message according to a name given in the URL:&lt;br /&gt;
&lt;br /&gt;
greeting.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $person= $_GET['name'];&lt;br /&gt;
 helloMsg($person);&lt;br /&gt;
 function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
     print &amp;quot;hello $name how are you today?&amp;lt;/br&amp;gt;&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you access the URL like this:  ''greeting.php?name=Alice'', it will show ''hello Alice how are you today?''. However, if you access it with ''greeting.php'', it will print ''hello how are you today?''. Note that it does not use the default value ''there'' since ''$name'' is now a string (in this case, an empty string) and so there is argument being being passed to ''helloMsg''.&lt;br /&gt;
&lt;br /&gt;
===POST: Passing Variables via Form===&lt;br /&gt;
In PHP one method for passing variables from page to page is by sending it to the other page via posting. The way post works in&lt;br /&gt;
PHP is that PHP retrieves the variable names and values from a (slightly modified) html form and sends an those variables in the associative array named $_POST to the page specified in the FORM tag's ''action'' attribute. &lt;br /&gt;
&lt;br /&gt;
Note that receiving the data from a form is very similar to getting it from the URL. Consider the following form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;FORM action=&amp;quot;info.php&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;P&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;firstnamelabel&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;firstname&amp;quot; id=&amp;quot;firstnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;lastnamelabel&amp;quot;&amp;gt;Last name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;lastname&amp;quot; id=&amp;quot;lastnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;birthyearlabel&amp;quot;&amp;gt;Birth Year: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;birthyear&amp;quot; id=&amp;quot;birthyearlabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Male&amp;quot;&amp;gt; Male&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Female&amp;quot;&amp;gt; Female&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;submit&amp;quot; value=&amp;quot;Send&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;reset&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;/P&amp;gt;&lt;br /&gt;
&amp;lt;/FORM&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This posts the form data to ''info.php'', with inputs ''firstname'', ''lastname'', ''birthyear'', and ''gender''.&lt;br /&gt;
The values passed through the form for each input can be accessed in ''info.php'' with the $_POST array.&lt;br /&gt;
*&amp;lt;label&amp;gt; : The label tag just provides a text label for the form inputs.&lt;br /&gt;
*&amp;lt;input ''type=&amp;quot;input_type_here&amp;quot;''&amp;gt; : The input tag provides various means of inputting information into the form, there are various types of inputs such as text, radio, submit, reset, etc. &lt;br /&gt;
*&amp;lt;input ... ''name=&amp;quot;input_name&amp;quot;''&amp;gt; : &lt;br /&gt;
*&amp;lt;label ''for=&amp;quot;identifying_string&amp;quot;''&amp;gt; and &amp;lt;INPUT ... ''id=&amp;quot;identifying_string&amp;quot;'': The optional ''for'' attribute of the label tag associates the label with what we'll call an &amp;quot;identifying string&amp;quot;, which should match the string specified in the corresponding ''id'' portion of the input tag. If both of these attributes are in place, then upon clicking on the label, the corresponding input will be selected.&lt;br /&gt;
&lt;br /&gt;
info.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 echo  &amp;quot;Personal Data:&amp;quot;, $_POST['firstname'], &amp;quot; &amp;quot;, $_POST['lastname'], &amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 $birthyear=$_POST['birthyear'];&lt;br /&gt;
 echo  &amp;quot;Birth year:&amp;quot;, $_birthyear, &amp;quot; (Age:&amp;quot; , 2007-$birthyear, &amp;quot;)&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 echo &amp;quot;Gender: &amp;quot;, $_POST['gender'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For further help with using HTML forms with PHP, we recommend [http://www.tizag.com/phpT/forms.php this tutorial]&lt;br /&gt;
===Self-Submitting Forms===&lt;br /&gt;
Oftentimes we don't want to pass form values to a different page, but rather to the same page as contains the form so that we can process the information and display output on that same page. The way we do this is to specify that the form should submit to $_SERVER['PHP_SELF'] and not say, other_page.php.&lt;br /&gt;
Note that you can make self-submitting forms that use either the get or post method.&lt;br /&gt;
&lt;br /&gt;
The following example simply takes the data input from a form and, via posting, echoes it back to the user as bold text on the same page:&lt;br /&gt;
bold_printer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Bold Printer&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action= &amp;lt;?php echo $_SERVER['PHP_SELF'] ?&amp;gt; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;label for=&amp;quot;text_field&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;user_text&amp;quot; id=&amp;quot;text_field&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Print in Bold&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    if(isset($_POST['user_text']))&lt;br /&gt;
    echo &amp;quot;&amp;lt;b&amp;gt;&amp;quot;, $_POST['user_text'], &amp;quot;&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Session Variables==&lt;br /&gt;
PHP provides a global array, ''$_SESSION'', for values that are available globally throughout a ''session''. The values will stay there until either you remove them in a PHP script, the user's browser is closed, or their session expires (you can tune the timeout period in the PHP configuration files).&lt;br /&gt;
&lt;br /&gt;
In order to access session values, you should first initiate the session with the ''session_start()'' function call. After that, when you access a values with ''$_SESSION'', it will become a session values. For example:&lt;br /&gt;
&lt;br /&gt;
setsessionvars.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$_SESSION['SESSION']=42;&lt;br /&gt;
$_SESSION['username']=&amp;quot;ArthurDent&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This sets two variables, ''SESSION'' and ''username'' to be accessible by other pages in this session. In another page, you can access them like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
if (!isset($_SESSION['SESSION'])) {&lt;br /&gt;
  echo &amp;quot;error session is not registered&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else echo &amp;quot;username=&amp;quot;,$_SESSION['username'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also remove a session variable from the session with unset command:&lt;br /&gt;
&lt;br /&gt;
 unset($_SESSION['SESSION']);&lt;br /&gt;
&lt;br /&gt;
==Redirect the browser to a different page==&lt;br /&gt;
&lt;br /&gt;
It is often useful to redirect the user's browser to a different webpage, for example, after processing inputs from a form.  Here is a simple example that shows how to do this, based on whether or not the ''LOGIN'' session variable is set:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
if(isset($_SESSION[&amp;quot;LOGIN&amp;quot;])){&lt;br /&gt;
	Header(&amp;quot;Location:/welcome.php&amp;quot;);&lt;br /&gt;
}else{&lt;br /&gt;
	Header(&amp;quot;Location:/login.php&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that anytime you call the ''Header'' function, it is adding information to the header section in the HTML object that is being requested.  As such, all ''Header'' calls should go before any other HTML output in the PHP script.&lt;br /&gt;
&lt;br /&gt;
==Sending a file to the Browser==&lt;br /&gt;
&lt;br /&gt;
Another useful function to know is the ''readfile'' function.  It takes a file on the webserver's file system and sends it to the web browser.  A simple example is given below, but note that you'll probably need to specify addition header fields in order to have the web browser download the file correctly. &lt;br /&gt;
For those of you who have used previous versions of PHP, note that mime_content_type($path) is now deprecated, use finfo_file instead&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$file=$_GET['file'];&lt;br /&gt;
$name = $_SESSION[&amp;quot;UNAME&amp;quot;];&lt;br /&gt;
$path=&amp;quot;/home/uploads/$name/$file&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension&lt;br /&gt;
if (!$finfo) {&lt;br /&gt;
 	echo &amp;quot;Opening fileinfo database failed&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$mime=finfo_file($finfo, $path);&lt;br /&gt;
finfo_close($finfo);&lt;br /&gt;
&lt;br /&gt;
header('content-type:'. $mime);&lt;br /&gt;
readfile($path);    &lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uploading files with PHP==&lt;br /&gt;
&lt;br /&gt;
This section describes one way to upload files with PHP.  First, you need an HTML form that contains an input with type ''file'':&lt;br /&gt;
&lt;br /&gt;
transfer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;uploader.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
Choose a file to upload: &amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you use a ''file'' input in a form, PHP automatically adds an entry in the ''$_FILES'' array with information about the file. For example, transfer.php called the file ''uploadedfile'', so $_FILES['uploadedfile'] refers to file that the user selected in the form.  The name of the original file is $_FILES['uploadedfile']['name'].   When you upload a file, it is put in a temporary location by the web server. This temporary name is $_FILES['uploadedfile']['tmp_name']. In PHP, you can move a file from its original location to a new location by calling a built-in function, ''move_uploaded_file'', which takes an argument for the source (temporary location) and destination location. Note that relative paths in the destination location are relative to the location of the PHP script being executed.&lt;br /&gt;
&lt;br /&gt;
uploader.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$target_path = &amp;quot;uploads/&amp;quot;;&lt;br /&gt;
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);&lt;br /&gt;
&lt;br /&gt;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {&lt;br /&gt;
    echo &amp;quot;The file &amp;quot;.  basename( $_FILES['uploadedfile']['name']) . &amp;quot; has been uploaded&amp;quot;;&lt;br /&gt;
} else{&lt;br /&gt;
    echo &amp;quot;There was an error uploading the file, please try again!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Eclipse ==&lt;br /&gt;
&lt;br /&gt;
For those of you who like programming with IDEs, Eclipse has support for PHP.  See:&lt;br /&gt;
http://www.eclipse.org.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=236</id>
		<title>PHP Original</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=236"/>
		<updated>2010-07-21T20:08:49Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Strings: Differences Between Java and PHP */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= PHP5 =&lt;br /&gt;
PHP is a server-side language. When a web request for PHP file comes in, the web server processes the PHP file to produce HTML output.  That is, the main function of most PHP scripts is to dynamically create HTML content.  PHP5 support for Apache is provided by mod_php5. In Ubuntu, you can provide PHP5 support by installing ''php5''. This will install the apache2 module and the php5 command line interpreter (useful for debugging). PHP files may have different extensions, but ''.php'' is the most common.  In the Apache configuration, you can specify which extensions are going to be treated as PHP scripts by adding the extensions to the list in the ''/etc/httpd/conf.d/php.conf'' file: &lt;br /&gt;
&lt;br /&gt;
  AddType text/html .php .phtml .php3&lt;br /&gt;
&lt;br /&gt;
==Declaring PHP==&lt;br /&gt;
If a PHP file is requested by a web user, Apache parses and interprets the file.  Any code declared between PHP tags is then executed and the total HTML output is sent back to the user. PHP code can be declared in a script file in any of the following ways, with the first being the most common:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 php?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script language=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, edit a new PHP file named ''info.php'', and put these lines in the file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Put the file in the web directory for your user account (remember that this is ''~username/.html/'' if you completed the first Module).  Then access the web page by going to ''http://server/~username/info.php''. You will see something similar to the picture below:&lt;br /&gt;
&lt;br /&gt;
[[Image:Php_info.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you look at the HTML code, you will see quite a lot of HTML output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;DTD/xhtml1-transitional.dtd&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&lt;br /&gt;
 body {background-color: #ffffff; color: #000000;}&lt;br /&gt;
 body, td, th, h1, h2 {font-family: sans-serif;}&lt;br /&gt;
 pre {margin: 0px; font-family: monospace;}&lt;br /&gt;
 a:link {color: #000099; text-decoration: none; background-color: #ffffff;}&lt;br /&gt;
 a:hover {text-decoration: underline;}&lt;br /&gt;
 table {border-collapse: collapse;}&lt;br /&gt;
 .center {text-align: center;}&lt;br /&gt;
 .center table { margin-left: auto; margin-right: auto; text-align: left;}&lt;br /&gt;
 .center th { text-align: center !important; }&lt;br /&gt;
 td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}&lt;br /&gt;
 h1 {font-size: 150%;}&lt;br /&gt;
 h2 {font-size: 125%;}&lt;br /&gt;
 .p {text-align: left;}&lt;br /&gt;
 .e {background-color: #ccccff; font-weight: bold; color: #000000;}&lt;br /&gt;
 .h {background-color: #9999cc; font-weight: bold; color: #000000;}&lt;br /&gt;
 .v {background-color: #cccccc; color: #000000;}&lt;br /&gt;
 .vr {background-color: #cccccc; text-align: right; color: #000000;}&lt;br /&gt;
 img {float: right; border: 0px;}&lt;br /&gt;
 hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}&lt;br /&gt;
 &amp;lt;/style&amp;gt;&lt;br /&gt;
 &amp;lt;title&amp;gt;phpinfo()&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
 &amp;lt;body&amp;gt;&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;table border=&amp;quot;0&amp;quot; cellpadding=&amp;quot;3&amp;quot; width=&amp;quot;600&amp;quot;&amp;gt;&lt;br /&gt;
 ....&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, none of that HTML was in the ''info.php'' file.  Instead, the ''phpinfo()'' line is a built-in PHP function that produces all of that HTML.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Variables require ''$'' at the beginning of their names (regardless of the purpose, be it setting, be it accessing). You don't need to specify the type of the variable; PHP will use the variable as the correct type based on how it is used in the code.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   #this is an integer&lt;br /&gt;
   $i=5;&lt;br /&gt;
   #this is a string&lt;br /&gt;
   $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in many other languages, statements end with a '';''.  To actually produce HTML output, you simply print the HTML from the PHP code.  The simplest way is to use the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
If you use a variable inside of a string, the value of the variable will be put in the string (you might also look at the ''echo'' function):&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   $i=5;&lt;br /&gt;
   print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will generate the string&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
If you create a file with just the above code, you will notice that your browser will only receive the above line. That is, there is no ''html'' tag or anything else. Thus, PHP files are normally a mix of HTML code and PHP code.&lt;br /&gt;
Most PHP scripts, then, look more like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code that the browser will receive is then:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  The value of i is 5&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can have any number of PHP segments and HTML segments in a PHP file.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;? $i=5; ?&amp;gt;&lt;br /&gt;
The value of i is &amp;lt;?  print &amp;quot;$i&amp;quot;; ?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, whatever you print out will be interpreted as HTML by the client's browser, so you can (and will) use HTML tags in the output you produce.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;? print &amp;quot;This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt; ?&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in the following at the user's browser:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which would then render ''test'' in bold.&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
&lt;br /&gt;
Declaring arrays is very easy in PHP. Either you can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
$student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
$student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
$student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or alternatively&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student=array(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you can access the contents with an index into the array, as normal:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find the size of an array with ''count'' function, e.g., ''count($student)''&lt;br /&gt;
&lt;br /&gt;
In PHP, you can also have associative arrays, which are arrays whose members can be accessible with string indices. Here is an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
$test[&amp;quot;test1&amp;quot;]=1;&lt;br /&gt;
$test[&amp;quot;test2&amp;quot;]=2;&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;test1=&amp;quot;,$test[test1],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
echo &amp;quot;test2=&amp;quot;,$test[test2],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access array keys through ''array_keys'' function.&lt;br /&gt;
===Strings: Differences Between Java and PHP===&lt;br /&gt;
&lt;br /&gt;
In php, unlike in java, you can enclose strings in single quotes or double quotes&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
The following is a valid string, just like in java.&lt;br /&gt;
 //A valid string in php and in java.&lt;br /&gt;
 $str=&amp;quot;hey jeff!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And in PHP, the following is also a valid string&lt;br /&gt;
 //A valid string in PHP, but invalid in java.&lt;br /&gt;
 $str='hey jeff!' &lt;br /&gt;
&lt;br /&gt;
However, there is a caveat when using the single quotes. If we want to then use MORE single quotes within the string We have to escape them&lt;br /&gt;
To yield the string&lt;br /&gt;
''I'm Jeff's string''&lt;br /&gt;
&lt;br /&gt;
we could use either of the following definitions:&lt;br /&gt;
&lt;br /&gt;
 $str=&amp;quot;I'm Jeff's string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
 $str='I\'m Jeff\'s string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if/elseif/else'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   some statements&lt;br /&gt;
 }&lt;br /&gt;
 elseif( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   more statements&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
    yet more statements&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string == &amp;quot;this is my string&amp;quot;) {&lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such thing as global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
PHP has ''for'' and ''while'' loops, similar to Java and C:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, the following code will print the sum of numbers from 1 to 10.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 echo &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The advantage of loops  becomes more obvious when you have to create a long table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Number&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Square&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
for($i=0;$i&amp;lt;100;$i++) {&lt;br /&gt;
echo &amp;quot;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;$i &amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;quot;,$i*$i,&amp;quot;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a table with two columns, each row contains a number and its square. &lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt normal control flow through a loop.  ''break'' jumps out of the loop entirely, while ''continue'' jumps to the end of the current loop iteration and flow resumes with the next loop iteration.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
PHP functions are identified with the keyword ''function'' followed by a function name and argument list for the function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
function name($arg1 [= constant], $arg2 [= constant], ...) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   function add($x,$y) {&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
   }&lt;br /&gt;
   echo &amp;quot;Sum of 2+5=&amp;quot;,add(2,5); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If an argument has a default value, it can be specified with the argument on the list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
      print &amp;quot;hello $name how are you today?&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
   helloMsg(&amp;quot;Alice&amp;quot;);&lt;br /&gt;
   helloMsg();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 hello Alice how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
 hello there how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Passing Variables ==&lt;br /&gt;
Input values can be passed to a script either through the URL request line or through data submitted by a form. Built in arrays $_GET['var'] and $_POST['var'] return the value of ''var'' depending on the access method. As we will see in the next section, values can also be passed through session variables. Finally, you can use cookies to store and retrieve values.&lt;br /&gt;
&lt;br /&gt;
===Get: Passing Variables via URL===&lt;br /&gt;
If you want to pass values in a URL, the format is ''http://yourserver/yourphpfile.php?var1=value1&amp;amp;var2=value2.....''&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code would be used to print the previous hello message according to a name given in the URL:&lt;br /&gt;
&lt;br /&gt;
greeting.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $person= $_GET['name'];&lt;br /&gt;
 helloMsg($person);&lt;br /&gt;
 function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
     print &amp;quot;hello $name how are you today?&amp;lt;/br&amp;gt;&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you access the URL like this:  ''greeting.php?name=Alice'', it will show ''hello Alice how are you today?''. However, if you access it with ''greeting.php'', it will print ''hello how are you today?''. Note that it does not use the default value ''there'' since ''$name'' is now a string (in this case, an empty string) and so there is argument being being passed to ''helloMsg''.&lt;br /&gt;
&lt;br /&gt;
===POST: Passing Variables via Form===&lt;br /&gt;
In PHP one method for passing variables from page to page is by sending it to the other page via posting. The way post works in&lt;br /&gt;
PHP is that PHP retrieves the variable names and values from a (slightly modified) html form and sends an those variables in the associative array named $_POST to the page specified in the FORM tag's ''action'' attribute. &lt;br /&gt;
&lt;br /&gt;
Note that receiving the data from a form is very similar to getting it from the URL. Consider the following form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;FORM action=&amp;quot;info.php&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;P&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;firstnamelabel&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;firstname&amp;quot; id=&amp;quot;firstnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;lastnamelabel&amp;quot;&amp;gt;Last name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;lastname&amp;quot; id=&amp;quot;lastnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;birthyearlabel&amp;quot;&amp;gt;Birth Year: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;birthyear&amp;quot; id=&amp;quot;birthyearlabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Male&amp;quot;&amp;gt; Male&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Female&amp;quot;&amp;gt; Female&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;submit&amp;quot; value=&amp;quot;Send&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;reset&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;/P&amp;gt;&lt;br /&gt;
&amp;lt;/FORM&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This posts the form data to ''info.php'', with inputs ''firstname'', ''lastname'', ''birthyear'', and ''gender''.&lt;br /&gt;
The values passed through the form for each input can be accessed in ''info.php'' with the $_POST array.&lt;br /&gt;
*&amp;lt;label&amp;gt; : The label tag just provides a text label for the form inputs.&lt;br /&gt;
*&amp;lt;input ''type=&amp;quot;input_type_here&amp;quot;''&amp;gt; : The input tag provides various means of inputting information into the form, there are various types of inputs such as text, radio, submit, reset, etc. &lt;br /&gt;
*&amp;lt;input ... ''name=&amp;quot;input_name&amp;quot;''&amp;gt; : &lt;br /&gt;
*&amp;lt;label ''for=&amp;quot;identifying_string&amp;quot;''&amp;gt; and &amp;lt;INPUT ... ''id=&amp;quot;identifying_string&amp;quot;'': The optional ''for'' attribute of the label tag associates the label with what we'll call an &amp;quot;identifying string&amp;quot;, which should match the string specified in the corresponding ''id'' portion of the input tag. If both of these attributes are in place, then upon clicking on the label, the corresponding input will be selected.&lt;br /&gt;
&lt;br /&gt;
info.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 echo  &amp;quot;Personal Data:&amp;quot;, $_POST['firstname'], &amp;quot; &amp;quot;, $_POST['lastname'], &amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 $birthyear=$_POST['birthyear'];&lt;br /&gt;
 echo  &amp;quot;Birth year:&amp;quot;, $_birthyear, &amp;quot; (Age:&amp;quot; , 2007-$birthyear, &amp;quot;)&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 echo &amp;quot;Gender: &amp;quot;, $_POST['gender'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For further help with using HTML forms with PHP, we recommend [http://www.tizag.com/phpT/forms.php this tutorial]&lt;br /&gt;
===Self-Submitting Forms===&lt;br /&gt;
Oftentimes we don't want to pass form values to a different page, but rather to the same page as contains the form so that we can process the information and display output on that same page. The way we do this is to specify that the form should submit to $_SERVER['PHP_SELF'] and not say, other_page.php.&lt;br /&gt;
Note that you can make self-submitting forms that use either the get or post method.&lt;br /&gt;
&lt;br /&gt;
The following example simply takes the data input from a form and, via posting, echoes it back to the user as bold text on the same page:&lt;br /&gt;
bold_printer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Bold Printer&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action= &amp;lt;?php echo $_SERVER['PHP_SELF'] ?&amp;gt; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;label for=&amp;quot;text_field&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;user_text&amp;quot; id=&amp;quot;text_field&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Print in Bold&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    if(isset($_POST['user_text'])&lt;br /&gt;
    echo &amp;quot;&amp;lt;b&amp;gt;&amp;quot;, $_POST['user_text'], &amp;quot;&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Session Variables==&lt;br /&gt;
PHP provides a global array, ''$_SESSION'', for values that are available globally throughout a ''session''. The values will stay there until either you remove them in a PHP script, the user's browser is closed, or their session expires (you can tune the timeout period in the PHP configuration files).&lt;br /&gt;
&lt;br /&gt;
In order to access session values, you should first initiate the session with the ''session_start()'' function call. After that, when you access a values with ''$_SESSION'', it will become a session values. For example:&lt;br /&gt;
&lt;br /&gt;
setsessionvars.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$_SESSION['SESSION']=42;&lt;br /&gt;
$_SESSION['username']=&amp;quot;ArthurDent&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This sets two variables, ''SESSION'' and ''username'' to be accessible by other pages in this session. In another page, you can access them like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
if (!isset($_SESSION['SESSION'])) {&lt;br /&gt;
  echo &amp;quot;error session is not registered&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else echo &amp;quot;username=&amp;quot;,$_SESSION['username'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also remove a session variable from the session with unset command:&lt;br /&gt;
&lt;br /&gt;
 unset($_SESSION['SESSION']);&lt;br /&gt;
&lt;br /&gt;
==Redirect the browser to a different page==&lt;br /&gt;
&lt;br /&gt;
It is often useful to redirect the user's browser to a different webpage, for example, after processing inputs from a form.  Here is a simple example that shows how to do this, based on whether or not the ''LOGIN'' session variable is set:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
if(isset($_SESSION[&amp;quot;LOGIN&amp;quot;])){&lt;br /&gt;
	Header(&amp;quot;Location:/welcome.php&amp;quot;);&lt;br /&gt;
}else{&lt;br /&gt;
	Header(&amp;quot;Location:/login.php&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that anytime you call the ''Header'' function, it is adding information to the header section in the HTML object that is being requested.  As such, all ''Header'' calls should go before any other HTML output in the PHP script.&lt;br /&gt;
&lt;br /&gt;
==Sending a file to the Browser==&lt;br /&gt;
&lt;br /&gt;
Another useful function to know is the ''readfile'' function.  It takes a file on the webserver's file system and sends it to the web browser.  A simple example is given below, but note that you'll probably need to specify addition header fields in order to have the web browser download the file correctly. &lt;br /&gt;
For those of you who have used previous versions of PHP, note that mime_content_type($path) is now deprecated, use finfo_file instead&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$file=$_GET['file'];&lt;br /&gt;
$name = $_SESSION[&amp;quot;UNAME&amp;quot;];&lt;br /&gt;
$path=&amp;quot;/home/uploads/$name/$file&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension&lt;br /&gt;
if (!$finfo) {&lt;br /&gt;
 	echo &amp;quot;Opening fileinfo database failed&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$mime=finfo_file($finfo, $path);&lt;br /&gt;
finfo_close($finfo);&lt;br /&gt;
&lt;br /&gt;
header('content-type:'. $mime);&lt;br /&gt;
readfile($path);    &lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uploading files with PHP==&lt;br /&gt;
&lt;br /&gt;
This section describes one way to upload files with PHP.  First, you need an HTML form that contains an input with type ''file'':&lt;br /&gt;
&lt;br /&gt;
transfer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;uploader.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
Choose a file to upload: &amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you use a ''file'' input in a form, PHP automatically adds an entry in the ''$_FILES'' array with information about the file. For example, transfer.php called the file ''uploadedfile'', so $_FILES['uploadedfile'] refers to file that the user selected in the form.  The name of the original file is $_FILES['uploadedfile']['name'].   When you upload a file, it is put in a temporary location by the web server. This temporary name is $_FILES['uploadedfile']['tmp_name']. In PHP, you can move a file from its original location to a new location by calling a built-in function, ''move_uploaded_file'', which takes an argument for the source (temporary location) and destination location. Note that relative paths in the destination location are relative to the location of the PHP script being executed.&lt;br /&gt;
&lt;br /&gt;
uploader.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$target_path = &amp;quot;uploads/&amp;quot;;&lt;br /&gt;
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);&lt;br /&gt;
&lt;br /&gt;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {&lt;br /&gt;
    echo &amp;quot;The file &amp;quot;.  basename( $_FILES['uploadedfile']['name']) . &amp;quot; has been uploaded&amp;quot;;&lt;br /&gt;
} else{&lt;br /&gt;
    echo &amp;quot;There was an error uploading the file, please try again!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Eclipse ==&lt;br /&gt;
&lt;br /&gt;
For those of you who like programming with IDEs, Eclipse has support for PHP.  See:&lt;br /&gt;
http://www.eclipse.org.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=235</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=235"/>
		<updated>2010-07-21T15:56:37Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Installing MySQL */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig --add mysqld&lt;br /&gt;
 sudo /sbin/chkconfig mysqld on&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is the following: &lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to USERNAME@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER USER_NAME@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to boss@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO david@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO TODD@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
&lt;br /&gt;
===Granting the Ability to Grant: With Grant Option===&lt;br /&gt;
Additionally, in order to allow the user to grant other users privileges you must add ''WITH GRANT OPTION'' to your grant statement. For example:&lt;br /&gt;
 GRANT SELECT on powerplant.reactions, TO homer@'powerplant.springfield.gov' WITH GRANT OPTION;&lt;br /&gt;
The above command not only allows user ''homer@powerplant.springfield.gov'' to ''SELECT'' from the powerplant database's reactions table, but also allows him to grant other users the ability to select on those databases.&lt;br /&gt;
&lt;br /&gt;
i.e. Homer can issue the following command:&lt;br /&gt;
 GRANT SELECT ON powerplant.reactions TO moe@'moestavern';&lt;br /&gt;
&lt;br /&gt;
===A Note on Privileges===&lt;br /&gt;
Privileges in MySQL are highly flexible in terms of specificity, that is to say that you can grant a user specific privileges such as SELECT on a database, table, or column level. Note that only the SELECT, INSERT, and UPDATE privileges are available at the column level, if this seems confusing to you, take a moment to think about why this might be. For instance, why shouldn't there exist a column-level DROP privilege?&lt;br /&gt;
&lt;br /&gt;
The flexibility allows a database administrator (you) to give other users access only to the minimal level of privileges that they require, in accordance with the [http://en.wikipedia.org/wiki/Principle_of_least_privilege principal of least privilege].&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it. For this Command to work, you must issue the following command to grant the FILE privilege to your user&lt;br /&gt;
 GRANT FILE on *.* to 'user'@'host';&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword. When loading data from the local machine you do not need to grant your user the FILE privilege, think about why this might be the case.&lt;br /&gt;
Hint: what is going on behind the scenes when you load the data in from a local machine when your database is hosted on a different machine? &lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt'&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.w3schools.com/sql/sql_join.asp here] for a good explanation of how to use joins (note, see the next 4 chapters for explanations of inner, left, right and full joins).&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys]&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=234</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=234"/>
		<updated>2010-07-20T20:26:09Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Joins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig mysqld&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is the following: &lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to USERNAME@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER USER_NAME@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to boss@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO david@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO TODD@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
&lt;br /&gt;
===Granting the Ability to Grant: With Grant Option===&lt;br /&gt;
Additionally, in order to allow the user to grant other users privileges you must add ''WITH GRANT OPTION'' to your grant statement. For example:&lt;br /&gt;
 GRANT SELECT on powerplant.reactions, TO homer@'powerplant.springfield.gov' WITH GRANT OPTION;&lt;br /&gt;
The above command not only allows user ''homer@powerplant.springfield.gov'' to ''SELECT'' from the powerplant database's reactions table, but also allows him to grant other users the ability to select on those databases.&lt;br /&gt;
&lt;br /&gt;
i.e. Homer can issue the following command:&lt;br /&gt;
 GRANT SELECT ON powerplant.reactions TO moe@'moestavern';&lt;br /&gt;
&lt;br /&gt;
===A Note on Privileges===&lt;br /&gt;
Privileges in MySQL are highly flexible in terms of specificity, that is to say that you can grant a user specific privileges such as SELECT on a database, table, or column level. Note that only the SELECT, INSERT, and UPDATE privileges are available at the column level, if this seems confusing to you, take a moment to think about why this might be. For instance, why shouldn't there exist a column-level DROP privilege?&lt;br /&gt;
&lt;br /&gt;
The flexibility allows a database administrator (you) to give other users access only to the minimal level of privileges that they require, in accordance with the [http://en.wikipedia.org/wiki/Principle_of_least_privilege principal of least privilege].&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it. For this Command to work, you must issue the following command to grant the FILE privilege to your user&lt;br /&gt;
 GRANT FILE on *.* to 'user'@'host';&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword. When loading data from the local machine you do not need to grant your user the FILE privilege, think about why this might be the case.&lt;br /&gt;
Hint: what is going on behind the scenes when you load the data in from a local machine when your database is hosted on a different machine? &lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt'&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.w3schools.com/sql/sql_join.asp here] for a good explanation of how to use joins (note, see the next 4 chapters for explanations of inner, left, right and full joins).&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys]&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=233</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=233"/>
		<updated>2010-07-20T16:08:00Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Loading Data From a File */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig mysqld&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is the following: &lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to USERNAME@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER USER_NAME@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to boss@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO david@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO TODD@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
&lt;br /&gt;
===Granting the Ability to Grant: With Grant Option===&lt;br /&gt;
Additionally, in order to allow the user to grant other users privileges you must add ''WITH GRANT OPTION'' to your grant statement. For example:&lt;br /&gt;
 GRANT SELECT on powerplant.reactions, TO homer@'powerplant.springfield.gov' WITH GRANT OPTION;&lt;br /&gt;
The above command not only allows user ''homer@powerplant.springfield.gov'' to ''SELECT'' from the powerplant database's reactions table, but also allows him to grant other users the ability to select on those databases.&lt;br /&gt;
&lt;br /&gt;
i.e. Homer can issue the following command:&lt;br /&gt;
 GRANT SELECT ON powerplant.reactions TO moe@'moestavern';&lt;br /&gt;
&lt;br /&gt;
===A Note on Privileges===&lt;br /&gt;
Privileges in MySQL are highly flexible in terms of specificity, that is to say that you can grant a user specific privileges such as SELECT on a database, table, or column level. Note that only the SELECT, INSERT, and UPDATE privileges are available at the column level, if this seems confusing to you, take a moment to think about why this might be. For instance, why shouldn't there exist a column-level DROP privilege?&lt;br /&gt;
&lt;br /&gt;
The flexibility allows a database administrator (you) to give other users access only to the minimal level of privileges that they require, in accordance with the [http://en.wikipedia.org/wiki/Principle_of_least_privilege principal of least privilege].&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it. For this Command to work, you must issue the following command to grant the FILE privilege to your user&lt;br /&gt;
 GRANT FILE on *.* to 'user'@'host';&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword. When loading data from the local machine you do not need to grant your user the FILE privilege, think about why this might be the case.&lt;br /&gt;
Hint: what is going on behind the scenes when you load the data in from a local machine when your database is hosted on a different machine? &lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt'&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.webdesign.org/web-programming/php/mysql-join-tutorial.14876.html here] for a good explanation of how to use joins.&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys]&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=232</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=232"/>
		<updated>2010-07-20T15:50:30Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Creating Users */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig mysqld&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is the following: &lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to USERNAME@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER USER_NAME@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to boss@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO david@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO TODD@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
&lt;br /&gt;
===Granting the Ability to Grant: With Grant Option===&lt;br /&gt;
Additionally, in order to allow the user to grant other users privileges you must add ''WITH GRANT OPTION'' to your grant statement. For example:&lt;br /&gt;
 GRANT SELECT on powerplant.reactions, TO homer@'powerplant.springfield.gov' WITH GRANT OPTION;&lt;br /&gt;
The above command not only allows user ''homer@powerplant.springfield.gov'' to ''SELECT'' from the powerplant database's reactions table, but also allows him to grant other users the ability to select on those databases.&lt;br /&gt;
&lt;br /&gt;
i.e. Homer can issue the following command:&lt;br /&gt;
 GRANT SELECT ON powerplant.reactions TO moe@'moestavern';&lt;br /&gt;
&lt;br /&gt;
===A Note on Privileges===&lt;br /&gt;
Privileges in MySQL are highly flexible in terms of specificity, that is to say that you can grant a user specific privileges such as SELECT on a database, table, or column level. Note that only the SELECT, INSERT, and UPDATE privileges are available at the column level, if this seems confusing to you, take a moment to think about why this might be. For instance, why shouldn't there exist a column-level DROP privilege?&lt;br /&gt;
&lt;br /&gt;
The flexibility allows a database administrator (you) to give other users access only to the minimal level of privileges that they require, in accordance with the [http://en.wikipedia.org/wiki/Principle_of_least_privilege principal of least privilege].&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it.&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword.&lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt'&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.webdesign.org/web-programming/php/mysql-join-tutorial.14876.html here] for a good explanation of how to use joins.&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys]&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=231</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=231"/>
		<updated>2010-07-20T15:49:54Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Granting the Ability to Grant: With Grant Option */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig mysqld&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is the following: &lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER 'USER_NAME'@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO david@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO TODD@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
&lt;br /&gt;
===Granting the Ability to Grant: With Grant Option===&lt;br /&gt;
Additionally, in order to allow the user to grant other users privileges you must add ''WITH GRANT OPTION'' to your grant statement. For example:&lt;br /&gt;
 GRANT SELECT on powerplant.reactions, TO homer@'powerplant.springfield.gov' WITH GRANT OPTION;&lt;br /&gt;
The above command not only allows user ''homer@powerplant.springfield.gov'' to ''SELECT'' from the powerplant database's reactions table, but also allows him to grant other users the ability to select on those databases.&lt;br /&gt;
&lt;br /&gt;
i.e. Homer can issue the following command:&lt;br /&gt;
 GRANT SELECT ON powerplant.reactions TO moe@'moestavern';&lt;br /&gt;
&lt;br /&gt;
===A Note on Privileges===&lt;br /&gt;
Privileges in MySQL are highly flexible in terms of specificity, that is to say that you can grant a user specific privileges such as SELECT on a database, table, or column level. Note that only the SELECT, INSERT, and UPDATE privileges are available at the column level, if this seems confusing to you, take a moment to think about why this might be. For instance, why shouldn't there exist a column-level DROP privilege?&lt;br /&gt;
&lt;br /&gt;
The flexibility allows a database administrator (you) to give other users access only to the minimal level of privileges that they require, in accordance with the [http://en.wikipedia.org/wiki/Principle_of_least_privilege principal of least privilege].&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it.&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword.&lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt'&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.webdesign.org/web-programming/php/mysql-join-tutorial.14876.html here] for a good explanation of how to use joins.&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys]&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=230</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=230"/>
		<updated>2010-07-20T15:49:41Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Granting the Ability to Grant: With Grant Option */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig mysqld&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is the following: &lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER 'USER_NAME'@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO david@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO TODD@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
&lt;br /&gt;
===Granting the Ability to Grant: With Grant Option===&lt;br /&gt;
Additionally, in order to allow the user to grant other users privileges you must add ''WITH GRANT OPTION'' to your grant statement. For example:&lt;br /&gt;
 GRANT SELECT on powerplant.reactions, TO 'homer'@'powerplant.springfield.gov' WITH GRANT OPTION;&lt;br /&gt;
The above command not only allows user ''homer@powerplant.springfield.gov'' to ''SELECT'' from the powerplant database's reactions table, but also allows him to grant other users the ability to select on those databases.&lt;br /&gt;
&lt;br /&gt;
i.e. Homer can issue the following command:&lt;br /&gt;
 GRANT SELECT ON powerplant.reactions TO moe@'moestavern';&lt;br /&gt;
&lt;br /&gt;
===A Note on Privileges===&lt;br /&gt;
Privileges in MySQL are highly flexible in terms of specificity, that is to say that you can grant a user specific privileges such as SELECT on a database, table, or column level. Note that only the SELECT, INSERT, and UPDATE privileges are available at the column level, if this seems confusing to you, take a moment to think about why this might be. For instance, why shouldn't there exist a column-level DROP privilege?&lt;br /&gt;
&lt;br /&gt;
The flexibility allows a database administrator (you) to give other users access only to the minimal level of privileges that they require, in accordance with the [http://en.wikipedia.org/wiki/Principle_of_least_privilege principal of least privilege].&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it.&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword.&lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt'&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.webdesign.org/web-programming/php/mysql-join-tutorial.14876.html here] for a good explanation of how to use joins.&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys]&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=229</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=229"/>
		<updated>2010-07-20T15:49:30Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Granting Privileges to Existing Users */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig mysqld&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is the following: &lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER 'USER_NAME'@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO david@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO TODD@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
&lt;br /&gt;
===Granting the Ability to Grant: With Grant Option===&lt;br /&gt;
Additionally, in order to allow the user to grant other users privileges you must add ''WITH GRANT OPTION'' to your grant statement. For example:&lt;br /&gt;
 GRANT SELECT on powerplant.reactions, TO 'homer'@'powerplant.springfield.gov' WITH GRANT OPTION;&lt;br /&gt;
The above command not only allows user ''homer@powerplant.springfield.gov'' to ''SELECT'' from the powerplant database's reactions table, but also allows him to grant other users the ability to select on those databases.&lt;br /&gt;
&lt;br /&gt;
i.e. Homer can issue the following command:&lt;br /&gt;
 GRANT SELECT ON powerplant.reactions TO 'moe'@'moestavern';&lt;br /&gt;
&lt;br /&gt;
===A Note on Privileges===&lt;br /&gt;
Privileges in MySQL are highly flexible in terms of specificity, that is to say that you can grant a user specific privileges such as SELECT on a database, table, or column level. Note that only the SELECT, INSERT, and UPDATE privileges are available at the column level, if this seems confusing to you, take a moment to think about why this might be. For instance, why shouldn't there exist a column-level DROP privilege?&lt;br /&gt;
&lt;br /&gt;
The flexibility allows a database administrator (you) to give other users access only to the minimal level of privileges that they require, in accordance with the [http://en.wikipedia.org/wiki/Principle_of_least_privilege principal of least privilege].&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it.&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword.&lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt'&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.webdesign.org/web-programming/php/mysql-join-tutorial.14876.html here] for a good explanation of how to use joins.&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys]&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=228</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=228"/>
		<updated>2010-07-20T15:49:16Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Granting Privileges to Existing Users */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig mysqld&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is the following: &lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER 'USER_NAME'@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO david@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO 'TODD'@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
&lt;br /&gt;
===Granting the Ability to Grant: With Grant Option===&lt;br /&gt;
Additionally, in order to allow the user to grant other users privileges you must add ''WITH GRANT OPTION'' to your grant statement. For example:&lt;br /&gt;
 GRANT SELECT on powerplant.reactions, TO 'homer'@'powerplant.springfield.gov' WITH GRANT OPTION;&lt;br /&gt;
The above command not only allows user ''homer@powerplant.springfield.gov'' to ''SELECT'' from the powerplant database's reactions table, but also allows him to grant other users the ability to select on those databases.&lt;br /&gt;
&lt;br /&gt;
i.e. Homer can issue the following command:&lt;br /&gt;
 GRANT SELECT ON powerplant.reactions TO 'moe'@'moestavern';&lt;br /&gt;
&lt;br /&gt;
===A Note on Privileges===&lt;br /&gt;
Privileges in MySQL are highly flexible in terms of specificity, that is to say that you can grant a user specific privileges such as SELECT on a database, table, or column level. Note that only the SELECT, INSERT, and UPDATE privileges are available at the column level, if this seems confusing to you, take a moment to think about why this might be. For instance, why shouldn't there exist a column-level DROP privilege?&lt;br /&gt;
&lt;br /&gt;
The flexibility allows a database administrator (you) to give other users access only to the minimal level of privileges that they require, in accordance with the [http://en.wikipedia.org/wiki/Principle_of_least_privilege principal of least privilege].&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it.&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword.&lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt'&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.webdesign.org/web-programming/php/mysql-join-tutorial.14876.html here] for a good explanation of how to use joins.&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys]&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=227</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=227"/>
		<updated>2010-07-19T21:44:49Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Granting the Ability to Grant: With Grant Option */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig mysqld&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is the following: &lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER 'USER_NAME'@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO 'david'@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO 'TODD'@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
&lt;br /&gt;
===Granting the Ability to Grant: With Grant Option===&lt;br /&gt;
Additionally, in order to allow the user to grant other users privileges you must add ''WITH GRANT OPTION'' to your grant statement. For example:&lt;br /&gt;
 GRANT SELECT on powerplant.reactions, TO 'homer'@'powerplant.springfield.gov' WITH GRANT OPTION;&lt;br /&gt;
The above command not only allows user ''homer@powerplant.springfield.gov'' to ''SELECT'' from the powerplant database's reactions table, but also allows him to grant other users the ability to select on those databases.&lt;br /&gt;
&lt;br /&gt;
i.e. Homer can issue the following command:&lt;br /&gt;
 GRANT SELECT ON powerplant.reactions TO 'moe'@'moestavern';&lt;br /&gt;
&lt;br /&gt;
===A Note on Privileges===&lt;br /&gt;
Privileges in MySQL are highly flexible in terms of specificity, that is to say that you can grant a user specific privileges such as SELECT on a database, table, or column level. Note that only the SELECT, INSERT, and UPDATE privileges are available at the column level, if this seems confusing to you, take a moment to think about why this might be. For instance, why shouldn't there exist a column-level DROP privilege?&lt;br /&gt;
&lt;br /&gt;
The flexibility allows a database administrator (you) to give other users access only to the minimal level of privileges that they require, in accordance with the [http://en.wikipedia.org/wiki/Principle_of_least_privilege principal of least privilege].&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it.&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword.&lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt'&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.webdesign.org/web-programming/php/mysql-join-tutorial.14876.html here] for a good explanation of how to use joins.&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys]&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=226</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=226"/>
		<updated>2010-07-19T21:38:03Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Granting the Ability to Grant: With Grant Option */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig mysqld&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is the following: &lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER 'USER_NAME'@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO 'david'@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO 'TODD'@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
&lt;br /&gt;
===Granting the Ability to Grant: With Grant Option===&lt;br /&gt;
Additionally, in order to allow the user to grant other users privileges you must add ''WITH GRANT OPTION'' to your grant statement. For example:&lt;br /&gt;
 GRANT SELECT on powerplant.reactions, TO 'homer'@'powerplant.springfield.gov' WITH GRANT OPTION&lt;br /&gt;
The above command not only allows user ''homer@powerplant.springfield.gov'' to ''SELECT'' from the powerplant database's reactions table, but also allows him to grant other users the ability to select on those databases.&lt;br /&gt;
&lt;br /&gt;
i.e. Homer can issue the following command:&lt;br /&gt;
 GRANT SELECT ON powerplant.reactions TO 'moe'@'moestavern';&lt;br /&gt;
&lt;br /&gt;
===A Note on Privileges===&lt;br /&gt;
Privileges in MySQL are highly flexible in terms of specificity, that is to say that you can grant a user specific privileges such as SELECT on a database, table, or column level. Note that only the SELECT, INSERT, and UPDATE privileges are available at the column level, if this seems confusing to you, take a moment to think about why this might be. For instance, why shouldn't there exist a column-level DROP privilege?&lt;br /&gt;
&lt;br /&gt;
The flexibility allows a database administrator (you) to give other users access only to the minimal level of privileges that they require, in accordance with the [http://en.wikipedia.org/wiki/Principle_of_least_privilege principal of least privilege].&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it.&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword.&lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt'&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.webdesign.org/web-programming/php/mysql-join-tutorial.14876.html here] for a good explanation of how to use joins.&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys]&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=225</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=225"/>
		<updated>2010-07-19T21:37:50Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Granting Privileges to Existing Users */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig mysqld&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is the following: &lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER 'USER_NAME'@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO 'david'@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO 'TODD'@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
&lt;br /&gt;
====Granting the Ability to Grant: With Grant Option====&lt;br /&gt;
Additionally, in order to allow the user to grant other users privileges you must add ''WITH GRANT OPTION'' to your grant statement. For example:&lt;br /&gt;
 GRANT SELECT on powerplant.reactions, TO 'homer'@'powerplant.springfield.gov' WITH GRANT OPTION&lt;br /&gt;
The above command not only allows user ''homer@powerplant.springfield.gov'' to ''SELECT'' from the powerplant database's reactions table, but also allows him to grant other users the ability to select on those databases.&lt;br /&gt;
&lt;br /&gt;
i.e. Homer can issue the following command:&lt;br /&gt;
 GRANT SELECT ON powerplant.reactions TO 'moe'@'moestavern';&lt;br /&gt;
&lt;br /&gt;
===A Note on Privileges===&lt;br /&gt;
Privileges in MySQL are highly flexible in terms of specificity, that is to say that you can grant a user specific privileges such as SELECT on a database, table, or column level. Note that only the SELECT, INSERT, and UPDATE privileges are available at the column level, if this seems confusing to you, take a moment to think about why this might be. For instance, why shouldn't there exist a column-level DROP privilege?&lt;br /&gt;
&lt;br /&gt;
The flexibility allows a database administrator (you) to give other users access only to the minimal level of privileges that they require, in accordance with the [http://en.wikipedia.org/wiki/Principle_of_least_privilege principal of least privilege].&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it.&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword.&lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt'&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.webdesign.org/web-programming/php/mysql-join-tutorial.14876.html here] for a good explanation of how to use joins.&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys]&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=224</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=224"/>
		<updated>2010-07-19T21:16:55Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* A Note on Privileges */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig mysqld&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is the following: &lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER 'USER_NAME'@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO 'david'@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO 'TODD'@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
===A Note on Privileges===&lt;br /&gt;
Privileges in MySQL are highly flexible in terms of specificity, that is to say that you can grant a user specific privileges such as SELECT on a database, table, or column level. Note that only the SELECT, INSERT, and UPDATE privileges are available at the column level, if this seems confusing to you, take a moment to think about why this might be. For instance, why shouldn't there exist a column-level DROP privilege?&lt;br /&gt;
&lt;br /&gt;
The flexibility allows a database administrator (you) to give other users access only to the minimal level of privileges that they require, in accordance with the [http://en.wikipedia.org/wiki/Principle_of_least_privilege principal of least privilege].&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it.&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword.&lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt'&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.webdesign.org/web-programming/php/mysql-join-tutorial.14876.html here] for a good explanation of how to use joins.&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys]&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=223</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=223"/>
		<updated>2010-07-19T21:16:44Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Creating and Managing Users */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig mysqld&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is the following: &lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER 'USER_NAME'@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO 'david'@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO 'TODD'@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
===A Note on Privileges===&lt;br /&gt;
Privileges in MySQL are highly flexible in terms of specificity, that is to say that you can grant a user specific privileges such as SELECT on a database, table, or column level. Note that only the SELECT, INSERT, and UPDATE privileges are available at the column level, if this seems confusing to you, take a moment to think about why this might be. For instance, why shouldn't there exist a column-level DROP privilege?&lt;br /&gt;
&lt;br /&gt;
The flexibility allows a database administrator (you) to give other users access only to the minimal level of privileges that they require, in accordance with the [http://en.wikipedia.org/wiki/Principle_of_least_privilege principal of least privilege]&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it.&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword.&lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt'&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.webdesign.org/web-programming/php/mysql-join-tutorial.14876.html here] for a good explanation of how to use joins.&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys]&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=222</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=222"/>
		<updated>2010-07-19T20:19:22Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Creating Users */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig mysqld&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is the following: &lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER 'USER_NAME'@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO 'david'@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO 'TODD'@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it.&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword.&lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt'&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.webdesign.org/web-programming/php/mysql-join-tutorial.14876.html here] for a good explanation of how to use joins.&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys]&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=221</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=221"/>
		<updated>2010-07-19T20:18:02Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Managing Users */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig mysqld&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Creating and Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. Note that ''grant'' can be used to both manage the privileges of existing users and to create new users. &lt;br /&gt;
&lt;br /&gt;
===Creating Users===&lt;br /&gt;
The syntax of ''GRANT'' when used to create a user is&lt;br /&gt;
&lt;br /&gt;
 GRANT PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
There is also a ''CREATE USER'' command which creates a user with no privileges (you will have to grant them privileges later.&lt;br /&gt;
The syntax of ''CREATE USER'' which is very similar to that of grant is described below:&lt;br /&gt;
 CREATE USER 'USER_NAME'@'HOST_NAME' IDENTIFIED BY 'password';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss'' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
===Granting Privileges to Existing Users===&lt;br /&gt;
Additionally, ''GRANT'' is used to manage privileges for existing users.&lt;br /&gt;
&lt;br /&gt;
To grant ''SELECT'' privileges to user ''david@%'' on the table ''products'' in database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT ON info.products TO 'david'@'%';&lt;br /&gt;
&lt;br /&gt;
Note that we can also grant users the INSERT, SELECT and UPDATE privileges to a user on a per column basis.&lt;br /&gt;
To grant ''SELECT'' privileges to user ''TODD@%'' only for the column ''GPA'' in the table ''student_info'' database ''info'' we use the following command:&lt;br /&gt;
 GRANT SELECT(student_info) TO 'TODD'@'%' ON info.student_info;&lt;br /&gt;
&lt;br /&gt;
As before, after each grant you must tell MySQL to reload the grant tables by flushing the privileges:&lt;br /&gt;
 FLUSH PRIVILEGES;&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it.&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword.&lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt'&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.webdesign.org/web-programming/php/mysql-join-tutorial.14876.html here] for a good explanation of how to use joins.&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys]&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=220</id>
		<title>PHP Original</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=220"/>
		<updated>2010-07-19T18:13:19Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Variables and Arrays */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= PHP5 =&lt;br /&gt;
PHP is a server-side language. When a web request for PHP file comes in, the web server processes the PHP file to produce HTML output.  That is, the main function of most PHP scripts is to dynamically create HTML content.  PHP5 support for Apache is provided by mod_php5. In Ubuntu, you can provide PHP5 support by installing ''php5''. This will install the apache2 module and the php5 command line interpreter (useful for debugging). PHP files may have different extensions, but ''.php'' is the most common.  In the Apache configuration, you can specify which extensions are going to be treated as PHP scripts by adding the extensions to the list in the ''/etc/httpd/conf.d/php.conf'' file: &lt;br /&gt;
&lt;br /&gt;
  AddType text/html .php .phtml .php3&lt;br /&gt;
&lt;br /&gt;
==Declaring PHP==&lt;br /&gt;
If a PHP file is requested by a web user, Apache parses and interprets the file.  Any code declared between PHP tags is then executed and the total HTML output is sent back to the user. PHP code can be declared in a script file in any of the following ways, with the first being the most common:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 php?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script language=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, edit a new PHP file named ''info.php'', and put these lines in the file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Put the file in the web directory for your user account (remember that this is ''~username/.html/'' if you completed the first Module).  Then access the web page by going to ''http://server/~username/info.php''. You will see something similar to the picture below:&lt;br /&gt;
&lt;br /&gt;
[[Image:Php_info.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you look at the HTML code, you will see quite a lot of HTML output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;DTD/xhtml1-transitional.dtd&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&lt;br /&gt;
 body {background-color: #ffffff; color: #000000;}&lt;br /&gt;
 body, td, th, h1, h2 {font-family: sans-serif;}&lt;br /&gt;
 pre {margin: 0px; font-family: monospace;}&lt;br /&gt;
 a:link {color: #000099; text-decoration: none; background-color: #ffffff;}&lt;br /&gt;
 a:hover {text-decoration: underline;}&lt;br /&gt;
 table {border-collapse: collapse;}&lt;br /&gt;
 .center {text-align: center;}&lt;br /&gt;
 .center table { margin-left: auto; margin-right: auto; text-align: left;}&lt;br /&gt;
 .center th { text-align: center !important; }&lt;br /&gt;
 td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}&lt;br /&gt;
 h1 {font-size: 150%;}&lt;br /&gt;
 h2 {font-size: 125%;}&lt;br /&gt;
 .p {text-align: left;}&lt;br /&gt;
 .e {background-color: #ccccff; font-weight: bold; color: #000000;}&lt;br /&gt;
 .h {background-color: #9999cc; font-weight: bold; color: #000000;}&lt;br /&gt;
 .v {background-color: #cccccc; color: #000000;}&lt;br /&gt;
 .vr {background-color: #cccccc; text-align: right; color: #000000;}&lt;br /&gt;
 img {float: right; border: 0px;}&lt;br /&gt;
 hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}&lt;br /&gt;
 &amp;lt;/style&amp;gt;&lt;br /&gt;
 &amp;lt;title&amp;gt;phpinfo()&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
 &amp;lt;body&amp;gt;&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;table border=&amp;quot;0&amp;quot; cellpadding=&amp;quot;3&amp;quot; width=&amp;quot;600&amp;quot;&amp;gt;&lt;br /&gt;
 ....&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, none of that HTML was in the ''info.php'' file.  Instead, the ''phpinfo()'' line is a built-in PHP function that produces all of that HTML.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Variables require ''$'' at the beginning of their names (regardless of the purpose, be it setting, be it accessing). You don't need to specify the type of the variable; PHP will use the variable as the correct type based on how it is used in the code.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   #this is an integer&lt;br /&gt;
   $i=5;&lt;br /&gt;
   #this is a string&lt;br /&gt;
   $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in many other languages, statements end with a '';''.  To actually produce HTML output, you simply print the HTML from the PHP code.  The simplest way is to use the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
If you use a variable inside of a string, the value of the variable will be put in the string (you might also look at the ''echo'' function):&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   $i=5;&lt;br /&gt;
   print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will generate the string&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
If you create a file with just the above code, you will notice that your browser will only receive the above line. That is, there is no ''html'' tag or anything else. Thus, PHP files are normally a mix of HTML code and PHP code.&lt;br /&gt;
Most PHP scripts, then, look more like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code that the browser will receive is then:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  The value of i is 5&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can have any number of PHP segments and HTML segments in a PHP file.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;? $i=5; ?&amp;gt;&lt;br /&gt;
The value of i is &amp;lt;?  print &amp;quot;$i&amp;quot;; ?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, whatever you print out will be interpreted as HTML by the client's browser, so you can (and will) use HTML tags in the output you produce.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;? print &amp;quot;This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt; ?&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in the following at the user's browser:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which would then render ''test'' in bold.&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
&lt;br /&gt;
Declaring arrays is very easy in PHP. Either you can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
$student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
$student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
$student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or alternatively&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student=array(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you can access the contents with an index into the array, as normal:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find the size of an array with ''count'' function, e.g., ''count($student)''&lt;br /&gt;
&lt;br /&gt;
In PHP, you can also have associative arrays, which are arrays whose members can be accessible with string indices. Here is an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
$test[&amp;quot;test1&amp;quot;]=1;&lt;br /&gt;
$test[&amp;quot;test2&amp;quot;]=2;&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;test1=&amp;quot;,$test[test1],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
echo &amp;quot;test2=&amp;quot;,$test[test2],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access array keys through ''array_keys'' function.&lt;br /&gt;
===Strings: Differences Between Java and PHP===&lt;br /&gt;
&lt;br /&gt;
In php, unlike in java, you can enclose strings in single quotes or double quotes&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
The following is a valid string, just like in java.&lt;br /&gt;
 //A valid string in php and in java.&lt;br /&gt;
 $str=&amp;quot;hey jeff!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And in PHP, the following is also a valid string&lt;br /&gt;
 //A valid string in PHP, but invalid in java.&lt;br /&gt;
 $str='hey jeff!' &lt;br /&gt;
&lt;br /&gt;
however, there is a caveat when using the single quotes&lt;br /&gt;
&lt;br /&gt;
if I wanted to then use MORE single quotes within the string&lt;br /&gt;
&lt;br /&gt;
I have to escape them&lt;br /&gt;
&lt;br /&gt;
so to get the string to contain &lt;br /&gt;
I'm Jeff's string&lt;br /&gt;
&lt;br /&gt;
you could either say&lt;br /&gt;
&lt;br /&gt;
$str=&amp;quot;I'm Jeff's string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
$str='I\'m Jeff\'s string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if/elseif/else'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   some statements&lt;br /&gt;
 }&lt;br /&gt;
 elseif( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   more statements&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
    yet more statements&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string == &amp;quot;this is my string&amp;quot;) {&lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such thing as global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
PHP has ''for'' and ''while'' loops, similar to Java and C:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, the following code will print the sum of numbers from 1 to 10.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 echo &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The advantage of loops  becomes more obvious when you have to create a long table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Number&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Square&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
for($i=0;$i&amp;lt;100;$i++) {&lt;br /&gt;
echo &amp;quot;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;$i &amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;quot;,$i*$i,&amp;quot;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a table with two columns, each row contains a number and its square. &lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt normal control flow through a loop.  ''break'' jumps out of the loop entirely, while ''continue'' jumps to the end of the current loop iteration and flow resumes with the next loop iteration.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
PHP functions are identified with the keyword ''function'' followed by a function name and argument list for the function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
function name($arg1 [= constant], $arg2 [= constant], ...) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   function add($x,$y) {&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
   }&lt;br /&gt;
   echo &amp;quot;Sum of 2+5=&amp;quot;,add(2,5); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If an argument has a default value, it can be specified with the argument on the list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
      print &amp;quot;hello $name how are you today?&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
   helloMsg(&amp;quot;Alice&amp;quot;);&lt;br /&gt;
   helloMsg();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 hello Alice how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
 hello there how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Passing Variables ==&lt;br /&gt;
Input values can be passed to a script either through the URL request line or through data submitted by a form. Built in arrays $_GET['var'] and $_POST['var'] return the value of ''var'' depending on the access method. As we will see in the next section, values can also be passed through session variables. Finally, you can use cookies to store and retrieve values.&lt;br /&gt;
&lt;br /&gt;
===Get: Passing Variables via URL===&lt;br /&gt;
If you want to pass values in a URL, the format is ''http://yourserver/yourphpfile.php?var1=value1&amp;amp;var2=value2.....''&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code would be used to print the previous hello message according to a name given in the URL:&lt;br /&gt;
&lt;br /&gt;
greeting.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $person= $_GET['name'];&lt;br /&gt;
 helloMsg($person);&lt;br /&gt;
 function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
     print &amp;quot;hello $name how are you today?&amp;lt;/br&amp;gt;&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you access the URL like this:  ''greeting.php?name=Alice'', it will show ''hello Alice how are you today?''. However, if you access it with ''greeting.php'', it will print ''hello how are you today?''. Note that it does not use the default value ''there'' since ''$name'' is now a string (in this case, an empty string) and so there is argument being being passed to ''helloMsg''.&lt;br /&gt;
&lt;br /&gt;
===POST: Passing Variables via Form===&lt;br /&gt;
In PHP one method for passing variables from page to page is by sending it to the other page via posting. The way post works in&lt;br /&gt;
PHP is that PHP retrieves the variable names and values from a (slightly modified) html form and sends an those variables in the associative array named $_POST to the page specified in the FORM tag's ''action'' attribute. &lt;br /&gt;
&lt;br /&gt;
Note that receiving the data from a form is very similar to getting it from the URL. Consider the following form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;FORM action=&amp;quot;info.php&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;P&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;firstnamelabel&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;firstname&amp;quot; id=&amp;quot;firstnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;lastnamelabel&amp;quot;&amp;gt;Last name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;lastname&amp;quot; id=&amp;quot;lastnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;birthyearlabel&amp;quot;&amp;gt;Birth Year: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;birthyear&amp;quot; id=&amp;quot;birthyearlabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Male&amp;quot;&amp;gt; Male&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Female&amp;quot;&amp;gt; Female&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;submit&amp;quot; value=&amp;quot;Send&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;reset&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;/P&amp;gt;&lt;br /&gt;
&amp;lt;/FORM&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This posts the form data to ''info.php'', with inputs ''firstname'', ''lastname'', ''birthyear'', and ''gender''.&lt;br /&gt;
The values passed through the form for each input can be accessed in ''info.php'' with the $_POST array.&lt;br /&gt;
*&amp;lt;label&amp;gt; : The label tag just provides a text label for the form inputs.&lt;br /&gt;
*&amp;lt;input ''type=&amp;quot;input_type_here&amp;quot;''&amp;gt; : The input tag provides various means of inputting information into the form, there are various types of inputs such as text, radio, submit, reset, etc. &lt;br /&gt;
*&amp;lt;input ... ''name=&amp;quot;input_name&amp;quot;''&amp;gt; : &lt;br /&gt;
*&amp;lt;label ''for=&amp;quot;identifying_string&amp;quot;''&amp;gt; and &amp;lt;INPUT ... ''id=&amp;quot;identifying_string&amp;quot;'': The optional ''for'' attribute of the label tag associates the label with what we'll call an &amp;quot;identifying string&amp;quot;, which should match the string specified in the corresponding ''id'' portion of the input tag. If both of these attributes are in place, then upon clicking on the label, the corresponding input will be selected.&lt;br /&gt;
&lt;br /&gt;
info.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 echo  &amp;quot;Personal Data:&amp;quot;, $_POST['firstname'], &amp;quot; &amp;quot;, $_POST['lastname'], &amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 $birthyear=$_POST['birthyear'];&lt;br /&gt;
 echo  &amp;quot;Birth year:&amp;quot;, $_birthyear, &amp;quot; (Age:&amp;quot; , 2007-$birthyear, &amp;quot;)&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 echo &amp;quot;Gender: &amp;quot;, $_POST['gender'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For further help with using HTML forms with PHP, we recommend [http://www.tizag.com/phpT/forms.php this tutorial]&lt;br /&gt;
===Self-Submitting Forms===&lt;br /&gt;
Oftentimes we don't want to pass form values to a different page, but rather to the same page as contains the form so that we can process the information and display output on that same page. The way we do this is to specify that the form should submit to $_SERVER['PHP_SELF'] and not say, other_page.php.&lt;br /&gt;
Note that you can make self-submitting forms that use either the get or post method.&lt;br /&gt;
&lt;br /&gt;
The following example simply takes the data input from a form and, via posting, echoes it back to the user as bold text on the same page:&lt;br /&gt;
bold_printer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Bold Printer&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action= &amp;lt;?php echo $_SERVER['PHP_SELF'] ?&amp;gt; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;label for=&amp;quot;text_field&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;user_text&amp;quot; id=&amp;quot;text_field&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Print in Bold&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    if(isset($_POST['user_text'])&lt;br /&gt;
    echo &amp;quot;&amp;lt;b&amp;gt;&amp;quot;, $_POST['user_text'], &amp;quot;&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Session Variables==&lt;br /&gt;
PHP provides a global array, ''$_SESSION'', for values that are available globally throughout a ''session''. The values will stay there until either you remove them in a PHP script, the user's browser is closed, or their session expires (you can tune the timeout period in the PHP configuration files).&lt;br /&gt;
&lt;br /&gt;
In order to access session values, you should first initiate the session with the ''session_start()'' function call. After that, when you access a values with ''$_SESSION'', it will become a session values. For example:&lt;br /&gt;
&lt;br /&gt;
setsessionvars.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$_SESSION['SESSION']=42;&lt;br /&gt;
$_SESSION['username']=&amp;quot;ArthurDent&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This sets two variables, ''SESSION'' and ''username'' to be accessible by other pages in this session. In another page, you can access them like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
if (!isset($_SESSION['SESSION'])) {&lt;br /&gt;
  echo &amp;quot;error session is not registered&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else echo &amp;quot;username=&amp;quot;,$_SESSION['username'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also remove a session variable from the session with unset command:&lt;br /&gt;
&lt;br /&gt;
 unset($_SESSION['SESSION']);&lt;br /&gt;
&lt;br /&gt;
==Redirect the browser to a different page==&lt;br /&gt;
&lt;br /&gt;
It is often useful to redirect the user's browser to a different webpage, for example, after processing inputs from a form.  Here is a simple example that shows how to do this, based on whether or not the ''LOGIN'' session variable is set:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
if(isset($_SESSION[&amp;quot;LOGIN&amp;quot;])){&lt;br /&gt;
	Header(&amp;quot;Location:/welcome.php&amp;quot;);&lt;br /&gt;
}else{&lt;br /&gt;
	Header(&amp;quot;Location:/login.php&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that anytime you call the ''Header'' function, it is adding information to the header section in the HTML object that is being requested.  As such, all ''Header'' calls should go before any other HTML output in the PHP script.&lt;br /&gt;
&lt;br /&gt;
==Sending a file to the Browser==&lt;br /&gt;
&lt;br /&gt;
Another useful function to know is the ''readfile'' function.  It takes a file on the webserver's file system and sends it to the web browser.  A simple example is given below, but note that you'll probably need to specify addition header fields in order to have the web browser download the file correctly. &lt;br /&gt;
For those of you who have used previous versions of PHP, note that mime_content_type($path) is now deprecated, use finfo_file instead&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$file=$_GET['file'];&lt;br /&gt;
$name = $_SESSION[&amp;quot;UNAME&amp;quot;];&lt;br /&gt;
$path=&amp;quot;/home/uploads/$name/$file&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension&lt;br /&gt;
if (!$finfo) {&lt;br /&gt;
 	echo &amp;quot;Opening fileinfo database failed&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$mime=finfo_file($finfo, $path);&lt;br /&gt;
finfo_close($finfo);&lt;br /&gt;
&lt;br /&gt;
header('content-type:'. $mime);&lt;br /&gt;
readfile($path);    &lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uploading files with PHP==&lt;br /&gt;
&lt;br /&gt;
This section describes one way to upload files with PHP.  First, you need an HTML form that contains an input with type ''file'':&lt;br /&gt;
&lt;br /&gt;
transfer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;uploader.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
Choose a file to upload: &amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you use a ''file'' input in a form, PHP automatically adds an entry in the ''$_FILES'' array with information about the file. For example, transfer.php called the file ''uploadedfile'', so $_FILES['uploadedfile'] refers to file that the user selected in the form.  The name of the original file is $_FILES['uploadedfile']['name'].   When you upload a file, it is put in a temporary location by the web server. This temporary name is $_FILES['uploadedfile']['tmp_name']. In PHP, you can move a file from its original location to a new location by calling a built-in function, ''move_uploaded_file'', which takes an argument for the source (temporary location) and destination location. Note that relative paths in the destination location are relative to the location of the PHP script being executed.&lt;br /&gt;
&lt;br /&gt;
uploader.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$target_path = &amp;quot;uploads/&amp;quot;;&lt;br /&gt;
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);&lt;br /&gt;
&lt;br /&gt;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {&lt;br /&gt;
    echo &amp;quot;The file &amp;quot;.  basename( $_FILES['uploadedfile']['name']) . &amp;quot; has been uploaded&amp;quot;;&lt;br /&gt;
} else{&lt;br /&gt;
    echo &amp;quot;There was an error uploading the file, please try again!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Eclipse ==&lt;br /&gt;
&lt;br /&gt;
For those of you who like programming with IDEs, Eclipse has support for PHP.  See:&lt;br /&gt;
http://www.eclipse.org.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=219</id>
		<title>Python</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Python&amp;diff=219"/>
		<updated>2010-07-16T21:00:41Z</updated>

		<summary type="html">&lt;p&gt;Juliad: /* Getting Started */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Python is an interpreted language with familiar syntax. It's a great language to use for parsing text files and rapid application development. Rather than the time-consuming write-compile-test-rewrite-recompile-retest-etc. cycle of languages like C and C++, Python scripts run through an interpreter. This eliminates the compile step and speeds up the development process.&lt;br /&gt;
=Getting Started=&lt;br /&gt;
==Installing Python==&lt;br /&gt;
&lt;br /&gt;
 yum install python&lt;br /&gt;
&lt;br /&gt;
should do the trick. Make sure that python is in your path with&lt;br /&gt;
&lt;br /&gt;
 which python&lt;br /&gt;
&lt;br /&gt;
and modify your path if necessary.&lt;br /&gt;
==Using Python==&lt;br /&gt;
&lt;br /&gt;
Python programs can be run in several ways. Two of the most common ways are through the python shell and in .py files. &lt;br /&gt;
To enter the python shell, simply type &lt;br /&gt;
&lt;br /&gt;
 python&lt;br /&gt;
&lt;br /&gt;
into your shell. Interactive mode should start up, and you can type commands to be directly executed. You can exit interactive mode with ctrl+d or with &lt;br /&gt;
&lt;br /&gt;
 quit()&lt;br /&gt;
&lt;br /&gt;
To run a .py file, simply use:&lt;br /&gt;
&lt;br /&gt;
 python &amp;lt;filename&amp;gt; &amp;lt;args&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Syntax==&lt;br /&gt;
&lt;br /&gt;
Since the online documentation for Python is quite extensive and helpful, [http://docs.python.org/tutorial/index.html the online Python tutorial] is definitely the place for any and all of your Python-related needs. Be sure to read the [http://docs.python.org/tutorial/datastructures.html data structures] page to understand how they work in Python.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=214</id>
		<title>Perl</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=214"/>
		<updated>2010-07-16T20:33:21Z</updated>

		<summary type="html">&lt;p&gt;Juliad: Protected &amp;quot;Perl&amp;quot; ([edit=sysop] (indefinite) [move=sysop] (indefinite))&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Perl=&lt;br /&gt;
Perl is an interpreted language with syntax similar to bash and PHP.  It's a high level, general purpose language that is used primarily to automate repetitive tasks, and to parse and process text.  Indeed, text processing was the original intended use for the language, although it has grown to be an extremely flexible language for many different types of processing.&lt;br /&gt;
&lt;br /&gt;
==Running Perl==&lt;br /&gt;
Like many scripting languages, Perl programs can be run in several ways.  The programs, or scripts, themselves are plain text files, usually ending in '.pl' to indicate that it is a Perl script.  Given a Perl script file, the most common ways to execute the script are shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pass the script as input to the 'perl' executable: &amp;lt;pre&amp;gt;perl -w SCRIPT&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Execute the script directly by adding the following line to the top of the script:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl -w&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Then, change the script to be executable, and finally execute the script:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chmod a+x SCRIPT&lt;br /&gt;
./SCRIPT &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While flexibility is one of the major strengths of Perl, the flexibility in the language itself can often lead to unintended consequences.  To overcome this, it is good practice to explicitly enable warnings and strict behavior for you scripts.  The ''-w'' above when running perl gets you part of the way there, and although it's not necessary to supply the ''-w'' it is nearly always the right thing to do.  You should also add the line:&lt;br /&gt;
&lt;br /&gt;
 use strict;&lt;br /&gt;
&lt;br /&gt;
to all of your scripts, near the top, to enable strict error checking.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Standard variables start with a dollar sign, ''$'', at the beginning of their names.  Unlike shell scripts, the ''$'' is always used, regardless of whether the variable is being set or accessed.  As with other scripting languages, you don't specify the type of the variable; it will be used treated as a integer, floating point number, string, etc, depending on how it is used in your script.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #this is an integer&lt;br /&gt;
 $i=5;&lt;br /&gt;
 #this is a string&lt;br /&gt;
 $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Comments start with the number/sharp/hash sign, ''#'', and extend until the end of the line.  All non-comment statements end with a semicolon, '';''.  You can print  variables with the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
Variables can be used inside strings, and the value of that variable will be included in the string. (Note that you can also use the ''printf'' function when you need specific formatting of the output).&lt;br /&gt;
 &lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
This will generate the string:&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
All variables are global by default, so you need to specify locality if you want local variables. This can be done with the ''local'' and ''my'' keywords. Generally, ''my'' is safer and faster, and is therefore used most often.  If a variable is defined by ''my'' in a block, it will be local to the block.  If a variable is defined by ''local'', any subroutine call from that block will also have that variable defined.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $i=0;&lt;br /&gt;
 while($i&amp;lt;5)&lt;br /&gt;
 {&lt;br /&gt;
   my $squared = $i*$i;&lt;br /&gt;
   $cubed = $i*$i*$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print $squared;&lt;br /&gt;
 print $cubed;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above program segment, ''$squared'' is defined with ''my'' inside the while loop, so it is only defined inside that loop.  ''$cubed'' is not given any special scope, so it is a global variable.  Hence, after the loop, ''$squared'' will be undefined, but ''$cubed'' with have the value 64 (the last value it was assigned in the loop).&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
Arrays are represented with two different symbols, depending on whether you are referring to elements of the array, or the entire array.  Array variable names start with ''@'' when declaring them or when you are referring to the array itself.  The normal ''$'' is used when you want to access individual elements of the array.  Declaring arrays is fairly easy.  You can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
 $student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
 $student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
 $student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 or alternatively&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @student=(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general, lists strings or variables enclosed in parentheses, ''(value1,value2,...,valuen )'', are interpreted as arrays. You can access the contents with an array index:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
You can also print the whole array:&lt;br /&gt;
&lt;br /&gt;
 print @student;&lt;br /&gt;
&lt;br /&gt;
The above will concatenate all the elements together without a delimiter.  Alternatively, you can print the array inside of a quotes&lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;@student';&lt;br /&gt;
&lt;br /&gt;
which will print each elements separated by a space.&lt;br /&gt;
&lt;br /&gt;
The current size of an array can be accessed with the ''scalar()'' function:&lt;br /&gt;
&lt;br /&gt;
 $sz=scalar(@student);&lt;br /&gt;
&lt;br /&gt;
You can also use special syntax to access the index of the last element in an array like so:&lt;br /&gt;
&lt;br /&gt;
 $last_index = $#student;&lt;br /&gt;
&lt;br /&gt;
Note that you start the array variable with ''$#'' instead of ''@'' or just ''$''.  In the example above, ''scalar(@student)'' is 4, and ''$#student'' is 3.&lt;br /&gt;
&lt;br /&gt;
You can declare a sequence with &lt;br /&gt;
&lt;br /&gt;
 (START..END);&lt;br /&gt;
&lt;br /&gt;
START and END could be numbers or a characters:&lt;br /&gt;
&lt;br /&gt;
 @newarray = (1 .. 20);&lt;br /&gt;
 @newchars = ('l' .. 't');&lt;br /&gt;
&lt;br /&gt;
There is also support for adding and removing elements. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''push()''' - adds an element to the end of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''unshift()''' - adds an element to the beginning of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''pop()''' - removes the last element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''shift()''' - removes the first element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''push()'' and ''unshift()'' take an array and a variable as arguments:&lt;br /&gt;
&lt;br /&gt;
 push @ARRAY, VARIABLE&lt;br /&gt;
&lt;br /&gt;
VARIABLE can actually be a single variable or another array:&lt;br /&gt;
&lt;br /&gt;
 push @student, &amp;quot;Eve&amp;quot;;&lt;br /&gt;
 push @student, @newstudents;&lt;br /&gt;
 push @student, (&amp;quot;Frank&amp;quot;,&amp;quot;Gabriel&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''pop()'' and ''shift()'' take only the array as an argument:&lt;br /&gt;
&lt;br /&gt;
 $laststudent = pop(@student);&lt;br /&gt;
&lt;br /&gt;
You can use the ''slice'' operation to retrieve portions of an array.&lt;br /&gt;
&lt;br /&gt;
 @newarray = @student[1..3]; # elements 1-3&lt;br /&gt;
 @newarray = @student[1..3,5,7,9..15]; # elements 1-3,5,7,9-15&lt;br /&gt;
&lt;br /&gt;
The ''splice()'' function lets you replace arbitrary elements within an array.  Here is the general syntax:&lt;br /&gt;
&lt;br /&gt;
 splice(@ARRAY,STARTING_INDEX,LENGTH,@REPLACING-ARRAY);&lt;br /&gt;
  &lt;br /&gt;
Here is an example that replaces element 3 with ''Jerry'' and elements 4 with ''Lena'':&lt;br /&gt;
&lt;br /&gt;
 splice(@student,3,2,(&amp;quot;Jerry&amp;quot;,&amp;quot;Lena&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
The ''split()'' operation creates an array from a string. It takes a delimiter and a string as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 @array=split(':',&amp;quot;test:array:1:2:3&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The above will produce an array with 5 elements (''test'',''array'',''1'',''2'',''3'').&lt;br /&gt;
&lt;br /&gt;
The ''join()'' function does the reverse, i.e., it creates a string from the elements of an array. It takes a delimiter and an array as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 $studentNames=join(',',@student);&lt;br /&gt;
&lt;br /&gt;
The above will form one string with every element from ''@student'' separated by a comma.&lt;br /&gt;
&lt;br /&gt;
Perl also has hash arrays, which are similar to associate arrays. Whereas arrays are represented with the ''@'', hash arrays are represented by the ''%''symbol.&lt;br /&gt;
&lt;br /&gt;
 %ages = (&amp;quot;Leela&amp;quot;, 25,&lt;br /&gt;
         &amp;quot;Fry&amp;quot;, 28,&lt;br /&gt;
         &amp;quot;Bender&amp;quot;, 4,&lt;br /&gt;
         &amp;quot;Lord Nibbler&amp;quot;, &amp;quot;Unknown&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Elements within a hash array accessed by prefacing the hash array name with ''$'' and then using curly braces for the index.  For example:&lt;br /&gt;
&lt;br /&gt;
 $ages{&amp;quot;Leela&amp;quot;};	# Returns 25&lt;br /&gt;
 $ages{&amp;quot;Fry&amp;quot;};		# Returns 28&lt;br /&gt;
 $ages{&amp;quot;Bender&amp;quot;};	# Returns 4&lt;br /&gt;
 $ages{&amp;quot;Lord Nibbler&amp;quot;};	# Returns &amp;quot;Unknown&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
&lt;br /&gt;
''if'' statements work as they do in most other languages.  The general format is&lt;br /&gt;
&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #some statements&lt;br /&gt;
 }&lt;br /&gt;
 elsif ( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #whatever&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
   #other stuff&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
The standard comparison operators work as usual, including ''=='', ''&amp;lt;'', ''&amp;gt;'', etc.  The only thing to be aware of is that you should use ''eq'' and ''neq'' to compare strings instead of ''=='' and ''!=''.  &lt;br /&gt;
&lt;br /&gt;
 if($string eq &amp;quot;some string&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;strings are equal\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 elsif($int == 5)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;integer is five\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
&lt;br /&gt;
There are three main types of loops.  ''for'' and ''while'' loops work similarly to other languages.  Here are the general forms:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while(condition)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here are simple examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $sum=0;&lt;br /&gt;
 $i=1;&lt;br /&gt;
 while($i&amp;lt;=10)&lt;br /&gt;
 {&lt;br /&gt;
   $sum=$sum+$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print &amp;quot;sum is $sum\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
 for($j=0; $j&amp;lt;5; ++$j)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;j is $j\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''next'' and ''last'' are special commands that will jump to the next loop iteration, or to the end of the loop, respectively.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 for($j=0; $j&amp;lt;5; ++$j)&lt;br /&gt;
 {&lt;br /&gt;
   if($j == 2) { next; }&lt;br /&gt;
   if($j == 4) { last; }&lt;br /&gt;
   print &amp;quot;j is $j\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
 j is 0&lt;br /&gt;
 j is 1&lt;br /&gt;
 j is 3&lt;br /&gt;
&lt;br /&gt;
Finally, there is the ''foreach'' loop.  ''foreach'' loops are most useful when iterating through arrays or hashs.  Here is the general form:&lt;br /&gt;
&lt;br /&gt;
 foreach $iterator (@array)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, to print out all the values in an array, you could do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @names = ('Bugs', 'Daffy', 'Marvin');&lt;br /&gt;
 foreach $name (@names)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;$name\n&amp;quot;;&lt;br /&gt;
 } &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
Perl functions are identified with the keyword ''sub'' followed by the function name. Arguments passed to functions are stored in a special array ''@_''.  You can either access all of the arguments by assigning them to local variables via ''@_'' or you can use the ''shift'' function.  Here is the general form for a function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub functionname&lt;br /&gt;
 {&lt;br /&gt;
   my ($arg1,$arg2,...,$argn)=@_;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub functionname&lt;br /&gt;
 {&lt;br /&gt;
   my $arg1 = shift;&lt;br /&gt;
   my $arg2 = shift;&lt;br /&gt;
   ...&lt;br /&gt;
   my $argn = shift;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is a simple function to add two numbers and a call to that function (note that functions can be defined above or below where they are called):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub add&lt;br /&gt;
 {&lt;br /&gt;
   my ($x,$y) = @_;&lt;br /&gt;
   return ($x+$y);&lt;br /&gt;
 }  &lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;sum of 2+5=&amp;quot; . add(2,5) . &amp;quot;\n&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command line arguments to the Perl script (excluding the script name) are stored in the array ''@ARGV''. For example, the following code prints out the number of command line arguments:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 print &amp;quot;There are &amp;quot; . scalar(@ARGV) . &amp;quot; arguments\n&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
Perl is widely known for excellence in text processing, and regular expressions are one of the big factors behind this fame. There is a lot to learn about regular expressions, and here is only a very brief introduction that serves as a starting point. Please refer to the CPAN [http://perldoc.perl.org/perlretut.html tutorial] (from which the following is extracted) and [http://perldoc.perl.org/perlre.html reference] for more complete and in-depth coverage.&lt;br /&gt;
&lt;br /&gt;
A regular expression (or simply regex) can be thought of as a ''pattern'', composed of characters and symbols, which expresses what you want to search in a body of text. A regex is used in Perl functions such as string matching, search and replace, and splitting. In the following, we will first look at some typical ways of defining regex. Then we will give examples of how they are used in Perl functions.&lt;br /&gt;
&lt;br /&gt;
==Defining Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
A regex is marked by ''//''. The simplest regex is just a word, or more generally, a string of characters. Such a regex simply means that the ''pattern'' to be matched is that word.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
/World/                       # matches the whole word 'World' (case sensitive)&lt;br /&gt;
/Hello World /                # matches the whole phrase with a space at the end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Not all characters can be used 'as is' in a match. Some characters, called metacharacters, are reserved for use in regex notation. The metacharacters are &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
{}[]()^$.|*+?\&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metacharacter can be used in the regex by putting a backslash before it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
/\/usr\/bin\/perl/            # matches the word '/usr/bin/perl' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The backslash also works for non-printable ASCII characters, represented as escape sequences. Common examples are \s for a space, \t for a tab, \n for a newline, and \r for a carriage return.&lt;br /&gt;
&lt;br /&gt;
A '''character class''' allows a set of possible characters, rather than just a single character, to match at a particular point in a regex. Character classes are denoted by brackets [...] , with the set of characters to be possibly matched inside. A &amp;quot;-&amp;quot; can be used to specify range of characters. Here are some examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
/[yY][eE][sS]/                # matches 'yes' in a case-insensitive way (e.g., 'Yes', 'YES', 'yes', etc.)&lt;br /&gt;
/item[012345]/                # matches 'item0' or ... or 'item5'&lt;br /&gt;
/item[0-5]/                   # does the same thing&lt;br /&gt;
/[0-9a-fA-F]/                 # matches a hexadecimal digit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Different character strings can be matched with the '''alternation''' metacharacter '|'. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
/cat|dog|bird/                # matches 'cat', or 'dog', or 'bird'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can '''group''' parts of a regex by enclosing them in parentheses '()'. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
/House(cat|keeper)/           # matches 'Housecat' or 'Housekeeper'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the powerful features of regex is matching '''repetitions'''. The quantifier metacharacters ?, * , + , and {} allow us to determine the number of repeats of a portion of a regex we consider to be a match. Quantifiers are put immediately after the character, character class, or grouping that we want to specify. They have the following meanings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
/a?/     # matches 'a' 1 or 0 times&lt;br /&gt;
/a*/     # matches 'a' 0 or more times, i.e., any number of times&lt;br /&gt;
/a+/     # matches 'a' 1 or more times, i.e., at least once&lt;br /&gt;
/a{3,5}/ # matches at least 3 times, but not more than 5 times.&lt;br /&gt;
/a{3,}/  # matches at least 3 or more times&lt;br /&gt;
/a{3}/   # matches exactly 3 times&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here are more examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
/[a-zA-Z]+.pl/                # matches any .pl filename that consists of only alphabet characters&lt;br /&gt;
/[a-z]+\s+\d*/                # matches a lowercase word, at least some space, and any number of digits&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Using Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regex is used almost any time when string matching is needed. To start, consider '''testing''' if a string contains the matching pattern (e.g., a regex). The operator =~ associates the string with the regex match and produces a true value if the regex matched, or false if the regex did not match. Perl will always match at the earliest possible point in the string. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
print &amp;quot;It matches\n&amp;quot; if &amp;quot;Hello World&amp;quot; =~ /World/;     # prints &amp;quot;It matches&amp;quot;&lt;br /&gt;
print &amp;quot;It matches\n&amp;quot; if &amp;quot;Hello World&amp;quot; =~ /o/;         # prints &amp;quot;It matches&amp;quot; (it matches the 'o' in 'Hello')&lt;br /&gt;
print &amp;quot;It matches\n&amp;quot; if &amp;quot;Hello World&amp;quot; =~ / o/;        # does not match&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Search and replace''' is performed using the format =~ s/regex/replacement/modifiers. The replacement is a Perl double quoted string that replaces in the string whatever is matched with the regex. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$x = &amp;quot;I batted 4 for 4&amp;quot;;&lt;br /&gt;
$x =~ s/4/four/;                         # $x now contains &amp;quot;I batted four for 4&amp;quot;&lt;br /&gt;
$x = &amp;quot;I batted 4 for 4&amp;quot;;&lt;br /&gt;
$x =~ s/4/four/g;                        # $x now contains &amp;quot;I batted four for four&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the global modifier g asks Perl to search and replace all occurrences of the regex in the string. If there is a match, s/// returns the number of substitutions made, otherwise it returns false.&lt;br /&gt;
&lt;br /&gt;
Another use of regex is to '''split''' a string into a set of strings marked by delimiters, where the delimiters are specified by a given regex. This is done using the split function. For example, to split a string into words, do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$x = &amp;quot;Calvin and Hobbes&amp;quot;;&lt;br /&gt;
@word = split /\s+/, $x;                 # $word[0] = 'Calvin'&lt;br /&gt;
                                         # $word[1] = 'and'&lt;br /&gt;
                                         # $word[2] = 'Hobbes'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen above, the split function returns the separated strings as an array. If the empty regex // is used, the string is split into individual characters.&lt;br /&gt;
&lt;br /&gt;
=Perl Modules=&lt;br /&gt;
Perl modules are simply Perl libraries that provide new or enhanced functionality.  Although some modules are distributed as part of the core Perl distribution, most of them are written and maintained by other Perl users or developers who needed some specific functionality that was missing.  The [http://www.cpan.org CPAN] repository is the portal for finding and getting modules.  CPAN also contains manuals for the modules, most of which contain examples.  Generally speaking, the modules are all perl programs, although sometimes they require libraries from other languages.&lt;br /&gt;
&lt;br /&gt;
The typical location for perl modules is ''/usr/lib/perl/XXX'' where XXX is the version of perl.  For current Ubuntu installs, the perl version is 5.10, so the modules are in ''/usr/lib/perl/5.10/''.  You can define other locations by setting the ''PERL5LIB'' environmental variable. &lt;br /&gt;
&lt;br /&gt;
 export PERL5LIB=directory1:directory2&lt;br /&gt;
&lt;br /&gt;
A module is contained in ''.pm'' files located in one of the module directories (either the default or through PERL5LIB).  They are accessed in a perl script by including them via the ''use'' command.  Most modules are grouped in to common packages. For example, there is a ''DBI'' package that contains many database interface modules.  One of those modules is the ''mysql'' module.  To include that module (assuming you have it installed on your system) in your script, you would add this line:&lt;br /&gt;
&lt;br /&gt;
 use DBI::mysql;&lt;br /&gt;
&lt;br /&gt;
If a module you want is not installed on your system, you need to install it either manually or through the automated ''cpan'' command.  The later approach takes a bit of set up, but it makes installing new modules much simpler.&lt;br /&gt;
&lt;br /&gt;
==Manual Module Installation==&lt;br /&gt;
Search the CPAN website for the module you want, then download the module.  Untar it (or unzip it) and change to the directory containing the source code.  You will notice that there is a file named ''Makefile.PL''. It's a perl script that generates a makefile for your perl installation.  Run this:&lt;br /&gt;
&lt;br /&gt;
 perl Makefile.PL&lt;br /&gt;
&lt;br /&gt;
This generates the actual makefile you need to build the module.  If you want to install the module to a non-default module location, set the PREFIX environment variable:&lt;br /&gt;
&lt;br /&gt;
 perl Makefile.PL PREFIX=my_module_directory&lt;br /&gt;
&lt;br /&gt;
Once you have the makefile, you can build the module, test it, and install it. Most Perl modules require testing to ensure they are functioning correctly on your system, so doing the tests is part of the build process.  If a critical component of the test fails, you may have to resolve any problems before moving on.&lt;br /&gt;
&lt;br /&gt;
 make&lt;br /&gt;
 make test&lt;br /&gt;
 make install&lt;br /&gt;
&lt;br /&gt;
Some modules require prerequisites which you need to install yourself.  In some cases, the dependencies can easily spiral out of control so that it becomes very difficult to install everything manually, which is why the CPAN approach is preferred. &lt;br /&gt;
&lt;br /&gt;
==Automated Module Installation==&lt;br /&gt;
''cpan'' is a perl script that comes with the perl package that uses the CPAN perl module. It is a tool that installs new modules with minimum user interaction and that auto installs prerequisites.  To install new modules system-wide, you'll need to run ''cpan'' as root.  So, run:&lt;br /&gt;
 &lt;br /&gt;
 sudo cpan&lt;br /&gt;
&lt;br /&gt;
If it is the first time you are running ''cpan'', you will need to answer some configuration questions.  When prompted, you should select the automated configuration to get things going without any extra hassle.  You will then get to the ''cpan'' prompt, where you can run commands to search for and install new modules.&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The first thing should probably install is ''Bundle::CPAN'' which installs a bunch of packages that make cpan easier to use.  To do that, at the ''cpan&amp;gt;'' prompt, run:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' install Bundle::CPAN&lt;br /&gt;
&lt;br /&gt;
Note that as it installs, it will probably ask if it's ok to install additional packages and such along the way.  It's generally ok to accept whatever it asks when you are prompted.  The ''help'' command shows you all the available commands and options. You can search for a module with the ''i'' command:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' i /KEYWORDS/&lt;br /&gt;
&lt;br /&gt;
You can install a module with the ''install'' command:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' install MODULE&lt;br /&gt;
&lt;br /&gt;
''install'' downloads the module, compiles and tests it, and finally installs it if everything went well. The downloaded modules are stored under your ''~/.cpan '' directory (unless you specified another one during configuration), in case you need to look at source code or anything related to the module.  Although ''cpan'' will download any perl modules that are needed by the module you actually want to install, it might fail if any of the modules rely on non-perl resources elsewhere on the system (e.g., graphics libraries).  If that happens, you can go to the build directory, ''~/.cpan/build/MODULE'', and try to fix the problem.&lt;br /&gt;
&lt;br /&gt;
=Connecting MySQL with Perl=&lt;br /&gt;
&lt;br /&gt;
In order to connect to a mysql server, you need to have the DBI module and DBD::mysql installed on your system.  The DBI is a generic database interface that is relatively easy to use, and a good overall description of the DBI is [http://www.perl.com/pub/a/1999/10/DBI.html here].&lt;br /&gt;
&lt;br /&gt;
Once you have the correct modules installed, you can connect to a mysql server with the function:&lt;br /&gt;
&lt;br /&gt;
 DBI-&amp;gt;connect(DATABASE_DESCRIPTION,USERNAME,PASS);&lt;br /&gt;
&lt;br /&gt;
The database description contains the type of the database (so that the DBI knows how to connect), the name of the database and the host name.  It returns a handle that you will use to execute SQL statements on that database.  Here is an example to connect the grades databases on the localhost:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 use DBI;&lt;br /&gt;
 $dbh=DBI-&amp;gt;connect(&amp;quot;DBI:mysql:database=grades&amp;quot;,'dbuser','dbpass') or die &amp;quot;error connecting to database&amp;quot;;  #note that $dbh is your database handle&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once you have the connection established, you can send SQL queries to the database server. First you need to prepare your SQL statement with the ''prepare'' function on the database handle:&lt;br /&gt;
&lt;br /&gt;
 $stmth = $dbh-&amp;gt;prepare(&amp;quot;SQL STATEMENT&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''prepare'' returns a statement handle that you will use when executing or working with that statement.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values ('Aladdin','Arabian Nights','Adventurer')&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
You have to be careful about quotation marks. It's usually simpler to use the ''qq'' function (which is just a generalized quotation mechanism):&lt;br /&gt;
&lt;br /&gt;
 $sql=qq(insert into employee values ('Aladdin','Arabian Nights','Adventurer'));&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
&lt;br /&gt;
Once you have a statement prepared, you can execute it with the ''execute'' function:&lt;br /&gt;
&lt;br /&gt;
 $sth-&amp;gt;execute() or die &amp;quot;problem executing statement&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
''prepare'' also lets you bind variables so that you don't have to repeat them.  For example, if you want to have a generic way to insert new values, you can prepare your statement like this:&lt;br /&gt;
&lt;br /&gt;
 $inserth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values (?,?,?)&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Then, to execute the statement, you supply the values to fill in for each ''?'':&lt;br /&gt;
&lt;br /&gt;
 $inserth-&amp;gt;execute('Aladdin','Arabian Nights','Adventurer') or die &amp;quot;problem executing statement&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
When executing statements that return data, the results are also accessed through the statement handle. After execution, ''$sth-&amp;gt;fetchrow_array'' returns a row from your results.  This function returns an array, and you can access the columns in each sql row as members of the perl array. As ''fetchrow_array'' returns only one row, you call it continuously until it stops returning rows.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sql=qq(select name,jobtitle from employee);&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
 $sth-&amp;gt;execute() or die &amp;quot;problem executing statement&amp;quot;;&lt;br /&gt;
 while(@row=$sth-&amp;gt;fetchrow_array)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;$row[0] $row[1] \n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Parsing HTML with Perl=&lt;br /&gt;
&lt;br /&gt;
The Perl module [http://search.cpan.org/~petek/HTML-Tree-3.23/lib/HTML/Element.pm HTML::Element] provides a structured way to represent HTML elements (starting with &amp;lt;tag&amp;gt;, having some attributes and children, and finishing with &amp;lt;/tag&amp;gt;).  Any HTML document can be represented as a tree made up of HTML::Elements. For example, consider the following HTML page:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;head&amp;gt;&lt;br /&gt;
&amp;amp;lt;title&amp;gt;List of Employees&amp;amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;center&amp;gt;&lt;br /&gt;
&amp;amp;lt;h1&amp;gt;Employees&amp;amp;lt;/h1&amp;gt; &amp;amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;table&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Name&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Department&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Title&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Alice&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Wonderland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Lost traveler&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Peter&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Neverland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Team leader&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This document can be represented in a tree format:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt; &lt;br /&gt;
  &amp;amp;lt;head&amp;gt; &lt;br /&gt;
    &amp;amp;lt;title&amp;gt; &lt;br /&gt;
      &amp;quot;List of Employees&amp;quot;&lt;br /&gt;
  &amp;amp;lt;body&amp;gt; &lt;br /&gt;
    &amp;amp;lt;center&amp;gt; &lt;br /&gt;
      &amp;amp;lt;h1&amp;gt; &lt;br /&gt;
        &amp;quot;Employees&amp;quot;&lt;br /&gt;
      &amp;amp;lt;br&amp;gt; &lt;br /&gt;
      &amp;amp;lt;table&amp;gt; &lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Name&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Department&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Title&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Alice&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Wonderland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Lost traveler&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Peter&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Neverland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt;&lt;br /&gt;
            &amp;quot;Team leader&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice the hierarchical relationships in the tree. For example, the ''&amp;lt;html&amp;gt;'' node has two direct children, ''&amp;lt;head&amp;gt;'' and ''&amp;lt;body&amp;gt;'', and these elements have their own children. The Perl module [http://search.cpan.org/~sburke/HTML-Tree/lib/HTML/Tree.pm HTML::Tree] builds this tree from an HTML document. You can find more about HTML::Trees [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/AboutTrees.pod here]. In order to use ''HTML::Tree''s, you have to use the ''HTML::TreeBuilder'' module.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 use HTML::TreeBuilder;&lt;br /&gt;
 $tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will create a new tree for you. You can then use this tree to parse an HTML file. There are two ways to parse HTML, either directly with ''parse_file'', or by parsing individual strings with ''parse''.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;parse_file('downloaded.html');&lt;br /&gt;
&lt;br /&gt;
You can print the contents of an HTML::Tree by calling the ''dump'' function.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;dump;&lt;br /&gt;
&lt;br /&gt;
You can call the parse function several times for different content, but before parsing new data, you need to empty the existing parsed data in the tree with the ''delete'' function.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;delete;&lt;br /&gt;
&lt;br /&gt;
==HTML Scanning==&lt;br /&gt;
&lt;br /&gt;
This section details an example that captures science headlines from an HTML document from [http://news.bbc.co.uk/2/hi/science/nature/default.stm BBC Science News]. Assuming you have downloaded the web page to ''bbc.html'', you can build a tree from it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you can use this tree to retrieve information from the document. The ''look_down'' function on an HTML::Tree or HTML::Element is used to return any child satisfying some criterion:&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE);&lt;br /&gt;
&lt;br /&gt;
For example, the following code will return the set of hyperlink HTML::Elements in the page, and put them into an array.&lt;br /&gt;
&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The return value can be a variable instead of array:&lt;br /&gt;
&lt;br /&gt;
 $first_link=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
In this case''look_down'' returns the first hyperlink element found.&lt;br /&gt;
&lt;br /&gt;
The power of the ''look_down'' function comes from the ability to specify further criteria like so:&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE, &lt;br /&gt;
       sub { SOME FUNCTION CODE HERE });&lt;br /&gt;
&lt;br /&gt;
This will return any element that has SOMEATTRIBUTE with value ITSVALUE, and the function specified in look_down returns 1.  Usually this function evaluates other attributes of the element. The element that is being evaluated is referred as $_[0]. For example, the following code returns all hyperlinks that contain URLs to cnn.com.&lt;br /&gt;
&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;,&lt;br /&gt;
           sub { &lt;br /&gt;
                 if($_[0]-&amp;gt;attr(&amp;quot;href&amp;quot;) =~ /www.cnn.com/)&lt;br /&gt;
                 {&lt;br /&gt;
                   return 1;&lt;br /&gt;
                 }&lt;br /&gt;
                 return 0;&lt;br /&gt;
               });&lt;br /&gt;
&lt;br /&gt;
Note that we are accessing the ''href'' attribute of the element and using a regular expression to match it against ''www.cnn.com''.&lt;br /&gt;
&lt;br /&gt;
To actually find news headlines, we need to determine the characteristics of the bbc.html file to discern what the headlines look like.  For example, this page has a main headline followed by a description:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;tsh&amp;quot; href=&amp;quot;/2/hi/health/7026443.stm&amp;quot;&amp;gt;&lt;br /&gt;
			Chilli compound fires painkiller&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
			A chemical from chilli peppers may be able to kill pain without affecting touch or movement.&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
.......&lt;br /&gt;
&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44151000/jpg/_44151791_art_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Artists impression of Gryposaurus monumentensis (copyright: Larry Felder)&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&lt;br /&gt;
			Duck-billed dinosaur had big bite&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
			A new species of duck-billed dinosaur that had up to 800 teeth is described by scientists.&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
.......&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44152000/jpg/_44152097_tombstone_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Before and after tombstone&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&lt;br /&gt;
			Scans reveal lost gravestone text&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
		        Illegible words on church headstones could be read once more thanks to a new scan technology.&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case, it's the hyperlinks that have class types ''tsh'' and ''shl'' that we are interested in, so we look down to find those class types:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/;&lt;br /&gt;
         });&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then by printing out these hyperlink elements as text, we can get the headlines:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 foreach $link (@links)&lt;br /&gt;
 {&lt;br /&gt;
   print $link-&amp;gt;as_text;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, these elements just give us the headlines, not the summaries. If you look at the BBC html document, the headlines are inside a ''div'' element.  The summaries are in the following ''div''.  So, we can reach those summaries by first moving to the parent of the hyperlink HTML::Element and then going to the next sibling of that parent:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 foreach $link (@links)&lt;br /&gt;
 {&lt;br /&gt;
   print $link-&amp;gt;as_text;&lt;br /&gt;
   print $link-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general, you have to be careful about these kind of operations.  If, for example, parent was undefined, the above code would be halted during execution with an error.  It's therefore important to always check if $link-&amp;gt;parent is actually defined before following the pointer.  Similarly, you would need to check if $link-&amp;gt;parent-&amp;gt;right was defined.&lt;br /&gt;
&lt;br /&gt;
Finally, you can use the LWP::UserAgent module to make web requests to get the contents of a page and pass those contents to the tree all in one perl script.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl -w&lt;br /&gt;
&lt;br /&gt;
use LWP::UserAgent;&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
&lt;br /&gt;
$ua=LWP::UserAgent-&amp;gt;new;&lt;br /&gt;
$req=$ua-&amp;gt;get(&amp;quot;http://news.bbc.co.uk/2/hi/science/nature/default.stm&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
$ua-&amp;gt;agent('Mozilla/5.0'); #you can modify several internal parameters, such as browser identification&lt;br /&gt;
&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse($req-&amp;gt;as_string);&lt;br /&gt;
&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a', #get all links&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; || # class name is tsh&lt;br /&gt;
            ($_[0]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot; &amp;amp;&amp;amp; &lt;br /&gt;
             $_[0]-&amp;gt;attr('href') =~ m/science/) || # or their url contain the keyword science and their parents belong to class arr&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~ m/hl$/; # or their class name ends with hl&lt;br /&gt;
         });&lt;br /&gt;
&lt;br /&gt;
foreach $link (@links)&lt;br /&gt;
{&lt;br /&gt;
  print $link-&amp;gt;as_text . &amp;quot;:&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
  if( !($link-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot;))&lt;br /&gt;
  {&lt;br /&gt;
    print $link-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text . &amp;quot;\n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
  else&lt;br /&gt;
  {&lt;br /&gt;
    print &amp;quot;Sideline \n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Additional examples can be found in the Perl documentation for Tree Scanning [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/Scanning.pod here].&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Remote_Procedure_Calls&amp;diff=213</id>
		<title>Remote Procedure Calls</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Remote_Procedure_Calls&amp;diff=213"/>
		<updated>2010-07-16T20:32:51Z</updated>

		<summary type="html">&lt;p&gt;Juliad: Protected &amp;quot;Remote Procedure Calls&amp;quot; ([edit=sysop] (indefinite) [move=sysop] (indefinite))&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=RPC=&lt;br /&gt;
Remote Procedure Calls (RPC) is a general computing paradigm for invoking procedures on one computer from another.  There are many forms of RPC technology, and we will focus on XML-RPC.&lt;br /&gt;
&lt;br /&gt;
=XML-RPC=&lt;br /&gt;
XML-RPC is an RPC protocol that uses XML encoding of RPC requests and responses over HTTP.&lt;br /&gt;
&lt;br /&gt;
==Calling Remote Procedures ==&lt;br /&gt;
&lt;br /&gt;
The calling procedure requests invokation of a function on a remote host using XML. A typical XML call looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;amp;lt;methodCall&amp;gt;&lt;br /&gt;
  &amp;amp;lt;methodName&amp;gt;FunctionName&amp;amp;lt;/methodName&amp;gt;&lt;br /&gt;
  &amp;amp;lt;params&amp;gt;&lt;br /&gt;
    &amp;amp;lt;param&amp;gt;&lt;br /&gt;
        &amp;amp;lt;value&amp;gt;&amp;amp;lt;DATATYPE&amp;gt;DATAVALUE&amp;amp;lt;/DATATYPE&amp;gt;&amp;amp;lt;/value&amp;gt;&lt;br /&gt;
    &amp;amp;lt;/param&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/params&amp;gt;&lt;br /&gt;
&amp;amp;lt;/methodCall&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The server invokes the function with the specified parameters then returns a response like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;amp;lt;methodResponse&amp;gt;&lt;br /&gt;
  &amp;amp;lt;params&amp;gt;&lt;br /&gt;
    &amp;amp;lt;param&amp;gt;&lt;br /&gt;
        &amp;amp;lt;value&amp;gt;&amp;amp;lt;DATATYPE&amp;gt;VALUE&amp;amp;lt;/DATATYPE&amp;gt;&amp;amp;lt;/value&amp;gt;&lt;br /&gt;
    &amp;amp;lt;/param&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/params&amp;gt;&lt;br /&gt;
&amp;amp;lt;/methodResponse&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Types==&lt;br /&gt;
&lt;br /&gt;
XML-RPC is generalized enough to transmit most any kind of data. The data types are specified like&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;DATATYPE&amp;gt;VALUE&amp;amp;lt;/DATATYPE&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The types include ''boolean'', ''dateTime'',''double'',''int'',''string''. Binary data has to be encoded at the sender in BASE-64 and decoded at the end by the receiver.  The encoded binary data is sent in a ''base64'' tag.  It also provides ''arrays'' and ''structs''.&lt;br /&gt;
&lt;br /&gt;
==XML-RPC Implementations ==&lt;br /&gt;
In order to use XML-RPC, you need to have an XML-RPC library installed for your platform and language. There are several available libraries, depending on your needs. You can find the list at http://www.xmlrpc.com/directory/1568/implementations.&lt;br /&gt;
&lt;br /&gt;
===XML-RPC for PHP(PHPXMLRPC)===&lt;br /&gt;
PHPXMLRPC is one of the several XML-RPC libraries for PHP. It is accessible at http://phpxmlrpc.sourceforge.net/&lt;br /&gt;
&lt;br /&gt;
In order to use this library, you need to download and unzip the package.  A handful of .inc files are included in the distribution for PHP clients and servers.  Include them in your PHP scripts to get access to the functionality in the library.  For example, xmlrpc.inc is the client library and xmlrpcs.inc is the server library.  It also provides several helper and wrapper functions that require other include files. You can look at the documentation to learn more about them. For this module, we will concentrate on client functionality.&lt;br /&gt;
&lt;br /&gt;
==== Creating a client in PHP ====&lt;br /&gt;
''xmlrpc_client'' is the XML-RPC client class. You can create a new client using&lt;br /&gt;
&lt;br /&gt;
 $client = new xmlrpc_client( server_url )&lt;br /&gt;
&lt;br /&gt;
It also supports basic HTTP authentication &lt;br /&gt;
&lt;br /&gt;
 $client=(  http(s)://user:password@server_url:port/path)&lt;br /&gt;
&lt;br /&gt;
For example, the following code creates a connection to the PHPXMLRPC demo server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?&lt;br /&gt;
include(&amp;quot;xmlrpc.inc&amp;quot;);&lt;br /&gt;
$client = new xmlrpc_client(&amp;quot;http://phpxmlrpc.sourceforge.net/server.php&amp;quot;);&lt;br /&gt;
?&amp;amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====PHPXMLRPC variables ====&lt;br /&gt;
The parameters(to or from remote procedures) are specified using the ''xmlrpcval'' function. This function can be called in different ways:&lt;br /&gt;
&lt;br /&gt;
 new xmlrpcval ( )&lt;br /&gt;
 new xmlrpcval ( string )&lt;br /&gt;
 new xmlrpcval ( VALUE, TYPE )&lt;br /&gt;
 new xmlrpcval ( ARRAY, ARRAY_TYPE )&lt;br /&gt;
&lt;br /&gt;
If ''xmlrpcval'' is called without a parameter, you need to set the variables by calling the ''addScalar'', ''addArray'' or ''addStruct'' functions to initialize the variable. If ''xmlrpcval'' is called with a string, the variable is assumed to be of type string, and the value is specified in the string parameter. A third option is to pass the value and type of a variable.  The fourth option is to create complex variables of arrays and structures.&lt;br /&gt;
&lt;br /&gt;
 $myInt = new xmlrpcvalue(1267, &amp;quot;int&amp;quot;);&lt;br /&gt;
 $myString = new xmlrpcvalue(&amp;quot;Hello, World!&amp;quot;, &amp;quot;string&amp;quot;);&lt;br /&gt;
 $myBool = new xmlrpcvalue(1, &amp;quot;boolean&amp;quot;);&lt;br /&gt;
 // note: this will serialize a php float value as xml-rpc string&lt;br /&gt;
 $myString2 = new xmlrpcvalue(1.24, &amp;quot;string&amp;quot;);&lt;br /&gt;
 $myArray = new xmlrpcval(&lt;br /&gt;
  array(&lt;br /&gt;
    new xmlrpcval(&amp;quot;Tom&amp;quot;),&lt;br /&gt;
    new xmlrpcval(&amp;quot;Dick&amp;quot;),&lt;br /&gt;
    new xmlrpcval(&amp;quot;Harry&amp;quot;)&lt;br /&gt;
  ),&lt;br /&gt;
  &amp;quot;array&amp;quot;);&lt;br /&gt;
 // recursive struct&lt;br /&gt;
 $myStruct = new xmlrpcval(&lt;br /&gt;
  array(&lt;br /&gt;
    &amp;quot;name&amp;quot; =&amp;gt; new xmlrpcval(&amp;quot;Tom&amp;quot;, &amp;quot;string&amp;quot;),&lt;br /&gt;
    &amp;quot;age&amp;quot; =&amp;gt; new xmlrpcval(34, &amp;quot;int&amp;quot;),&lt;br /&gt;
    &amp;quot;address&amp;quot; =&amp;gt; new xmlrpcval(&lt;br /&gt;
      array(&lt;br /&gt;
        &amp;quot;street&amp;quot; =&amp;gt; new xmlrpcval(&amp;quot;Fifht Ave&amp;quot;, &amp;quot;string&amp;quot;),&lt;br /&gt;
        &amp;quot;city&amp;quot; =&amp;gt; new xmlrpcval(&amp;quot;NY&amp;quot;, &amp;quot;string&amp;quot;)&lt;br /&gt;
      ), &lt;br /&gt;
      &amp;quot;struct&amp;quot;)&lt;br /&gt;
  ), &lt;br /&gt;
  &amp;quot;struct&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Once you have your variables ready, you can send them to the remote server.&lt;br /&gt;
&lt;br /&gt;
==== Preparing a message ====&lt;br /&gt;
&lt;br /&gt;
In order to make an RPC, first you need to prepare a message. This is done through the ''xmlrpcmsg'' class.&lt;br /&gt;
&lt;br /&gt;
 $message = new xmlrpcmsg(&amp;quot;REMOTE_FUNCTION&amp;quot;, PARAMETER_ARRAY);&lt;br /&gt;
&lt;br /&gt;
Remote function is the name of the function on the remote machine. Parameter array stores the parameters formed above. It can be skipped if the function does not take any parameters.&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code sends a message to the demo server to call an existing function:&lt;br /&gt;
&lt;br /&gt;
 $myParams=array(new xmlrpcval(23, &amp;quot;int&amp;quot;));&lt;br /&gt;
 $msg = new xmlrpcmsg(&amp;quot;examples.getStateName&amp;quot;, $myParams);&lt;br /&gt;
&lt;br /&gt;
==== Calling the function and receiving a response ====&lt;br /&gt;
The actual RPC is done when the client's ''send'' function is called.&lt;br /&gt;
&lt;br /&gt;
 $client-&amp;gt;return_type = 'phpvals';&lt;br /&gt;
 $resp = $client-&amp;gt;send($msg);&lt;br /&gt;
 if($resp-&amp;gt;faultCode())&lt;br /&gt;
   echo 'Error: ' . $resp-&amp;gt;faultString();&lt;br /&gt;
 else&lt;br /&gt;
   echo 'OK: got ' . $resp-&amp;gt;value();&lt;br /&gt;
&lt;br /&gt;
The client's return_type specifies either that the result should be stored in XML or as PHP variables. If there was an error during the call, the response's error code will be set. Otherwise, the value function returns the response value.&lt;br /&gt;
&lt;br /&gt;
===Perl Frontier  Module ===&lt;br /&gt;
Perl has a few different modules for XML-RPC.  We will use the Frontier::* modules, focusing on Frontier::Daemon which provides XML-RPC server functionality.&lt;br /&gt;
&lt;br /&gt;
You can initialize the daemon with the ''new'' function. The arguments include the port number to listen on and function names to serve to clients. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
use Frontier::Daemon;&lt;br /&gt;
&lt;br /&gt;
$methods = { 'myservice.convert' =&amp;gt; \&amp;amp;convert };&lt;br /&gt;
&lt;br /&gt;
Frontier::Daemon-&amp;gt;new(LocalPort =&amp;gt; 4873,  methods =&amp;gt; $methods) or die &amp;quot;Couldn't start HTTP server: $!&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will set up the daemon at port 4873 and the function ''convert'' is called from the remote clients as ''myservice.convert''.&lt;br /&gt;
In the following example, the daemon receives a Base64 encoded image, decodes, saves, and modifies it, and then sends the modified image back in Base64. Note that it assumes just single user is using the system at any time.  If multiple users access the same server the files could be overwritten.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
use MIME::Base64;&lt;br /&gt;
use Frontier::Daemon;&lt;br /&gt;
use Frontier::RPC2;&lt;br /&gt;
&lt;br /&gt;
$|=1;&lt;br /&gt;
print &amp;quot;server $$&amp;quot;;&lt;br /&gt;
$methods = { 'myservice.convert' =&amp;gt; \&amp;amp;convert };&lt;br /&gt;
&lt;br /&gt;
#initialize daemon&lt;br /&gt;
Frontier::Daemon-&amp;gt;new(LocalPort =&amp;gt; 4873,  methods =&amp;gt; $methods, ReuseAddr=&amp;gt;1) or die &amp;quot;Couldn't start HTTP server: $!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
sub convert&lt;br /&gt;
{&lt;br /&gt;
  ($name,$encoded) = @_; #client sends filename and file in base64&lt;br /&gt;
  print &amp;quot;Name:$name\n\n\n\n&amp;quot;;&lt;br /&gt;
  $decoded = MIME::Base64::decode($encoded);&lt;br /&gt;
  open(OUT,&amp;quot;&amp;gt;/tmp/$name&amp;quot;); #save the image&lt;br /&gt;
  print OUT $decoded;&lt;br /&gt;
  close(OUT);&lt;br /&gt;
  &lt;br /&gt;
  #put a label over the image&lt;br /&gt;
  system(&amp;quot;convert -pointsize 40 -draw \&amp;quot;text 200,200 'cse330'\&amp;quot; /tmp/$name /tmp/a2.jpg&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
  #read back the image&lt;br /&gt;
  open(IN,&amp;quot;&amp;lt;/tmp/a2.jpg&amp;quot;);&lt;br /&gt;
  binmode(IN);&lt;br /&gt;
  read IN,$de,1000000;&lt;br /&gt;
  close(OUT);&lt;br /&gt;
    &lt;br /&gt;
  #encode it&lt;br /&gt;
  $encoded = MIME::Base64::encode($de);&lt;br /&gt;
  #print $encoded;&lt;br /&gt;
  &lt;br /&gt;
  #send it back&lt;br /&gt;
  return {'success' =&amp;gt; &amp;quot;true&amp;quot;,'result' =&amp;gt; $encoded};&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the client side, you may have an upload page:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
  &amp;amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;image-xmlrpc.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
  Choose an image  file to upload: &amp;amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;amp;lt;br /&amp;gt;&lt;br /&gt;
  &amp;amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;amp;lt/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, a PHP based client that would send the uploaded image to the RPC server, and display the modified image.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?&lt;br /&gt;
  $uploadfile = $_FILES['uploadedfile']['tmp_name'];&lt;br /&gt;
  $handle = fopen($uploadfile,'r');&lt;br /&gt;
  $file_content = fread($handle,filesize($uploadfile)); //read uploaded file&lt;br /&gt;
  fclose($handle);&lt;br /&gt;
  $encoded = chunk_split(base64_encode($file_content)); //encode it&lt;br /&gt;
  include(&amp;quot;xmlrpc.inc&amp;quot;); //for xmlrpc operations&lt;br /&gt;
&lt;br /&gt;
  $client = new xmlrpc_client(&amp;quot;http://127.0.0.1:4873/RPC2&amp;quot;); //your host&lt;br /&gt;
 &lt;br /&gt;
  $myParams = array(new xmlrpcval($_FILES['uploadedfile']['name'], &amp;quot;string&amp;quot;),&lt;br /&gt;
                    new xmlrpcval($encoded, &amp;quot;string&amp;quot;));&lt;br /&gt;
 &lt;br /&gt;
  $message = new xmlrpcmsg(&amp;quot;myservice.convert&amp;quot;, $myParams);&lt;br /&gt;
  $resp = $client-&amp;gt;send($message);&lt;br /&gt;
  if ($resp-&amp;gt;faultCode())&lt;br /&gt;
  {&lt;br /&gt;
    echo 'KO. Error: '.$resp-&amp;gt;faultString();&lt;br /&gt;
  }&lt;br /&gt;
  else&lt;br /&gt;
  {&lt;br /&gt;
    $struct=$resp-&amp;gt;value();&lt;br /&gt;
    $retvalue = $struct-&amp;gt;structmem('success'); //value of the field success&lt;br /&gt;
    $resultval = $struct-&amp;gt;structmem('result'); //result field&lt;br /&gt;
    $result = $resultval-&amp;gt;scalarval(); //encoded image from the result&lt;br /&gt;
&lt;br /&gt;
    $decoded = base64_decode($result); //decode the base64 data&lt;br /&gt;
    $handle = fopen(&amp;quot;changed.jpg&amp;quot;,'w'); //save it to a new file&lt;br /&gt;
    $file_content = fwrite($handle,$decoded);&lt;br /&gt;
    fclose($handle);&lt;br /&gt;
&lt;br /&gt;
    echo &amp;quot;&amp;lt;img src='changed.jpg'/&amp;gt;&amp;lt;br&amp;gt;&amp;quot;; //display the new image&lt;br /&gt;
  }&lt;br /&gt;
?&amp;amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=212</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=212"/>
		<updated>2010-07-16T20:15:20Z</updated>

		<summary type="html">&lt;p&gt;Juliad: Protected &amp;quot;Javascript and AJAX&amp;quot; ([edit=sysop] (indefinite) [move=sysop] (indefinite))&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
Javascript is one of the most widely used scripting languages for preforming client-side functions in the web environment.  That is, the javascript code is interpreted and executed in a client's web browser.. not on the web server.  It is vaguely similar to Java, in that it is an object oriented language, but it was designed to be simpler for non-programmers to be able to accomplish common web processing tasks.&lt;br /&gt;
&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
Since the javascript code runs in the web browser, you'll have to use web browser tools for all debugging.  Generally speaking, if you have syntax or runtime errors in your javascript, the web browser will report nothing but not attempt to run the script.  So if it appears that your code is doing nothing, then it's likely a syntax error of some sort.  Fortunately, in Firefox, you can use the error console to view warnings and errors produced as javascripts are executed.  Go to Tools-&amp;gt;Error Console to access it.  This is most useful for simple syntatic debugging.  If you need a more serious debugger, you can use [http://getfirebug.com/ Firebug] for Firefox.&lt;br /&gt;
&lt;br /&gt;
=Asynchronous Javascript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that a large fraction of the work happens at the web browser, and larger requests happen behind the scenes, keeping waiting times for data to come back over the network to a minimum.  AJAX is generally also associated with higher-level APIs and libraries that aid in producing cleaner user interfaces for web applications.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   if(xmlHttp.readyState==4)&lt;br /&gt;
   {&lt;br /&gt;
     .... do some cool stuff ...&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are sent either in the url (to, for example, a PHP page) or through the ''send'' function.&lt;br /&gt;
Consider following PHP code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply ()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the x and y values are sent as part of the url as GET parameters. You can also send them as part of the ''send'' function, provided that your receiving page uses POST parameters.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
multiply-ajax-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply-post.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=jQuery=&lt;br /&gt;
&lt;br /&gt;
jQuery is a fairly lightweight javascript library that has a number of particularly useful features.  Even thought it's only been around for a few years now, it has gained a lot of momentum in the web development community.  jQuery provides a simple interface to easily modify CSS attributes on all the elements in the HTML document, has simplified AJAX integration, and is accompanied by a number of handy user interface related APIs.  You can get a good overview of jQuery in the jQuery [http://docs.jquery.com/Tutorials:How_jQuery_Works Tutorials], and there is a short summary on [http://en.wikipedia.org/wiki/JQuery Wikipedia].  Of course, there are plenty of other tutorials a google search away.&lt;br /&gt;
&lt;br /&gt;
To use jQuery in your javascript, you need to include the jQuery javascript library, which is just another javascript file.  It's freely available online at the jQuery [http://docs.jquery.com/Downloading_jQuery website].  Assuming you've downloaded the file to your web server, put it in the directory with your PHP and HTML files, and renamed it ''jquery.js'', you include it by adding this line to the head section of your HTML of PHP file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;jquery.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also link to the jQuery library directly on other websites as mentioned in the above tutorials.  The trade-off is that having a local copy will load faster, but then requires you to manage the library files your self (e.g., downloading new versions).&lt;br /&gt;
&lt;br /&gt;
A few code snippets are given below, but the best way to learn about all the features of jQuery is to head to the [http://jquery.com jQuery website] and look through the tutorials, references, and examples there.&lt;br /&gt;
&lt;br /&gt;
The ''$'' symbol is used to denote jQuery functionality.  You can use it to call jQuery functions, access HTML document elements in order to read or write values, or modify style information via CSS for a range of document elements.  For example, here is some code that sets up a function to be called whenever the user clicks on a link:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $(&amp;quot;a&amp;quot;).click( function(event)&lt;br /&gt;
 {&lt;br /&gt;
   event.preventDefault();&lt;br /&gt;
   $(this).hide(&amp;quot;slow&amp;quot;);&lt;br /&gt;
 } );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code leverages many useful javascript and jQuery features.  ''$(&amp;quot;a&amp;quot;)'' is the jQuery way of selecting all HTML ''a'' elements.  That is, in this case, all elements in the HTML document that are ''&amp;lt;a&amp;gt;'' tags.  The ''click'' function is invoked for all such ''a'' elements in the document.  The argument to the ''click'' function is another function that is to be called when the selected elements are clicked by the user.  The ''event'' argument to this user-defined function is a default argument sent to functions which handle user actions like clicking a mouse button.  In this example, the code calls another function, ''preventDefault()'' on the event passed to stop the standard action on that event.  Here that means stopping the web browser from following the link that the user clicked on.  Finally, the ''$(this)'' variable is the jQuery way of accessing the current encapsulating object, as in Java.  This example calls the jQuery hide function on the object to make the object (again, in this case, ''this'' is referring to the ''&amp;lt;a&amp;gt;'' tag that the user clicked) disappear from the browser.  The script also passes an argument to the hide function to make the link disappear slowly as described in the API [http://docs.jquery.com/Hide documentation].&lt;br /&gt;
&lt;br /&gt;
jQuery also comes with some easy to use widgets for building better user interfaces.  One of the handiest is the [http://docs.jquery.com/UI/Dialog dialog] widget that pops-up an in-browser dialog window.  To use the dialog functions, you need to include a few more javascript libraries in your HTML header block like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;link type=&amp;quot;text/css&amp;quot; href=&amp;quot;http://jqueryui.com/latest/themes/base/ui.all.css&amp;quot; rel=&amp;quot;stylesheet&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/jquery-1.3.2.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.core.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.draggable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.resizable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.dialog.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
   // your code goes here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can then use the ''$().dialog()'' function to make dialog boxes.  The ''dialog'' function can take many arguments to customize its behavior and add standard elements like buttons; see the documentation for details.  The content of the dialog box is typically placed in a ''div'' container and then that container is hidden when the document loads.  You can put any elements in the ''div'' and they will be displayed as normal in the dialog box.  Here is a full example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&lt;br /&gt;
 &amp;lt;head&amp;gt;&lt;br /&gt;
  &amp;lt;style&amp;gt;&lt;br /&gt;
    #mydialog { display:none }&lt;br /&gt;
  &amp;lt;/style&amp;gt;&lt;br /&gt;
  &amp;lt;link type=&amp;quot;text/css&amp;quot; href=&amp;quot;http://jqueryui.com/latest/themes/base/ui.all.css&amp;quot; rel=&amp;quot;stylesheet&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/jquery-1.3.2.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.core.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.draggable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.resizable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.dialog.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
    function showdialog()&lt;br /&gt;
    {&lt;br /&gt;
      $(&amp;quot;#mydialog&amp;quot;).dialog();&lt;br /&gt;
    }&lt;br /&gt;
  &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  &amp;lt;input type=&amp;quot;button&amp;quot; value=&amp;quot;Show Dialog&amp;quot; onclick=showdialog() /&amp;gt;&lt;br /&gt;
  &amp;lt;div id=&amp;quot;mydialog&amp;quot; title=&amp;quot;Howdy&amp;quot;&amp;gt;Look at me!&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To understand the page, start at the bottom.  There is a ''div'' element with a title and some string content.  It also has a unique id (the ids can be anything you want) which is used to identify this particular element in the rest of the document.  This div's id is ''mydialog''.  Above that is a button input.  The only thing special about it is the ''onclick'' attribute which specifies a javascript function to call when the button is clicked by a user.  In this case, the function is ''showdialog()'' which is a user defined javascript function contained in the document header.  All the function does is access the document element with id ''mydialog'' (the ''#identifier'' is the jQuery way of accessing an individual element based on the assigned id in the element attribute list), and build a dialog box from it.  Here, the dialog box will have the title ''Howdy'' from the title attribute in the ''mydialog'' element, and have a text string ''Look at me!''.  Finally, at the top of the document is the ''style'' section, which is a normal HTML/CSS section describing appearance of parts of the document.  There is only one entry, again describing the document element with id ''mydialog''.  One style attribute is set, namely the ''display'' attribute is set to ''none'' to indicate that the element should not be displayed.  Otherwise, the ''mydialog'' div would be displayed like any other part of the document rather than being used solely for our dialog box.&lt;br /&gt;
&lt;br /&gt;
Here is another example of using jQuery to do simple animations by moving an image across the browser window:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
  // these are for storing target locations&lt;br /&gt;
  var targetLeft=0;&lt;br /&gt;
  var targetTop=0;&lt;br /&gt;
  function moveTarget()&lt;br /&gt;
  {&lt;br /&gt;
    targetLeft+=10; //increase target location&lt;br /&gt;
    targetTop+=10; &lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
    if(targetLeft&amp;amp;lt;200) &lt;br /&gt;
    { //stop if we reached left=200&lt;br /&gt;
      window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready( function() &lt;br /&gt;
  { //at the beginning create a clock timeout&lt;br /&gt;
    window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
  });&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
&amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Other Resources=&lt;br /&gt;
There are a number of other w3schools tutorials and references that you may find useful, including [http://www.w3schools.com/php/default.asp PHP] (note the PHP and AJAX section), [http://www.w3schools.com/htmldom/default.asp DOM], [http://www.w3schools.com/ajax/default.asp AJAX], and [http://www.w3schools.com/css/default.asp CSS].  Many other resources exist around the web, and you should feel free to incorporate any existing libraries or other javascript or AJAX-related tools.  For example, the [http://code.google.com/apis/ajaxlibs/ Google AJAX APIs].&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=211</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=211"/>
		<updated>2010-07-16T20:15:04Z</updated>

		<summary type="html">&lt;p&gt;Juliad: Protected &amp;quot;Databases and MySQL&amp;quot; ([edit=sysop] (indefinite) [move=sysop] (indefinite))&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 yum install mysql-server &lt;br /&gt;
&lt;br /&gt;
next enable then start the service with the following commands&lt;br /&gt;
 sudo /sbin/chkconfig mysqld&lt;br /&gt;
 sudo /sbin/service mysqld start&lt;br /&gt;
After the server is installed, enter the command &lt;br /&gt;
  mysqladmin -u root password 'your-password-here'&lt;br /&gt;
to choose a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. Currently there are only rpms in the yum repository for the old version which does not yet support php 5.3. To download and install the phpMyAdmin software, find the appropriate download url [http://www.phpmyadmin.net/home_page/downloads.php here], and then use &lt;br /&gt;
 wget download_url&lt;br /&gt;
to download it to your instance's src directory or wherever you'd like to download the tar ball. Next follow the installation/configuration instructions [http://wiki.phpmyadmin.net/pma/Quick_Install here]  &lt;br /&gt;
This install process may not work with php 5.3, but may merit further investigation.&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then select ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. The format of ''grant'' is&lt;br /&gt;
&lt;br /&gt;
 grant PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
====Inserting Entries Manually====&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
====Loading Data From a File====&lt;br /&gt;
The ''load data infile'' command populates a table using data stored in a text file. The general form of the command is:&lt;br /&gt;
 LOAD DATA INFILE 'dataFile.txt' INTO TABLE table_name;&lt;br /&gt;
where ''dataFile.txt'' is located relative to the server's data directory if a path name with leading components is given, otherwise if no leading components are given, the server assumes the file is in the default database's database directory. Also, if an absolute path is specified, the server uses it.&lt;br /&gt;
&lt;br /&gt;
Alternatively, and quite usefully, you can load data from a local file (a file residing on the same machine as the client) using the LOCAL keyword.&lt;br /&gt;
when the local keyword is used, it is best to specify an absolute path as in the following:&lt;br /&gt;
 LOAD DATA LOCAL INFILE '/home/linus_torvalds/Documents/dataFile.txt'&lt;br /&gt;
&lt;br /&gt;
By default the text files are assumed to have tab-delimited columns with column values specified in the same order as they are defined in the table (though you can specify additional/alternative delimiters using additional options).&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
====Joins====&lt;br /&gt;
To quote the [http://en.wikipedia.org/wiki/Join_%28SQL%29 wikepedia entry on joins], &lt;br /&gt;
A JOIN is a means for combining fields from two [or more] tables by using values common to each.&lt;br /&gt;
&lt;br /&gt;
See the tutorial [http://www.webdesign.org/web-programming/php/mysql-join-tutorial.14876.html here] for a good explanation of how to use joins.&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
==Keys==&lt;br /&gt;
&lt;br /&gt;
===Primary Keys===&lt;br /&gt;
A primary key is one or many data fields, which taken together can uniquely identify a row in a table.&lt;br /&gt;
Therefore, there cannot be rows with equivalent primary keys within one table.&lt;br /&gt;
&lt;br /&gt;
See the Wikipedia entry on [http://en.wikipedia.org/wiki/Unique_key unique keys]:&lt;br /&gt;
Note that primary keys are a more specific case of unique keys.&lt;br /&gt;
&lt;br /&gt;
====Examples====&lt;br /&gt;
Use the following syntax to define a primary key in a CREATE TABLE statement &lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               employee_id INT, first_name CHAR(50), last_name CHAR(50),&lt;br /&gt;
               PRIMARY KEY(employee_ID)&lt;br /&gt;
              );&lt;br /&gt;
&lt;br /&gt;
There is an abbreviated syntax for declaring primary keys comprised of only one field. Here is the CREATE TABLE statement to define the table using the shorter syntax:&lt;br /&gt;
 CREATE TABLE(&lt;br /&gt;
               employee_id INT PRIMARY KEY,  first_name CHAR(50), last_name CHAR(50)&lt;br /&gt;
             );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the following command which creates a table containing the dates of the last emissions check for cars identified by their license plates' state and numbers.&lt;br /&gt;
 CREATE TABLE( &lt;br /&gt;
               state CHAR(2), license_plate_number CHAR(7), last_emissions_check DATE,&lt;br /&gt;
               PRIMARY KEY(state, license_plate_number)&lt;br /&gt;
             );&lt;br /&gt;
 &lt;br /&gt;
The plate number alone is not enough to uniquely identify a row because there may be a car with the same plate number from a different state. So we define what is called a ''composite primary key'' which uses both the state and license_plate_number fields. ''Composite keys'' are so named because they are composed of multiple fields.&lt;br /&gt;
&lt;br /&gt;
===Foreign Keys===&lt;br /&gt;
&lt;br /&gt;
See the wikipedia entry for [http://en.wikipedia.org/wiki/Foreign_key foreign keys]&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with yum install.  If it's already on your system, yum install will not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. &lt;br /&gt;
&lt;br /&gt;
Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
Note that the connection automatically closes as soon as the script finishes execution.&lt;br /&gt;
However, you can explicitly close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The [http://us2.php.net/manual/en/ref.mysql.php PHP manual] has fairly detailed descriptions of each mysql related function, it is a great reference if you're not sure of how to do something in PHP.&lt;br /&gt;
&lt;br /&gt;
==Protecting against SQL Injection Attacks==&lt;br /&gt;
SQL injection attacks pose a major security risk to the security and integrity of your database if you do not protect from them.&lt;br /&gt;
&lt;br /&gt;
See [http://www.learnphponline.com/security/sql-injection-prevention-mysql-php this] very helpful tutorial on protecting against SQL injection.&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=210</id>
		<title>PHP Original</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=PHP_Original&amp;diff=210"/>
		<updated>2010-07-16T20:14:48Z</updated>

		<summary type="html">&lt;p&gt;Juliad: Protected &amp;quot;PHP&amp;quot; ([edit=sysop] (indefinite) [move=sysop] (indefinite))&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= PHP5 =&lt;br /&gt;
PHP is a server-side language. When a web request for PHP file comes in, the web server processes the PHP file to produce HTML output.  That is, the main function of most PHP scripts is to dynamically create HTML content.  PHP5 support for Apache is provided by mod_php5. In Ubuntu, you can provide PHP5 support by installing ''php5''. This will install the apache2 module and the php5 command line interpreter (useful for debugging). PHP files may have different extensions, but ''.php'' is the most common.  In the Apache configuration, you can specify which extensions are going to be treated as PHP scripts by adding the extensions to the list in the ''/etc/httpd/conf.d/php.conf'' file: &lt;br /&gt;
&lt;br /&gt;
  AddType text/html .php .phtml .php3&lt;br /&gt;
&lt;br /&gt;
==Declaring PHP==&lt;br /&gt;
If a PHP file is requested by a web user, Apache parses and interprets the file.  Any code declared between PHP tags is then executed and the total HTML output is sent back to the user. PHP code can be declared in a script file in any of the following ways, with the first being the most common:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 php?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script language=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
   PHP Code In Here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, edit a new PHP file named ''info.php'', and put these lines in the file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   phpinfo();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Put the file in the web directory for your user account (remember that this is ''~username/.html/'' if you completed the first Module).  Then access the web page by going to ''http://server/~username/info.php''. You will see something similar to the picture below:&lt;br /&gt;
&lt;br /&gt;
[[Image:Php_info.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you look at the HTML code, you will see quite a lot of HTML output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;DTD/xhtml1-transitional.dtd&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&lt;br /&gt;
 body {background-color: #ffffff; color: #000000;}&lt;br /&gt;
 body, td, th, h1, h2 {font-family: sans-serif;}&lt;br /&gt;
 pre {margin: 0px; font-family: monospace;}&lt;br /&gt;
 a:link {color: #000099; text-decoration: none; background-color: #ffffff;}&lt;br /&gt;
 a:hover {text-decoration: underline;}&lt;br /&gt;
 table {border-collapse: collapse;}&lt;br /&gt;
 .center {text-align: center;}&lt;br /&gt;
 .center table { margin-left: auto; margin-right: auto; text-align: left;}&lt;br /&gt;
 .center th { text-align: center !important; }&lt;br /&gt;
 td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}&lt;br /&gt;
 h1 {font-size: 150%;}&lt;br /&gt;
 h2 {font-size: 125%;}&lt;br /&gt;
 .p {text-align: left;}&lt;br /&gt;
 .e {background-color: #ccccff; font-weight: bold; color: #000000;}&lt;br /&gt;
 .h {background-color: #9999cc; font-weight: bold; color: #000000;}&lt;br /&gt;
 .v {background-color: #cccccc; color: #000000;}&lt;br /&gt;
 .vr {background-color: #cccccc; text-align: right; color: #000000;}&lt;br /&gt;
 img {float: right; border: 0px;}&lt;br /&gt;
 hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}&lt;br /&gt;
 &amp;lt;/style&amp;gt;&lt;br /&gt;
 &amp;lt;title&amp;gt;phpinfo()&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
 &amp;lt;body&amp;gt;&amp;lt;div class=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;table border=&amp;quot;0&amp;quot; cellpadding=&amp;quot;3&amp;quot; width=&amp;quot;600&amp;quot;&amp;gt;&lt;br /&gt;
 ....&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, none of that HTML was in the ''info.php'' file.  Instead, the ''phpinfo()'' line is a built-in PHP function that produces all of that HTML.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Variables require ''$'' at the beginning of their names (regardless of the purpose, be it setting, be it accessing). You don't need to specify the type of the variable; PHP will use the variable as the correct type based on how it is used in the code.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   #this is an integer&lt;br /&gt;
   $i=5;&lt;br /&gt;
   #this is a string&lt;br /&gt;
   $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in many other languages, statements end with a '';''.  To actually produce HTML output, you simply print the HTML from the PHP code.  The simplest way is to use the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
If you use a variable inside of a string, the value of the variable will be put in the string (you might also look at the ''echo'' function):&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   $i=5;&lt;br /&gt;
   print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will generate the string&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
If you create a file with just the above code, you will notice that your browser will only receive the above line. That is, there is no ''html'' tag or anything else. Thus, PHP files are normally a mix of HTML code and PHP code.&lt;br /&gt;
Most PHP scripts, then, look more like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code that the browser will receive is then:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  The value of i is 5&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can have any number of PHP segments and HTML segments in a PHP file.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;? $i=5; ?&amp;gt;&lt;br /&gt;
The value of i is &amp;lt;?  print &amp;quot;$i&amp;quot;; ?&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, whatever you print out will be interpreted as HTML by the client's browser, so you can (and will) use HTML tags in the output you produce.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;? print &amp;quot;This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt; ?&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in the following at the user's browser:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 This is a &amp;lt;b&amp;gt; test &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which would then render ''test'' in bold.&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
&lt;br /&gt;
Declaring arrays is very easy in PHP. Either you can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
$student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
$student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
$student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or alternatively&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student=array(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you can access the contents with an index into the array, as normal:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find the size of an array with ''count'' function, e.g., ''count($student)''&lt;br /&gt;
&lt;br /&gt;
In PHP, you can also have associative arrays, which are arrays whose members can be accessible with string indices. Here is an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
$test[&amp;quot;test1&amp;quot;]=1;&lt;br /&gt;
$test[&amp;quot;test2&amp;quot;]=2;&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;test1=&amp;quot;,$test[test1],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
echo &amp;quot;test2=&amp;quot;,$test[test2],&amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access array keys through ''array_keys'' function.&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if/elseif/else'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   some statements&lt;br /&gt;
 }&lt;br /&gt;
 elseif( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   more statements&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
    yet more statements&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string == &amp;quot;this is my string&amp;quot;) {&lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such thing as global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
PHP has ''for'' and ''while'' loops, similar to Java and C:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, the following code will print the sum of numbers from 1 to 10.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 echo &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The advantage of loops  becomes more obvious when you have to create a long table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Number&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Square&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
for($i=0;$i&amp;lt;100;$i++) {&lt;br /&gt;
echo &amp;quot;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;$i &amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;quot;,$i*$i,&amp;quot;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a table with two columns, each row contains a number and its square. &lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt normal control flow through a loop.  ''break'' jumps out of the loop entirely, while ''continue'' jumps to the end of the current loop iteration and flow resumes with the next loop iteration.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
PHP functions are identified with the keyword ''function'' followed by a function name and argument list for the function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
function name($arg1 [= constant], $arg2 [= constant], ...) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;? &lt;br /&gt;
   function add($x,$y) {&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
   }&lt;br /&gt;
   echo &amp;quot;Sum of 2+5=&amp;quot;,add(2,5); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If an argument has a default value, it can be specified with the argument on the list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;?&lt;br /&gt;
   function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
      print &amp;quot;hello $name how are you today?&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
   helloMsg(&amp;quot;Alice&amp;quot;);&lt;br /&gt;
   helloMsg();&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 hello Alice how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
 hello there how are you today?&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Passing Variables ==&lt;br /&gt;
Input values can be passed to a script either through the URL request line or through data submitted by a form. Built in arrays $_GET['var'] and $_POST['var'] return the value of ''var'' depending on the access method. As we will see in the next section, values can also be passed through session variables. Finally, you can use cookies to store and retrieve values.&lt;br /&gt;
&lt;br /&gt;
===Get: Passing Variables via URL===&lt;br /&gt;
If you want to pass values in a URL, the format is ''http://yourserver/yourphpfile.php?var1=value1&amp;amp;var2=value2.....''&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code would be used to print the previous hello message according to a name given in the URL:&lt;br /&gt;
&lt;br /&gt;
greeting.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $person= $_GET['name'];&lt;br /&gt;
 helloMsg($person);&lt;br /&gt;
 function helloMsg($name=&amp;quot;there&amp;quot;) {&lt;br /&gt;
     print &amp;quot;hello $name how are you today?&amp;lt;/br&amp;gt;&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you access the URL like this:  ''greeting.php?name=Alice'', it will show ''hello Alice how are you today?''. However, if you access it with ''greeting.php'', it will print ''hello how are you today?''. Note that it does not use the default value ''there'' since ''$name'' is now a string (in this case, an empty string) and so there is argument being being passed to ''helloMsg''.&lt;br /&gt;
&lt;br /&gt;
===POST: Passing Variables via Form===&lt;br /&gt;
In PHP one method for passing variables from page to page is by sending it to the other page via posting. The way post works in&lt;br /&gt;
PHP is that PHP retrieves the variable names and values from a (slightly modified) html form and sends an those variables in the associative array named $_POST to the page specified in the FORM tag's ''action'' attribute. &lt;br /&gt;
&lt;br /&gt;
Note that receiving the data from a form is very similar to getting it from the URL. Consider the following form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;FORM action=&amp;quot;info.php&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;P&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;firstnamelabel&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;firstname&amp;quot; id=&amp;quot;firstnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;lastnamelabel&amp;quot;&amp;gt;Last name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;lastname&amp;quot; id=&amp;quot;lastnamelabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;LABEL for=&amp;quot;birthyearlabel&amp;quot;&amp;gt;Birth Year: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;text&amp;quot; name=&amp;quot;birthyear&amp;quot; id=&amp;quot;birthyearlabel&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Male&amp;quot;&amp;gt; Male&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;radio&amp;quot; name=&amp;quot;gender&amp;quot; value=&amp;quot;Female&amp;quot;&amp;gt; Female&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;submit&amp;quot; value=&amp;quot;Send&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;INPUT type=&amp;quot;reset&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;/P&amp;gt;&lt;br /&gt;
&amp;lt;/FORM&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This posts the form data to ''info.php'', with inputs ''firstname'', ''lastname'', ''birthyear'', and ''gender''.&lt;br /&gt;
The values passed through the form for each input can be accessed in ''info.php'' with the $_POST array.&lt;br /&gt;
*&amp;lt;label&amp;gt; : The label tag just provides a text label for the form inputs.&lt;br /&gt;
*&amp;lt;input ''type=&amp;quot;input_type_here&amp;quot;''&amp;gt; : The input tag provides various means of inputting information into the form, there are various types of inputs such as text, radio, submit, reset, etc. &lt;br /&gt;
*&amp;lt;input ... ''name=&amp;quot;input_name&amp;quot;''&amp;gt; : &lt;br /&gt;
*&amp;lt;label ''for=&amp;quot;identifying_string&amp;quot;''&amp;gt; and &amp;lt;INPUT ... ''id=&amp;quot;identifying_string&amp;quot;'': The optional ''for'' attribute of the label tag associates the label with what we'll call an &amp;quot;identifying string&amp;quot;, which should match the string specified in the corresponding ''id'' portion of the input tag. If both of these attributes are in place, then upon clicking on the label, the corresponding input will be selected.&lt;br /&gt;
&lt;br /&gt;
info.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 echo  &amp;quot;Personal Data:&amp;quot;, $_POST['firstname'], &amp;quot; &amp;quot;, $_POST['lastname'], &amp;quot;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 $birthyear=$_POST['birthyear'];&lt;br /&gt;
 echo  &amp;quot;Birth year:&amp;quot;, $_birthyear, &amp;quot; (Age:&amp;quot; , 2007-$birthyear, &amp;quot;)&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
 echo &amp;quot;Gender: &amp;quot;, $_POST['gender'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For further help with using HTML forms with PHP, we recommend [http://www.tizag.com/phpT/forms.php this tutorial]&lt;br /&gt;
===Self-Submitting Forms===&lt;br /&gt;
Oftentimes we don't want to pass form values to a different page, but rather to the same page as contains the form so that we can process the information and display output on that same page. The way we do this is to specify that the form should submit to $_SERVER['PHP_SELF'] and not say, other_page.php.&lt;br /&gt;
Note that you can make self-submitting forms that use either the get or post method.&lt;br /&gt;
&lt;br /&gt;
The following example simply takes the data input from a form and, via posting, echoes it back to the user as bold text on the same page:&lt;br /&gt;
bold_printer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Bold Printer&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action= &amp;lt;?php echo $_SERVER['PHP_SELF'] ?&amp;gt; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;label for=&amp;quot;text_field&amp;quot;&amp;gt;First name: &amp;lt;/LABEL&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;user_text&amp;quot; id=&amp;quot;text_field&amp;quot;&amp;gt;&amp;lt;BR&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Print in Bold&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    if(isset($_POST['user_text'])&lt;br /&gt;
    echo &amp;quot;&amp;lt;b&amp;gt;&amp;quot;, $_POST['user_text'], &amp;quot;&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Session Variables==&lt;br /&gt;
PHP provides a global array, ''$_SESSION'', for values that are available globally throughout a ''session''. The values will stay there until either you remove them in a PHP script, the user's browser is closed, or their session expires (you can tune the timeout period in the PHP configuration files).&lt;br /&gt;
&lt;br /&gt;
In order to access session values, you should first initiate the session with the ''session_start()'' function call. After that, when you access a values with ''$_SESSION'', it will become a session values. For example:&lt;br /&gt;
&lt;br /&gt;
setsessionvars.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$_SESSION['SESSION']=42;&lt;br /&gt;
$_SESSION['username']=&amp;quot;ArthurDent&amp;quot;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This sets two variables, ''SESSION'' and ''username'' to be accessible by other pages in this session. In another page, you can access them like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
if (!isset($_SESSION['SESSION'])) {&lt;br /&gt;
  echo &amp;quot;error session is not registered&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else echo &amp;quot;username=&amp;quot;,$_SESSION['username'];&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also remove a session variable from the session with unset command:&lt;br /&gt;
&lt;br /&gt;
 unset($_SESSION['SESSION']);&lt;br /&gt;
&lt;br /&gt;
==Redirect the browser to a different page==&lt;br /&gt;
&lt;br /&gt;
It is often useful to redirect the user's browser to a different webpage, for example, after processing inputs from a form.  Here is a simple example that shows how to do this, based on whether or not the ''LOGIN'' session variable is set:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
if(isset($_SESSION[&amp;quot;LOGIN&amp;quot;])){&lt;br /&gt;
	Header(&amp;quot;Location:/welcome.php&amp;quot;);&lt;br /&gt;
}else{&lt;br /&gt;
	Header(&amp;quot;Location:/login.php&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that anytime you call the ''Header'' function, it is adding information to the header section in the HTML object that is being requested.  As such, all ''Header'' calls should go before any other HTML output in the PHP script.&lt;br /&gt;
&lt;br /&gt;
==Sending a file to the Browser==&lt;br /&gt;
&lt;br /&gt;
Another useful function to know is the ''readfile'' function.  It takes a file on the webserver's file system and sends it to the web browser.  A simple example is given below, but note that you'll probably need to specify addition header fields in order to have the web browser download the file correctly. &lt;br /&gt;
For those of you who have used previous versions of PHP, note that mime_content_type($path) is now deprecated, use finfo_file instead&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
session_start();&lt;br /&gt;
&lt;br /&gt;
$file=$_GET['file'];&lt;br /&gt;
$name = $_SESSION[&amp;quot;UNAME&amp;quot;];&lt;br /&gt;
$path=&amp;quot;/home/uploads/$name/$file&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension&lt;br /&gt;
if (!$finfo) {&lt;br /&gt;
 	echo &amp;quot;Opening fileinfo database failed&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$mime=finfo_file($finfo, $path);&lt;br /&gt;
finfo_close($finfo);&lt;br /&gt;
&lt;br /&gt;
header('content-type:'. $mime);&lt;br /&gt;
readfile($path);    &lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Uploading files with PHP==&lt;br /&gt;
&lt;br /&gt;
This section describes one way to upload files with PHP.  First, you need an HTML form that contains an input with type ''file'':&lt;br /&gt;
&lt;br /&gt;
transfer.php&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;uploader.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
Choose a file to upload: &amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you use a ''file'' input in a form, PHP automatically adds an entry in the ''$_FILES'' array with information about the file. For example, transfer.php called the file ''uploadedfile'', so $_FILES['uploadedfile'] refers to file that the user selected in the form.  The name of the original file is $_FILES['uploadedfile']['name'].   When you upload a file, it is put in a temporary location by the web server. This temporary name is $_FILES['uploadedfile']['tmp_name']. In PHP, you can move a file from its original location to a new location by calling a built-in function, ''move_uploaded_file'', which takes an argument for the source (temporary location) and destination location. Note that relative paths in the destination location are relative to the location of the PHP script being executed.&lt;br /&gt;
&lt;br /&gt;
uploader.php:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$target_path = &amp;quot;uploads/&amp;quot;;&lt;br /&gt;
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);&lt;br /&gt;
&lt;br /&gt;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {&lt;br /&gt;
    echo &amp;quot;The file &amp;quot;.  basename( $_FILES['uploadedfile']['name']) . &amp;quot; has been uploaded&amp;quot;;&lt;br /&gt;
} else{&lt;br /&gt;
    echo &amp;quot;There was an error uploading the file, please try again!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Eclipse ==&lt;br /&gt;
&lt;br /&gt;
For those of you who like programming with IDEs, Eclipse has support for PHP.  See:&lt;br /&gt;
http://www.eclipse.org.&lt;/div&gt;</summary>
		<author><name>Juliad</name></author>
		
	</entry>
</feed>