<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2359453265019837844</id><updated>2012-01-18T10:53:53.061-08:00</updated><category term='LANGUAGES'/><category term='C AND C++'/><category term='AJAX'/><category term='MYSQL AND PHP'/><title type='text'>WORLD OF W3C</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://w3cworld.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2359453265019837844/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://w3cworld.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Imran Malik</name><uri>http://www.blogger.com/profile/13742779381556383378</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='http://3.bp.blogspot.com/_bYJnR7vm4kE/TJtq9K9FMdI/AAAAAAAABmk/EtUpHKcAerw/S220/ASDF+(128).jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>6</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2359453265019837844.post-2606528981533965151</id><published>2009-06-01T22:55:00.000-07:00</published><updated>2009-06-02T05:34:01.761-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='AJAX'/><title type='text'>SIMPLE AJAX EXAMPLES</title><content type='html'>In this article I don't want to show you the history of AJAX and discuss its pros and cons, but only focus on how to create a basic working AJAX - PHP communication.&lt;br /&gt;&lt;br /&gt;The only important thing at the moment is that AJAX uses JavaScript so it need to be enabled in your browser to successfully complete this tutorial.&lt;br /&gt;&lt;br /&gt;To demonstrate the AJAX PHP connection we will create a very simple form with 2 input fields. In the first field you can type any text and we will send this text to our PHP script which will convert it to uppercase and sends it back to us. At the end we will put the result into the second input field. ( The example maybe not very useful but I think it is acceptable at this level. )&lt;br /&gt;&lt;br /&gt;So let's list what we need to do:&lt;br /&gt;&lt;br /&gt;    * Listen on key-press event on the input field.&lt;br /&gt;    * In case of key-press send a message to the PHP script on the server.&lt;br /&gt;    * Process the input with PHP and send back the result.&lt;br /&gt;    * Capture the returning data and display it.&lt;br /&gt;&lt;br /&gt;Our html code is very simple it looks like this:&lt;br /&gt;&lt;br /&gt;Code:&lt;br /&gt;&lt;br /&gt;   1.&lt;br /&gt;      &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"&lt;br /&gt;   2.&lt;br /&gt;      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;&lt;br /&gt;   3.&lt;br /&gt;      &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;br /&gt;   4.&lt;br /&gt;      &lt;head&gt;&lt;br /&gt;   5.&lt;br /&gt;      &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;&lt;br /&gt;   6.&lt;br /&gt;      &lt;title&gt;Ajax - PHP example&lt;/title&gt;&lt;br /&gt;   7.&lt;br /&gt;      &lt;/head&gt;&lt;br /&gt;   8.&lt;br /&gt;       &lt;br /&gt;   9.&lt;br /&gt;      &lt;body&gt;&lt;br /&gt;  10.&lt;br /&gt;       &lt;br /&gt;  11.&lt;br /&gt;      &lt;form name="testForm"&gt;&lt;br /&gt;  12.&lt;br /&gt;      Input text: &lt;input type="text" onkeyup="doWork();" name="inputText" id="inputText" /&gt;&lt;br /&gt;  13.&lt;br /&gt;      Output text: &lt;input type="text" name="outputText" id="outputText" /&gt;&lt;br /&gt;  14.&lt;br /&gt;      &lt;/form&gt;&lt;br /&gt;  15.&lt;br /&gt;      &lt;/body&gt;&lt;br /&gt;  16.&lt;br /&gt;      &lt;/html&gt;&lt;br /&gt;&lt;br /&gt;Javascript F1&lt;br /&gt;&lt;br /&gt;As you can see there is a doWork() function which is called in every case when a key is up (a key was pressed). Of course you can use any other supported events if you want. &lt;br /&gt;&lt;br /&gt;But what is this doWork() and how we can send messages to the server script? On the next page you will find the answers.&lt;br /&gt;&lt;br /&gt;Step 2 - Sending data to PHP with Ajax&lt;br /&gt;&lt;br /&gt;Ajax PHP tutorial&lt;br /&gt;&lt;br /&gt;Before the explanation of the doWork() function we first need to learn a more important thing. To make a communication between the client and the server the client code needs to create a so called XMLHttpRequest object. This object will be responsible for AJAX PHP communication.&lt;br /&gt;&lt;br /&gt;However creating this object is bit triky as the browser implement it various ways. If you don't want to support the quite old browsers we can do it as follows:&lt;br /&gt;&lt;br /&gt;Code:&lt;br /&gt;&lt;br /&gt;   1.&lt;br /&gt;      // Get the HTTP Object&lt;br /&gt;   2.&lt;br /&gt;      function getHTTPObject(){&lt;br /&gt;   3.&lt;br /&gt;      if (window.ActiveXObject) &lt;br /&gt;   4.&lt;br /&gt;      return new ActiveXObject("Microsoft.XMLHTTP");&lt;br /&gt;   5.&lt;br /&gt;      else if (window.XMLHttpRequest) &lt;br /&gt;   6.&lt;br /&gt;      return new XMLHttpRequest();&lt;br /&gt;   7.&lt;br /&gt;      else {&lt;br /&gt;   8.&lt;br /&gt;      alert("Your browser does not support AJAX.");&lt;br /&gt;   9.&lt;br /&gt;      return null;&lt;br /&gt;  10.&lt;br /&gt;      }&lt;br /&gt;  11.&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;Javascript F1&lt;br /&gt;&lt;br /&gt;Ok, now we have the XMLHttpRequest object, so it's time to implement the business logic inside the doWork() function.&lt;br /&gt;&lt;br /&gt;First of all we need to get a valid XMLHttpRequest object. If we have it then we can send the value of the inputText field to the server script. We do this by composing an URL with parameter, so in the PHP script we can use the $_GET super-global array to catch the data. As next step we call the send() function of the XMLHttpRequest object which will send our request to the server. At the moment our doWork() function looks like this:&lt;br /&gt;&lt;br /&gt;Code:&lt;br /&gt;&lt;br /&gt;   1.&lt;br /&gt;      // Implement business logic&lt;br /&gt;   2.&lt;br /&gt;      function doWork(){&lt;br /&gt;   3.&lt;br /&gt;      httpObject = getHTTPObject();&lt;br /&gt;   4.&lt;br /&gt;      if (httpObject != null) {&lt;br /&gt;   5.&lt;br /&gt;      httpObject.open("GET", "upperCase.php?inputText="&lt;br /&gt;   6.&lt;br /&gt;      +document.getElementById('inputText').value, true);&lt;br /&gt;   7.&lt;br /&gt;      httpObject.send(null);&lt;br /&gt;   8.&lt;br /&gt;&lt;br /&gt;   9.&lt;br /&gt;      }&lt;br /&gt;  10.&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;Javascript F1&lt;br /&gt;&lt;br /&gt;It's nice but how we can catch the response from the server? To do this we need to use an other special property of the XMLHttpRequest object. We can assign a function to this parameter and this function will be called if the state of the object was changed. The final code is the following:&lt;br /&gt;&lt;br /&gt;Code:&lt;br /&gt;&lt;br /&gt;   1.&lt;br /&gt;      // Implement business logic&lt;br /&gt;   2.&lt;br /&gt;      function doWork(){&lt;br /&gt;   3.&lt;br /&gt;      httpObject = getHTTPObject();&lt;br /&gt;   4.&lt;br /&gt;      if (httpObject != null) {&lt;br /&gt;   5.&lt;br /&gt;      httpObject.open("GET", "upperCase.php?inputText="&lt;br /&gt;   6.&lt;br /&gt;      +document.getElementById('inputText').value, true);&lt;br /&gt;   7.&lt;br /&gt;      httpObject.send(null);&lt;br /&gt;   8.&lt;br /&gt;      httpObject.onreadystatechange = setOutput;&lt;br /&gt;   9.&lt;br /&gt;      }&lt;br /&gt;  10.&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;Javascript F1&lt;br /&gt;&lt;br /&gt;The last step on client side is to implement the setOutput() function which will change the value of our second field. The only interesting thing in this function that we need to check the actual state of the XMLHttpRequest object. We need to change the field value only if the state is complete. The readyState property can have the following values:&lt;br /&gt;&lt;br /&gt;    * 0 = uninitialized&lt;br /&gt;    * 1 = loading&lt;br /&gt;    * 2 = loaded&lt;br /&gt;    * 3 = interactive&lt;br /&gt;    * 4 = complete&lt;br /&gt;&lt;br /&gt;So the setOutput() looks like this:&lt;br /&gt;&lt;br /&gt;Code:&lt;br /&gt;&lt;br /&gt;   1.&lt;br /&gt;      // Change the value of the outputText field&lt;br /&gt;   2.&lt;br /&gt;      function setOutput(){&lt;br /&gt;   3.&lt;br /&gt;      if(httpObject.readyState == 4){&lt;br /&gt;   4.&lt;br /&gt;      document.getElementById('outputText').value&lt;br /&gt;   5.&lt;br /&gt;      = httpObject.responseText;&lt;br /&gt;   6.&lt;br /&gt;      }&lt;br /&gt;   7.&lt;br /&gt;       &lt;br /&gt;   8.&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;Javascript F1&lt;br /&gt;&lt;br /&gt;Now the client side is ready let's implement the server side.&lt;br /&gt;&lt;br /&gt;Step 3 - PHP code and the complete AJAX example&lt;br /&gt;&lt;br /&gt;Ajax PHP tutorial&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Implementing the server side functionality is very simple compared to the client side. In the PHP code we just need to check the $_GET super-global array. Afterwards convert it to uppercase and echo the result. So the PHP code is this:&lt;br /&gt;&lt;br /&gt;Code:&lt;br /&gt;&lt;br /&gt;   1.&lt;br /&gt;      &lt;?php&lt;br /&gt;   2.&lt;br /&gt;      if (isset($_GET['inputText']))&lt;br /&gt;   3.&lt;br /&gt;      echo strtoupper($_GET['inputText']);&lt;br /&gt;   4.&lt;br /&gt;      ?&gt;&lt;br /&gt;&lt;br /&gt;Javascript F1&lt;br /&gt;&lt;br /&gt;That's really short, isn't it?&lt;br /&gt;&lt;br /&gt;At least you can find the complete client and server code below.&lt;br /&gt;&lt;br /&gt;Client:&lt;br /&gt;&lt;br /&gt;Code:&lt;br /&gt;&lt;br /&gt;   1.&lt;br /&gt;      &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"&lt;br /&gt;   2.&lt;br /&gt;      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;&lt;br /&gt;   3.&lt;br /&gt;      &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;br /&gt;   4.&lt;br /&gt;      &lt;head&gt;&lt;br /&gt;   5.&lt;br /&gt;      &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;&lt;br /&gt;   6.&lt;br /&gt;      &lt;title&gt;Ajax - PHP example&lt;/title&gt;&lt;br /&gt;   7.&lt;br /&gt;      &lt;/head&gt;&lt;br /&gt;   8.&lt;br /&gt;       &lt;br /&gt;   9.&lt;br /&gt;      &lt;body&gt;&lt;br /&gt;  10.&lt;br /&gt;       &lt;br /&gt;  11.&lt;br /&gt;      &lt;script language="javascript" type="text/javascript"&gt;&lt;br /&gt;  12.&lt;br /&gt;      &lt;!--&lt;br /&gt;  13.&lt;br /&gt;      // Get the HTTP Object&lt;br /&gt;  14.&lt;br /&gt;      function getHTTPObject(){&lt;br /&gt;  15.&lt;br /&gt;      if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");&lt;br /&gt;  16.&lt;br /&gt;      else if (window.XMLHttpRequest) return new XMLHttpRequest();&lt;br /&gt;  17.&lt;br /&gt;      else {&lt;br /&gt;  18.&lt;br /&gt;      alert("Your browser does not support AJAX.");&lt;br /&gt;  19.&lt;br /&gt;      return null;&lt;br /&gt;  20.&lt;br /&gt;      }&lt;br /&gt;  21.&lt;br /&gt;      }&lt;br /&gt;  22.&lt;br /&gt;       &lt;br /&gt;  23.&lt;br /&gt;      // Change the value of the outputText field&lt;br /&gt;  24.&lt;br /&gt;      function setOutput(){&lt;br /&gt;  25.&lt;br /&gt;      if(httpObject.readyState == 4){&lt;br /&gt;  26.&lt;br /&gt;      document.getElementById('outputText').value = httpObject.responseText;&lt;br /&gt;  27.&lt;br /&gt;      }&lt;br /&gt;  28.&lt;br /&gt;       &lt;br /&gt;  29.&lt;br /&gt;      }&lt;br /&gt;  30.&lt;br /&gt;       &lt;br /&gt;  31.&lt;br /&gt;      // Implement business logic&lt;br /&gt;  32.&lt;br /&gt;      function doWork(){&lt;br /&gt;  33.&lt;br /&gt;      httpObject = getHTTPObject();&lt;br /&gt;  34.&lt;br /&gt;      if (httpObject != null) {&lt;br /&gt;  35.&lt;br /&gt;      httpObject.open("GET", "upperCase.php?inputText="&lt;br /&gt;  36.&lt;br /&gt;      +document.getElementById('inputText').value, true);&lt;br /&gt;  37.&lt;br /&gt;      httpObject.send(null);&lt;br /&gt;  38.&lt;br /&gt;      httpObject.onreadystatechange = setOutput;&lt;br /&gt;  39.&lt;br /&gt;      }&lt;br /&gt;  40.&lt;br /&gt;      }&lt;br /&gt;  41.&lt;br /&gt;       &lt;br /&gt;  42.&lt;br /&gt;      var httpObject = null;&lt;br /&gt;  43.&lt;br /&gt;       &lt;br /&gt;  44.&lt;br /&gt;      //--&gt;&lt;br /&gt;  45.&lt;br /&gt;      &lt;/script&gt;&lt;br /&gt;  46.&lt;br /&gt;       &lt;br /&gt;  47.&lt;br /&gt;      &lt;form name="testForm"&gt;&lt;br /&gt;  48.&lt;br /&gt;      Input text: &lt;input type="text" onkeyup="doWork();" name="inputText" id="inputText" /&gt;&lt;br /&gt;  49.&lt;br /&gt;      Output text: &lt;input type="text" name="outputText" id="outputText" /&gt;&lt;br /&gt;  50.&lt;br /&gt;      &lt;/form&gt;&lt;br /&gt;  51.&lt;br /&gt;      &lt;/body&gt;&lt;br /&gt;  52.&lt;br /&gt;      &lt;/html&gt;&lt;br /&gt;&lt;br /&gt;Javascript F1&lt;br /&gt;&lt;br /&gt;Server:&lt;br /&gt;&lt;br /&gt;Code:&lt;br /&gt;&lt;br /&gt;   1.&lt;br /&gt;      &lt;?php&lt;br /&gt;   2.&lt;br /&gt;      if (isset($_GET['inputText']))&lt;br /&gt;   3.&lt;br /&gt;      echo strtoupper($_GET['inputText']);&lt;br /&gt;   4.&lt;br /&gt;      ?&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;http://w3cworld.blogspot.com/feeds/posts/

http://w3cworld.blogspot.com/feeds/comment/&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2359453265019837844-2606528981533965151?l=w3cworld.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://w3cworld.blogspot.com/feeds/2606528981533965151/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://w3cworld.blogspot.com/2009/06/simple-ajax-examples.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2359453265019837844/posts/default/2606528981533965151'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2359453265019837844/posts/default/2606528981533965151'/><link rel='alternate' type='text/html' href='http://w3cworld.blogspot.com/2009/06/simple-ajax-examples.html' title='SIMPLE AJAX EXAMPLES'/><author><name>Imran Malik</name><uri>http://www.blogger.com/profile/13742779381556383378</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='http://3.bp.blogspot.com/_bYJnR7vm4kE/TJtq9K9FMdI/AAAAAAAABmk/EtUpHKcAerw/S220/ASDF+(128).jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2359453265019837844.post-6368028597746802334</id><published>2009-06-01T22:45:00.000-07:00</published><updated>2009-06-01T22:54:50.494-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='AJAX'/><title type='text'>AJAX EXAMPLES</title><content type='html'>Example 1: Ajax with innerHTML&lt;br /&gt;&lt;br /&gt;For a simple innerHTML-based Ajax example, we’ll create a quasi-functional address book application. We’ll start with the XHTML page (line wraps marked » —Ed.):&lt;br /&gt;&lt;br /&gt;&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" »&lt;br /&gt;  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;&lt;br /&gt;&lt;html xmlns="http://www.w3.org/1999/xhtml" »&lt;br /&gt;  xml:lang="en" lang="en"&gt;&lt;br /&gt;&lt;head&gt;&lt;br /&gt;  &lt;title&gt;Ajax Address Book&lt;/title&gt;&lt;br /&gt;  &lt;meta http-equiv="content-type" content="text/html; »&lt;br /&gt;    charset=iso-8859-1" /&gt;&lt;br /&gt;&lt;br /&gt;  &lt;meta http-equiv="Content-Language" content="en-us" /&gt;&lt;br /&gt;  &lt;script type="text/javascript" src="XHConn.js"&gt;&lt;/script&gt;&lt;br /&gt;  &lt;script type="text/javascript" src="addressBook.js"&gt;&lt;/script&gt;&lt;br /&gt;&lt;/head&gt;&lt;br /&gt;&lt;body&gt;&lt;br /&gt;  &lt;h1&gt;Simple Ajax Address Book&lt;/h1&gt;&lt;br /&gt;&lt;br /&gt;  &lt;form action="getAddress.php" method="POST"&gt;&lt;br /&gt;    &lt;fieldset&gt;&lt;br /&gt;      &lt;legend&gt;Please Choose a Person&lt;/legend&gt;&lt;br /&gt;      &lt;select id="person" name="person"&gt;&lt;br /&gt;        &lt;option value=""&gt;Choose Someone&lt;/option&gt;&lt;br /&gt;&lt;br /&gt;        &lt;option value="1"&gt;Bob Smith&lt;/option&gt;&lt;br /&gt;        &lt;option value="2"&gt;Janet Jones&lt;/option&gt;&lt;br /&gt;      &lt;/select&gt;&lt;br /&gt;      &lt;input type="submit" id="submit" name="submit" »&lt;br /&gt;        value="Get the Address" /&gt;&lt;br /&gt;    &lt;/fieldset&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/form&gt;&lt;br /&gt;  &lt;pre id="address"&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/body&gt;&lt;br /&gt;&lt;/html&gt;&lt;br /&gt;&lt;br /&gt;As you can see, we have a simple form with a select, from which to choose a person. Again, we are providing a fallback action for the form, in case our JavaScript cannot run. Below the form, we have a simple pre element that will be displaying the address information from the database.&lt;br /&gt;&lt;br /&gt;And now for the JavaScript. Basically, we will be commandeering the select and using its onchange event handler to trigger an XMLHttpRequest() call to obtain the address information for the selected individual. The server will be returning this information as a string like this:&lt;br /&gt;&lt;br /&gt;Bob Smith&lt;br /&gt;123 School Street&lt;br /&gt;Anytown, NY 12345&lt;br /&gt;&lt;br /&gt;We will take this return as a string and dump it into the pre element using innerHTML. Take a look at the code (line wraps marked » —Ed.):&lt;br /&gt;&lt;br /&gt;var addressBook = {&lt;br /&gt;  myConn:      false, // the XMLHttpRequest&lt;br /&gt;  body:        false, // the body element&lt;br /&gt;  target:      false, // the target container&lt;br /&gt;  loader:      false, // the loader&lt;br /&gt;  init:        function(controlId, sbmtBtnId, targetId){&lt;br /&gt;    /* init() takes three arguments:&lt;br /&gt;       * the id of the controller (select)&lt;br /&gt;       * the id of the submit button&lt;br /&gt;       * the id of the target container */&lt;br /&gt;    // test for methods &amp; elements&lt;br /&gt;    if(!document.getElementById ||&lt;br /&gt;       !document.getElementsByTagName ||&lt;br /&gt;       !document.getElementById(controlId) ||&lt;br /&gt;       !document.getElementById(sbmtBtnId)  ||&lt;br /&gt;       !document.getElementById(targetId)) return;&lt;br /&gt;    // set and test XHConn, quitting silently if it fails&lt;br /&gt;    addressBook.myConn = new XHConn();&lt;br /&gt;    if(!addressBook.myConn) return;&lt;br /&gt;    // get the body&lt;br /&gt;    addressBook.body = document.getElementsByTagName('body')[0];&lt;br /&gt;    // get the controller&lt;br /&gt;    var control = document.getElementById(controlId);&lt;br /&gt;    // get the submit button&lt;br /&gt;    var sbmtBtn = document.getElementById(sbmtBtnId);&lt;br /&gt;    // remove the submit button&lt;br /&gt;    sbmtBtn.parentNode.removeChild(sbmtBtn);&lt;br /&gt;    // get the target&lt;br /&gt;    addressBook.target = document.getElementById(targetId);&lt;br /&gt;    // add the onchange event to the controller,&lt;br /&gt;    addressBook.addEvent(control,&lt;br /&gt;                         'change',&lt;br /&gt;                         function(){&lt;br /&gt;                           if(this.value != ''){&lt;br /&gt;                             /* if there's a value,&lt;br /&gt;                                trigger getAddress */&lt;br /&gt;                             addressBook.getAddress(this.value);&lt;br /&gt;                           } else { &lt;br /&gt;                             // otherwise empty the target&lt;br /&gt;                             addressBook.target.innerHTML = '';&lt;br /&gt;                           }&lt;br /&gt;                         });&lt;br /&gt;  },&lt;br /&gt;  getAddress:  function(id){ // the Ajax call&lt;br /&gt;    // let's let the user know something is happening (see below)&lt;br /&gt;    addressBook.buildLoader();&lt;br /&gt;    /* this is the function that is run&lt;br /&gt;       once the Ajax call completes */&lt;br /&gt;    var fnWhenDone = function(oXML) {&lt;br /&gt;      // get rid of the loader&lt;br /&gt;      addressBook.killLoader();&lt;br /&gt;      // insert the returned address information into the target&lt;br /&gt;      addressBook.target.innerHTML = oXML.responseText;&lt;br /&gt;    };&lt;br /&gt;    // use XHConn's connect method&lt;br /&gt;    addressBook.myConn.connect('index.php', 'POST',&lt;br /&gt;                               'id='+id, fnWhenDone);&lt;br /&gt;  },&lt;br /&gt;  buildLoader: function(){     // builds a loader&lt;br /&gt;    // create a new div&lt;br /&gt;    addressBook.loader = document.createElement('div');&lt;br /&gt;    // give it some style&lt;br /&gt;    addressBook.loader.style.position   = 'absolute';&lt;br /&gt;    addressBook.loader.style.top        = '50%';&lt;br /&gt;    addressBook.loader.style.left       = '50%';&lt;br /&gt;    addressBook.loader.style.width      = '300px';&lt;br /&gt;    addressBook.loader.style.lineHeight = '100px';&lt;br /&gt;    addressBook.loader.style.margin     = '-50px 0 0 - 150px';&lt;br /&gt;    addressBook.loader.style.textAlign  = 'center';&lt;br /&gt;    addressBook.loader.style.border     = '1px solid #870108';&lt;br /&gt;    addressBook.loader.style.background = '#fff';&lt;br /&gt;    // give it some text&lt;br /&gt;    addressBook.loader.appendChild( »&lt;br /&gt;      document.createTextNode( »&lt;br /&gt;        'Loading Data, please wait\u2026'));&lt;br /&gt;    // append it to the body&lt;br /&gt;    addressBook.body.appendChild(addressBook.loader);&lt;br /&gt;  },&lt;br /&gt;  killLoader:  function(){     // kills the loader&lt;br /&gt;    // remove the loader form the body&lt;br /&gt;    addressBook.body.removeChild(addressBook.loader);&lt;br /&gt;  },&lt;br /&gt;  addEvent: function(obj, type, fn){  // the add event function&lt;br /&gt;    if (obj.addEventListener) »&lt;br /&gt;      obj.addEventListener(type, fn, false);&lt;br /&gt;    else if (obj.attachEvent) {&lt;br /&gt;      obj["e"+type+fn] = fn;&lt;br /&gt;      obj[type+fn] = function() {&lt;br /&gt;        obj["e"+type+fn](window.event);&lt;br /&gt;      };&lt;br /&gt;      obj.attachEvent("on"+type, obj[type+fn]);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;};&lt;br /&gt;/* run the init() method on page load, passing it&lt;br /&gt;   the required arguments */&lt;br /&gt;addressBook.addEvent(window, 'load',&lt;br /&gt;                     function(){&lt;br /&gt;                       addressBook.init('person',&lt;br /&gt;                                        'submit',&lt;br /&gt;                                        'address');&lt;br /&gt;                     });&lt;br /&gt;&lt;br /&gt;See this script in action.&lt;br /&gt;Example 2: Ajax with Nodes&lt;br /&gt;&lt;br /&gt;Let’s alter the example, and instead of returning a string from the server, this time, make it XML:&lt;br /&gt;&lt;br /&gt;&lt;file&gt;&lt;br /&gt;  &lt;name&gt;&lt;br /&gt;    &lt;first&gt;Bob&lt;/first&gt;&lt;br /&gt;    &lt;last&gt;Smith&lt;/last&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/name&gt;&lt;br /&gt;  &lt;address&gt;&lt;br /&gt;    &lt;street&gt;123 School Street&lt;/street&gt;&lt;br /&gt;    &lt;city&gt;Anytown&lt;/city&gt;&lt;br /&gt;    &lt;state&gt;NY&lt;/state&gt;&lt;br /&gt;&lt;br /&gt;    &lt;zip&gt;12345&lt;/zip&gt;&lt;br /&gt;  &lt;/address&gt;&lt;br /&gt;&lt;/file&gt;&lt;br /&gt;&lt;br /&gt;The XHTML page remains the same, but we need to make some minor adjustments to the JavaScript. To highlight the differences, I will touch on each change individually.&lt;br /&gt;&lt;br /&gt;The first change, to the onchange event handler of the select, is pretty simple (line wraps marked » —Ed.):&lt;br /&gt;&lt;br /&gt;…&lt;br /&gt;    addressBook.addEvent(addressBook.control,&lt;br /&gt;                         'change',&lt;br /&gt;                         function(){&lt;br /&gt;                            if(this.value != ''){&lt;br /&gt;                              addressBook.getAddress(this.value);&lt;br /&gt;                           } else {&lt;br /&gt;                             addressBook.target.removeChild( »&lt;br /&gt;                               addressBook.target.firstChild);&lt;br /&gt;                           }&lt;br /&gt;                         });&lt;br /&gt;…&lt;br /&gt;&lt;br /&gt;Instead of setting the content of the target to empty using innerHTML, the DOM is removing the node that is the target’s first child.&lt;br /&gt;&lt;br /&gt;Next up is the getAddress() method (line wraps marked » —Ed.):&lt;br /&gt;&lt;br /&gt;…&lt;br /&gt;  getAddress:  function(id){&lt;br /&gt;    addressBook.buildLoader();&lt;br /&gt;    var fnWhenDone = function(oXML) {&lt;br /&gt;      addressBook.killLoader();&lt;br /&gt;      if(addressBook.target.hasChildNodes()){&lt;br /&gt;        addressBook.target.removeChild( »&lt;br /&gt;          addressBook.target.firstChild);&lt;br /&gt;      }&lt;br /&gt;      xml = oXML.responseXML;&lt;br /&gt;      var name    = addressBook.getNodeValue(xml, 'first')+' '+&lt;br /&gt;                    addressBook.getNodeValue(xml, 'last');&lt;br /&gt;      var address = addressBook.getNodeValue(xml, 'street');&lt;br /&gt;      var csz     = addressBook.getNodeValue(xml, 'city')+', '+&lt;br /&gt;                    addressBook.getNodeValue(xml, 'state')+' '+&lt;br /&gt;                    addressBook.getNodeValue(xml, 'zip');&lt;br /&gt;      var txt = document.createTextNode(name + "\ n" +&lt;br /&gt;                                        address + "\n" + csz);&lt;br /&gt;      addressBook.target.appendChild(txt);&lt;br /&gt;    };&lt;br /&gt;    addressBook.myConn.connect('getAddress.php', 'POST',&lt;br /&gt;                               'id=' + id, fnWhenDone);&lt;br /&gt;  },&lt;br /&gt;…&lt;br /&gt;&lt;br /&gt;As we are working with XML, we can use the responseXML property to get the return from the server as a node tree. Then we can traverse that tree, collecting the tidbits of information we need. In this example, we added a new method (getNodeValue()) that makes working with XML returns easier:&lt;br /&gt;&lt;br /&gt;…&lt;br /&gt;  getNodeValue: function(tree, el){&lt;br /&gt;    return tree.getElementsByTagName(el)[0].firstChild.nodeValue;&lt;br /&gt;  },&lt;br /&gt;…&lt;br /&gt;&lt;br /&gt;This method takes two arguments: the node tree (tree) and the element (el) whose content is wanted. It returns the nodeValue of the firstChild of the first el within tree or, in other words, the text value of the node requested from the node tree.&lt;br /&gt;&lt;br /&gt;Once we have collected all of the requested contents from the XML, the text string is rebuilt and generated with the DOM before being appended to the target. The end result can be seen here.&lt;div class="blogger-post-footer"&gt;http://w3cworld.blogspot.com/feeds/posts/

http://w3cworld.blogspot.com/feeds/comment/&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2359453265019837844-6368028597746802334?l=w3cworld.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://w3cworld.blogspot.com/feeds/6368028597746802334/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://w3cworld.blogspot.com/2009/06/ajax-examples.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2359453265019837844/posts/default/6368028597746802334'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2359453265019837844/posts/default/6368028597746802334'/><link rel='alternate' type='text/html' href='http://w3cworld.blogspot.com/2009/06/ajax-examples.html' title='AJAX EXAMPLES'/><author><name>Imran Malik</name><uri>http://www.blogger.com/profile/13742779381556383378</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='http://3.bp.blogspot.com/_bYJnR7vm4kE/TJtq9K9FMdI/AAAAAAAABmk/EtUpHKcAerw/S220/ASDF+(128).jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2359453265019837844.post-8870924309888200428</id><published>2009-06-01T09:08:00.000-07:00</published><updated>2009-06-01T09:22:13.261-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='MYSQL AND PHP'/><title type='text'>MYSQL AND PHP</title><content type='html'>&lt;h3&gt;&lt;a class="anchor" name="3"&gt;Connecting to MySQL.&lt;/a&gt;&lt;/h3&gt; &lt;p&gt; Before we can do anything with MySQL we need to be able to connect to the server. Here we will demonstrate 3 ways of doing this with PHP mysql, mysqli and PDO. First with the standard mysql connection. &lt;/p&gt;  &lt;h4&gt;&lt;a class="anchor" name="3.1"&gt;Connecting with mysql.&lt;/a&gt;&lt;/h4&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** connect to the database ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_connect&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check if the link is a valid resource ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;is_resource&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected successfully'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close the connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we fail to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt;Before we move on with the other extensions, lets look briefly at what has happened above. We have begun by setting some variables for our database, these are the hostname, username, and password. The next line of code shows the use of mysql_connect() function to connect to the MySQL server. Lets look at this line further. &lt;/p&gt; &lt;div class="codebox"&gt;$link = @mysql_connect($hostname, $username, $password);&lt;/div&gt; &lt;p&gt;We have created a mysql link resource variable called $link. We see also here the use of the @ symbol to suppress and errors that may arise from a failure to connect, more on this later..., After we use mysql_connect() we check with the is_resource() function that the link variable is indeed a valid resource, and if so, we can continue with our code, else, an error is printed saying Unable to connect. If the connection is valid, then a message is printed to say so, and the database link is closed with the mysql_close() function. The database connection should cease at the end of the script, but with longer running scripts this may be a waste of valuable resources, so we free up some room with mysql_close(). Now, lets see the same thing with mysqli.. &lt;/p&gt; &lt;h4&gt;&lt;a class="anchor" name="3.2"&gt;Connecting with mysqli.&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;This could simply be done in the same manner as mysql_connect(), but mysqli offers us an Object Oriented (OO) interface to MySQL so it would be futile to do things "the old way". Lets see how we go. &lt;/p&gt;&lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** create a new mysqli object ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/* check connection */&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_connect_errno&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected Successfully'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    exit();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt;  &lt;p&gt;Here we see the creation of a new mysqli object which passes the hostname, username, and password to the constructor which makes the connection for us. We can then use this new object for other functionality that we will soon use. We have also suppressed error_reporting with the @ symbol once more, this is because a warning will be produced if the connection fails. We have also tested for an error condition (more on errors later) to see if the connection is valid, and if so, it tells us. Now, lets move onto PDO. &lt;/p&gt; &lt;h4&gt;&lt;a class="anchor" name="3.3"&gt;Connecting with PDO&lt;/a&gt;&lt;/h4&gt; &lt;p&gt; PHP has spawned an amazingly neat idea to standardize database connections. This is PDO (Php Data Objects). A full &lt;a href="http://phpro.org/tutorials/Introduction-to-PHP-PDO-%28PHP-Data-Objects%29.html"&gt;Introduction to PDO&lt;/a&gt; is available and is well worth reading. Here we will use some of the concepts from this article to use with mysql. So, lets get connected &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'root'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'rootpass'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"mysql:host=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;;dbname=mysql"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message saying we have connected ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected to database'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;catch(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDOException $e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;    echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;getMessage&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt;In the above code, several variations have taken place. Like the other connections we have supplied a hostname, username, and password. PDO demands that we also have a valid database to connect to. If no database is specified an exception is thrown. In the example above we have used the default mysql database to connect to. The database named mysql contains the tables of users, databases and other vital information. YOU HAVE BEEN WARNED! We use the catch{ } block to catch any thrown exceptions from the try{ } block. Another variation is that we have used root as the username.&lt;br /&gt; Is this a bad idea?&lt;br /&gt;In most circumstances yes, however, here we wish to demonstrate some administrative tasks such as creating users and creating databases. Most MySQL servers, by default, will not allow this to be done by a user other than the root super user. In the following section we will be creating a database, so root access is required. &lt;/p&gt;  &lt;h3&gt;&lt;a class="anchor" name="4"&gt;Creating a Database.&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;Now we have seen how to connect to a database server, it is just a small step to create a database. You will need to be a user with CREATE privileges to do this. Usually this will be the user root, so thats what we will be using here. Once again we will use the three different PHP mysql extenstions to do this, beginning with the mysql extenstion. &lt;/p&gt;&lt;h4&gt;&lt;a class="anchor" name="4.1"&gt;Create with mysql.&lt;/a&gt;&lt;/h4&gt; &lt;div class="codebox"&gt;  &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'root'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'rootpassword'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** connect to the database ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_connect&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check if the link is a valid resource ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;is_resource&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to create a database ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'CREATE DATABASE periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** run the sql query ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Database created successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;    else&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to create database: &lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_error&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;        }&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close the connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we fail to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt;  &lt;p&gt; In the above code the we have used the function mysql_query() to run the SQL statement&lt;br /&gt;CREATE DATABASE periodic_table&lt;br /&gt;You may ask why we created the variable &lt;span class="codechar"&gt;$sql&lt;/span&gt; when it would have been just as easy, and used less code to embed the sql query as an arguement for the mysql_query() function like this: &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;mysql_query("CREATE DATABASE periodic_table")&lt;br /&gt;&lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;br /&gt; &lt;p&gt; We created the variable &lt;span class="codechar"&gt;$sql&lt;/span&gt; for possible debugging purposes. Should we encounter an error within our sql statement, the error checking will print a message, including the sql statement. Eg: should we run this script twice, we would see an error something like this.. &lt;/p&gt; &lt;p&gt; Connected successfully&lt;br /&gt;Unable to create database:&lt;br /&gt;CREATE DATABASE periodic_table&lt;br /&gt;Cannot create database periodic_table; database exists &lt;/p&gt;  &lt;h4&gt;&lt;a class="anchor" name="4.2"&gt;Create with mysqli.&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;The mysqli connection provides a smooth object oriented approach for same functionality. Using the connection code from the earlier connect script we can simply use the created mysqli object to run queries. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'root'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'rootpassword'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** create a new mysqli object ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/* check connection */&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_connect_errno&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected Successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** our sql statement ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'CREATE DATABASE periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) === &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;TRUE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Database created successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;    else&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;error&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    exit();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt;  &lt;p&gt; Should we run this code twice, we would again get an get an error like the following &lt;/p&gt; &lt;p&gt; Connected Successfully&lt;br /&gt;CREATE DATABASE periodic_table&lt;br /&gt;Cannotcreate database periodic_table; database exists&lt;br /&gt;&lt;/p&gt; &lt;p&gt;You can see from the message, as in the previous example, we have a message to tell us that we have connected successfully, then the SQL query used followed by a message from the database itself telling us the exact nature of the error. &lt;/p&gt;  &lt;h4&gt;&lt;a class="anchor" name="4.3"&gt;Create with PDO&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;Not to be out-done in the Object Oriented approach, PDO makes the code base somewhat cleaner. Using the code from the initial connection we can use the provided DSN object for our query. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'root'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'rootpassword'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"mysql:host=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;;dbname=mysql"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message saying we have connected ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected to database&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set the PDO error mode to exception ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;setAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ATTR_ERRMODE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ERRMODE_EXCEPTION&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;   &lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** our sql statement ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'CREATE DATABASE periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** we use PDO::exec because no results are returned ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message to say the database was created ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Database created successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;catch(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDOException $e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo the sql statement and error message ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;getMessage&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt;As you see, PDO provides a nice exception class to handle any problems that may arise in our database queries. If an exception is thrown within the try{ } block, no further code within the try{ } block is executed and the flows directly to the first catch(){ } block. In the catch block above we echo the SQL statement and any error message that may be generated by the database. &lt;/p&gt;  &lt;p&gt; This query is not one you will be running often but is needed in order to show how to create database tables. Below is the SQL statement needed to create the a table and fields within your newly created database. &lt;/p&gt;  &lt;h3&gt;&lt;a class="anchor" name="5"&gt;Creating a MySQL user&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;Now a database has been created, a user other than the root user needs to be able to work with it. This means a MySQL user, not a system user, needs to be created and given the appropriate permissions to be able to work with data. We will create a user with all permissions on the periodic_table database. If you want real security, you can limit the permissions a user has to just SELECT, thus limitting possible injection attacks. The topic of PHP/MySQL security will be covered in another article. For now, we will GRANT ALL privileges to the new user, except GRANT. Once again we need to have root access to mysql to do this. The code itself should look very familiar if you have read the &lt;a href="http://www.phpro.org/tutorials/4"&gt;previous section&lt;/a&gt; as it is almost identical. The same principles and connections apply, the only thing that changes is the SQL statement itself. &lt;/p&gt; &lt;h4&gt;&lt;a class="anchor" name="5.1"&gt;Create a user with mysql.&lt;/a&gt;&lt;/h4&gt; &lt;p&gt; As mentioned, this is identical to creating a database, the only change is the SQL statement, we have reproduced the code here for the benifit of familiarity. &lt;/p&gt; &lt;div class="codebox"&gt;  &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'root'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'rootpassword'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** connect to the database ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_connect&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check if the link is a valid resource ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;is_resource&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to create a user ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"GRANT ALL ON periodic_table.* TO username@localhost IDENTIFIED BY 'password'"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** run the sql query ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'New user created successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;    else&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to create user: &lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_error&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;        }&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close the connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we fail to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt;    &lt;h4&gt;&lt;a class="anchor" name="5.2"&gt;Create a user with mysqli.&lt;/a&gt;&lt;/h4&gt; &lt;p&gt; Here also there are no changes except for the SQL. All the code remains as it was to create a database. &lt;/p&gt; &lt;div class="codebox"&gt;  &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'root'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'rootpassword'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** create a new mysqli object ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/* check connection */&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_connect_errno&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected Successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to create a user ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"GRANT ALL ON periodic_table.* TO username@localhost IDENTIFIED BY 'password'"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) === &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;TRUE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'New user created successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;    else&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;error&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    exit();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt;  &lt;h4&gt;&lt;a class="anchor" name="5.3"&gt;Create a user with PDO&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;And you guessed it, no changes here either from the CREATE DATABASE example above. Everything remains the same except for the SQL statement itself, the new user is created, and if there is a problem an exception is thrown. &lt;/p&gt; &lt;div class="codebox"&gt;  &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'root'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'rootpassword'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"mysql:host=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;;dbname=mysql"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message saying we have connected ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected to database&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set the PDO error mode to exception ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;setAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ATTR_ERRMODE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ERRMODE_EXCEPTION&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;   &lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to create a user ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"GRANT ALL ON periodic_table.* TO username@localhost IDENTIFIED BY 'password'"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** we use PDO::exec because no results are returned ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message to say the database was created ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Database created successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;catch(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDOException $e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo the sql statement and error message ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;getMessage&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt;  &lt;h3&gt;&lt;a class="anchor" name="6"&gt;Creating a Table.&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;Now that a database has been created, and a database user other than root exists, we are able to connect to the database with our shiny new username and password. A database it really needs some data in it for it to be functional. For the purposes of this tutorial we are using the periodic table of elements, (we can all recite those from school, right?) so in our periodic_table database an appropriate table name would be elements. Lets first look at the SQL need to create such a table. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;CREATE TABLE elements (&lt;br /&gt;  atomicnumber tinyint(3) NOT NULL default '0',&lt;br /&gt;  latin varchar(20) NOT NULL default '',&lt;br /&gt;  english varchar(20) NOT NULL default '',&lt;br /&gt;  abbr char(3) NOT NULL default '',&lt;br /&gt;  PRIMARY KEY  (atomicnumber)&lt;br /&gt;  )&lt;/span&gt; &lt;/code&gt; &lt;/div&gt; &lt;p&gt;Once again we can use code similar to that which we have already used to create a database and to create a new MySQL user. The same process is repeated here with the difference that we no longer need root access to be able to interface with our new database. The user "username" can be used and this is recommended practice. Never use root if you do not need it. Our new user has all the privileges needed to carry out any interactions with the database in this tutorial. With that in mind, lets get to table creation, first with the mysql extension functions. &lt;/p&gt; &lt;h4&gt;&lt;a class="anchor" name="6.1"&gt;Create table with mysql.&lt;/a&gt;&lt;/h4&gt; &lt;div class="codebox"&gt;  &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** connect to the database ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_connect&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check if the link is a valid resource ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;is_resource&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** select the database we wish to use ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_select_db&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"periodic_table"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) === &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;TRUE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;        {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to create a new table ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"CREATE TABLE elements (&lt;br /&gt;        atomicnumber tinyint(3) NOT NULL default '0',&lt;br /&gt;        latin varchar(20) NOT NULL default '',&lt;br /&gt;        english varchar(20) NOT NULL default '',&lt;br /&gt;        abbr char(3) NOT NULL default '',&lt;br /&gt;        PRIMARY KEY  (atomicnumber)&lt;br /&gt;        )"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** run the sql query ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;            {&lt;br /&gt;            echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'New table created successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;            }&lt;br /&gt;        else&lt;br /&gt;            {&lt;br /&gt;            echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to create table: &lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_error&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to select the database show an error ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;else&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to select database'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close the connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we fail to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt; Lets go over what we have done here. We have used the CREATE TABLE statement to create a table called elements&lt;br /&gt;Following that we have 4 fields that will contain the data itself. They are:&lt;br /&gt;atomicnumber&lt;br /&gt;latin&lt;br /&gt;english&lt;br /&gt;abbr&lt;br /&gt;These fields will contain different data types and so have different attributes. The atomicnumber field has a type of tinyint or tiny interger and its maximum length is 3 with a default value of zero. More on types can be found in the MySQL documentation and is recommended reading. &lt;/p&gt;    &lt;h4&gt;&lt;a class="anchor" name="6.2"&gt;Creating a Table with mysqli.&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;As with the script above, we no longer need to use the root username and password for creating our table. An extra parameter is added to our class instantiation that contains the name of the default database to use for our script. We assign this to a variable at the top of our script with the other variables. &lt;/p&gt; &lt;div class="codebox"&gt;  &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql database name ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** create a new mysqli object with default database***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/* check connection */&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_connect_errno&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected Successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to create a new table ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"CREATE TABLE elements (&lt;br /&gt;    atomicnumber tinyint(3) NOT NULL default '0',&lt;br /&gt;    latin varchar(20) NOT NULL default '',&lt;br /&gt;    english varchar(20) NOT NULL default '',&lt;br /&gt;    abbr char(3) NOT NULL default '',&lt;br /&gt;    PRIMARY KEY  (atomicnumber)&lt;br /&gt;    )"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) === &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;TRUE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'New table created successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;    else&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;error&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    exit();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;  &lt;/div&gt; &lt;h4&gt;&lt;a class="anchor" name="6.3"&gt;Creating a Table with PDO&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;The use of PDO will significantly cut down our code. Much of the savings comes from the use of exceptions rather than constantly checking for error conditions. As with the previous table creation examples we are now using a non-root username/password pair. We have also created a new variable called &lt;span class="codechar"&gt;$dbname&lt;/span&gt;. This variable holds the name of the database we wish to connect to and is used when we instantiate a new PDO object like this: &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;  $dbh &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"mysql:host=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;;dbname=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt; With this in mind, our table creation script using PDO is as follows. &lt;/p&gt; &lt;div class="codebox"&gt;  &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** database name ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"mysql:host=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;;dbname=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message saying we have connected ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected to database&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set the PDO error mode to exception ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;setAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ATTR_ERRMODE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ERRMODE_EXCEPTION&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;   &lt;br /&gt;       &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"CREATE TABLE elements (&lt;br /&gt;        atomicnumber tinyint(3) NOT NULL default '0',&lt;br /&gt;        latin varchar(20) NOT NULL default '',&lt;br /&gt;        english varchar(20) NOT NULL default '',&lt;br /&gt;        abbr char(3) NOT NULL default '',&lt;br /&gt;        PRIMARY KEY  (atomicnumber)&lt;br /&gt;        )"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** we use PDO::exec because no results are returned ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message to say the database was created ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Table created successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;catch(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDOException $e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo the sql statement and error message ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;getMessage&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;  &lt;/div&gt;  &lt;p&gt; You should now have a database called "periodic" with a table named periodic within it. This table is has 4 fields and it is these that will contain the real data we wish to use. First we must INSERT some data into the tables. &lt;/p&gt;  &lt;h3&gt;&lt;a class="anchor" name="7"&gt;INSERT data into MySQL&lt;/a&gt;&lt;/h3&gt; &lt;p&gt; This will likely be the second most frequent task done with the database. All the data in the database needs to come from somewhere and it is done using the INSERT statement. The process is similar to what we have been doing with other SQL statements and should be familiar by now, so lets get to it... &lt;/p&gt; &lt;h4&gt;&lt;a class="anchor" name="7.1"&gt;INSERT data with mysql.&lt;/a&gt;&lt;/h4&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** connect to the database ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_connect&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check if the link is a valid resource ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;is_resource&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** select the database we wish to use ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_select_db&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"periodic_table"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) === &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;TRUE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;        {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to INSERT a new record ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO elements (atomicnumber, latin, english, abbr)&lt;br /&gt;        VALUES ( 1, 'HYDROGENIUM', 'Hydrogen', 'H')"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** run the sql query ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;            {&lt;br /&gt;            echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'New record created successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;            }&lt;br /&gt;        else&lt;br /&gt;            {&lt;br /&gt;            echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to INSERT data: &lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_error&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to select the database show an error ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;else&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to select database'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close the connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we fail to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt;Now you should be getting in the swing of sending SQL to the database. It goes much the same for mysqli and PDO. We connect to the database server, then select the appropriate database, and issue commands. Lets continue with our mysqli example. &lt;/p&gt; &lt;h4&gt;&lt;a class="anchor" name="7.2"&gt;INSERT data with mysqli&lt;/a&gt;&lt;/h4&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql database name ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** create a new mysqli object with default database***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/* check connection */&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_connect_errno&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected Successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to INSERT a new record ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO elements (atomicnumber, latin, english, abbr)&lt;br /&gt;    VALUES ( 1, 'HYDROGENIUM', 'Hydrogen', 'H')"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) === &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;TRUE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'New record created successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;    else&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;error&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    exit();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt;Once again we have used the same code with just a different SQL statement. The process remains the same for commands that do not return a value or values. &lt;/p&gt;  &lt;h4&gt;&lt;a class="anchor" name="7.3"&gt;INSERT data with PDO&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;Not to be out classed, PDO maintains a compact code base for perfoming the same task. Here we see the creation of the same record as above. Again we use the same script as creating the database and table. The only thing that changes is the SQL statement. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** database name ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"mysql:host=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;;dbname=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message saying we have connected ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected to database&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set the PDO error mode to exception ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;setAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ATTR_ERRMODE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ERRMODE_EXCEPTION&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;   &lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to INSERT a new record ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO elements (atomicnumber, latin, english, abbr)&lt;br /&gt;        VALUES ( 1, 'HYDROGENIUM', 'Hydrogen', 'H')"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** we use PDO::exec because no results are returned ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message to say the database was created ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Recored created successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;catch(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDOException $e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo the sql statement and error message ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;getMessage&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt;  &lt;h3&gt;&lt;a class="anchor" name="8"&gt;INSERT multiple records&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;In the previous section we can see how to INSERT a record into a database. This works fine for single records, but what if we have over one hundred records. The periodic table of elements holds one hundred and nine known elements. We need a method to INSERT all these records without needing one hundred and nine database queries. This would be a much to heavy drain on resources and time conusumin and time conusuming. The solution comes in several forms, depending on the extension you are using. We will step through each extension and see how the various INSERT methods compares. Lets get started with the mysql extention. MySQL provides a simple query method of handling multiple INSERTs. &lt;/p&gt; &lt;h4&gt;&lt;a class="anchor" name="8.1"&gt;INSERT multiple records with mysql&lt;/a&gt;&lt;/h4&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** connect to the database ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_connect&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check if the link is a valid resource ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;is_resource&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** select the database we wish to use ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_select_db&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"periodic_table"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) === &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;TRUE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;        {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to INSERT multiple new records ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO elements (atomicnumber, latin, english, abbr)&lt;br /&gt;        VALUES&lt;br /&gt;    (1, 'HYDROGENIUM', 'Hydrogen', 'H'),&lt;br /&gt;    (2, 'HELIUM', 'Helium', 'He'),&lt;br /&gt;    (3, 'LITHIUM', 'Lithium', 'Li'),&lt;br /&gt;    (4, 'Beryllium', 'Beryllium', 'Be'),&lt;br /&gt;    (5, 'Borum', 'Boron', 'B'),&lt;br /&gt;    (6, 'Carboneum', 'Carbon', 'C'),&lt;br /&gt;    (7, 'Nitrogenium', 'Nitrogen', 'N'),&lt;br /&gt;    (8, 'Oxygenium', 'Oxygen', 'O'),&lt;br /&gt;    (9, 'Fluorum', 'Fluorine', 'F'),&lt;br /&gt;    (10, 'Neon', 'Neon', 'Ne'),&lt;br /&gt;    (11, 'Natrium', 'Sodium', 'Na'),&lt;br /&gt;    (12, 'Magnesium', 'Magnesium', 'Mg'),&lt;br /&gt;    (13, 'aluminium', 'Aluminum', 'Al'),&lt;br /&gt;    (14, 'Silicium', 'Silicon', 'Si'),&lt;br /&gt;    (15, 'Phosphorus', 'Phosphorus', 'P'),&lt;br /&gt;    (16, 'Sulphur', 'Sulphur', 'S'),&lt;br /&gt;    (17, 'Chlorum', 'Chlorine', 'Cl'),&lt;br /&gt;    (18, 'Argon', 'argon', 'Ar'),&lt;br /&gt;    (19, 'Kalium', 'Potassium', 'K'),&lt;br /&gt;    (20, 'Calcium', 'Calcium', 'Ca'),&lt;br /&gt;    (21, 'Scandium', 'Scandium', 'Sc'),&lt;br /&gt;    (22, 'Titanium', 'Titanium', 'Ti'),&lt;br /&gt;    (23, 'Vanadium', 'Vanadium', 'V'),&lt;br /&gt;    (24, 'Chromium', 'Chromium', 'Cr'),&lt;br /&gt;    (25, 'Manganum', 'Manganese', 'Mn'),&lt;br /&gt;    (26, 'Ferrum', 'Iron', 'Fe'),&lt;br /&gt;    (27, 'Cobaltum', 'Cobalt', 'Co'),&lt;br /&gt;    (28, 'Niccolum', 'Nickel', 'Ni'),&lt;br /&gt;    (29, 'Cuprum', 'Copper', 'Cu'),&lt;br /&gt;    (30, 'Zincum', 'Zinc', 'Zn'),&lt;br /&gt;    (31, 'Gallium', 'Gallium', 'Ga'),&lt;br /&gt;    (32, 'Germanium', 'Germanium', 'Ge'),&lt;br /&gt;    (33, 'Arsenicum', 'Arsenic', 'As'),&lt;br /&gt;    (34, 'Selenium', 'Selenium', 'Se'),&lt;br /&gt;    (35, 'Bromum', 'Bromine', 'Br'),&lt;br /&gt;    (36, 'Krypton', 'Krypton', 'Kr'),&lt;br /&gt;    (37, 'Rubidium', 'Rubidium', 'Rb'),&lt;br /&gt;    (38, 'Strontium', 'Strontium', 'Sr'),&lt;br /&gt;    (39, 'Yttrium', 'Yttrium', 'Y'),&lt;br /&gt;    (40, 'Zirkonium', 'Zirkonium', 'Zr'),&lt;br /&gt;    (41, 'Niobium', 'Niobium', 'Nb'),&lt;br /&gt;    (42, 'Molybdaenum', 'Molybdaenum', 'Mo'),&lt;br /&gt;    (43, 'Technetium', 'Technetium', 'Tc'),&lt;br /&gt;    (44, 'Ruthenium', 'Ruthenium', 'Ru'),&lt;br /&gt;    (45, 'Rhodium', 'Rhodium', 'Rh'),&lt;br /&gt;    (46, 'Palladium', 'Palladium', 'Pd'),&lt;br /&gt;    (47, 'Argentum', 'Silver', 'Ag'),&lt;br /&gt;    (48, 'Cadmium', 'Cadmium', 'Cd'),&lt;br /&gt;    (49, 'Indium', 'Indium', ' In'),&lt;br /&gt;    (50, 'Stannum', 'Tin', 'Sn'),&lt;br /&gt;    (51, 'Stibium', 'Antimony', 'Sb'),&lt;br /&gt;    (52, 'Tellurium', 'Tellurium', 'Te'),&lt;br /&gt;    (53, 'Iodum', 'Iodine', 'I'),&lt;br /&gt;    (54, 'Xenon', 'Xenon', 'Xe'),&lt;br /&gt;    (55, 'Caesium', 'Cesium', 'Cs'),&lt;br /&gt;    (56, 'Baryum', 'Barium', 'Ba'),&lt;br /&gt;    (57, 'Lanthanum', 'Lanthanum', 'La'),&lt;br /&gt;    (58, 'Cerium', 'Cerium', 'Ce'),&lt;br /&gt;    (59, 'Praseodymium', 'Praseodymium', 'Pr'),&lt;br /&gt;    (60, 'Neodymium', 'Neodymium', 'Nd'),&lt;br /&gt;    (61, 'Promethium', 'Promethium', 'Pm'),&lt;br /&gt;    (62, 'Samarium', 'Samarium', 'Sm'),&lt;br /&gt;    (63, 'Europium', 'Europium', 'Eu'),&lt;br /&gt;    (64, 'Gadolinium', 'Gadolinium', 'Gd'),&lt;br /&gt;    (65, 'Terbium', 'Terbium', 'Tb'),&lt;br /&gt;    (66, 'Dysprosium', 'Dysprosium', 'Dy'),&lt;br /&gt;    (67, 'Holmium', 'Holmium', 'Ho'),&lt;br /&gt;    (68, 'Erbium', 'Erbium', 'Er'),&lt;br /&gt;    (69, 'Thulium', 'Thulium', 'Tm'),&lt;br /&gt;    (70, 'Ytterbium', 'Ytterbium', 'Yb'),&lt;br /&gt;    (71, 'Lutetium', 'Lutetium', 'Lu'),&lt;br /&gt;    (72, 'Hafnium', 'Hafnium', 'Hf'),&lt;br /&gt;    (73, 'Tantalum', 'Tantalum', 'Ta'),&lt;br /&gt;    (74, 'Wolframium', 'Tungsten', 'W'),&lt;br /&gt;    (75, 'Rhenium', 'Rhenium', 'Re'),&lt;br /&gt;    (76, 'Osmium', 'Osmium', 'Os'),&lt;br /&gt;    (77, 'Iridium', 'Iridium', 'Ir'),&lt;br /&gt;    (78, 'Platinum', 'Platinum', 'Pt'),&lt;br /&gt;    (79, 'Aurum', 'Gold', 'Au'),&lt;br /&gt;    (80, 'Mercury', 'Hydrargyrum', 'Hg'),&lt;br /&gt;    (81, 'Thallium', 'Thallium', 'Tl'),&lt;br /&gt;    (82, 'Plumbum', 'Lead', 'Pb'),&lt;br /&gt;    (83, 'Bismuthum', 'Bismuth', 'Bi'),&lt;br /&gt;    (84, 'Polonium', 'Polonium', 'Po'),&lt;br /&gt;    (85, 'Astatium', 'Astatine', 'At'),&lt;br /&gt;    (86, 'Radon', 'Radon', 'Rn'),&lt;br /&gt;    (87, 'Francium', 'Francium', 'Fr'),&lt;br /&gt;    (88, 'Radium', 'Radium', 'Ra'),&lt;br /&gt;    (89, 'Actinium', 'Actinium', 'Ac'),&lt;br /&gt;    (90, 'Thorium', 'Thorium', 'Th'),&lt;br /&gt;    (91, 'Protactinium', 'Protactinium', 'Pa'),&lt;br /&gt;    (92, 'Uranium', 'Uranium', 'U'),&lt;br /&gt;    (93, 'Neptunium', 'Neptunium', 'Np'),&lt;br /&gt;    (94, 'Plutonium', 'Plutonium', 'Pu'),&lt;br /&gt;    (95, 'Americium', 'Americium', 'Am'),&lt;br /&gt;    (96, 'Curium', 'Curium', 'Cm'),&lt;br /&gt;    (97, 'Berkelium', 'Berkelium', 'Bk'),&lt;br /&gt;    (98, 'Californium', 'Californium', 'Cf'),&lt;br /&gt;    (99, 'Einsteinium', 'Einsteinium', 'Es'),&lt;br /&gt;    (100, 'Fermium', 'Fermium', 'Fm'),&lt;br /&gt;    (101, 'Mendelevium', 'Mendelevium', 'Md'),&lt;br /&gt;    (102, 'Nobelium', 'Nobelium', 'No'),&lt;br /&gt;    (103, 'Lawrencium', 'Lawrencium', 'Lr'),&lt;br /&gt;    (104, 'Rutherfordium', 'Rutherfordium', 'Rf'),&lt;br /&gt;    (105, 'Dubnium', 'Dubnium', 'Db'),&lt;br /&gt;    (106, 'Seaborgium', 'Seaborgium', 'Sg'),&lt;br /&gt;    (107, 'Bohrium', 'Bohrium', 'Bh'),&lt;br /&gt;    (108, 'Hassium', 'Hassium', 'Hs'),&lt;br /&gt;    (109, 'Meitnerium', 'Meitnerium', 'Mt')&lt;br /&gt;    "&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** run the sql query ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;            {&lt;br /&gt;            echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'New record created successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;            }&lt;br /&gt;        else&lt;br /&gt;            {&lt;br /&gt;            echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to INSERT data: &lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_error&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to select the database show an error ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;else&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to select database'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close the connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we fail to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt;  &lt;p&gt;The above code will populate the elements table with all one hundred and nine elements. This is far superior to one hundred and nine seperate INSERT statements and database connections. Be aware that of the Max Allowed Packet size as large SQL statements of several thousand INSERTs or BLOBs may breach this limit. &lt;/p&gt;  &lt;h4&gt;&lt;a class="anchor" name="8.2"&gt;INSERT multiple records with mysqli&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;The mysqli extenstion provides an object oriented approach to the same task. The mysqli extension contains a class method named multi_query() for exactly this purpose. This class method basically takes an SQL statement, or multiple SQL statements concatenated by a semicolon &lt;span class="codechar"&gt;;&lt;/span&gt; character. Lets see it in action with a small subset of our periodic table of elements. We use a subset because I had to type that first lot out and it took me hours ... &lt;/p&gt;  &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql database name ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** create a new mysqli object with default database***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/* check connection */&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_connect_errno&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected Successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to INSERT a new record ***/&lt;br /&gt;    /*** note the semi colon on the end of each statement ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql  &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO elements (atomicnumber, latin, english, abbr) VALUES ( 1, 'HYDROGENIUM', 'Hydrogen', 'H');"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO elements (atomicnumber, latin, english, abbr) VALUES ( 2, 'HELIUM', 'Helium', 'He');"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO elements (atomicnumber, latin, english, abbr) VALUES ( 3, 'LITHIUM', 'Lithium', 'Li')"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO elements (atomicnumber, latin, english, abbr) VALUES ( 4, 'BERYLLIUM', 'Beryllium', 'Be');"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO elements (atomicnumber, latin, english, abbr) VALUES ( 5, 'BORUM', 'Boron', 'B')"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** run the multiple statements and check for errors ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;multi_query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) !== &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;FALSE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'New records created successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;    else&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;error&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    exit();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt;The above, shortened INSERT will work for the whole table and it is left as an exercise for the reader to complete the table. The mysqli extension provides a second method for achieving the same result for multiple INSERT. We can also use prepared statements and bind parameters. The mysql extension can send a statement or query without any data to the mysql database. You can then associate or "bind" variables to the columns. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql database name ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** create a new mysqli object with default database***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/* check connection */&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_connect_errno&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected Successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** Create the SQL statement ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'INSERT INTO elements VALUES(?, ?, ?, ?)'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** Initialize a statement object for mysqli_stmt_prepare() ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_stmt_init&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** prepare our statement ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_stmt_prepare&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;        {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** bind the parameters ***/&lt;br /&gt;          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_stmt_bind_param&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'isss'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$atomicnumber&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$latin&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$english&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$abbr&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt; &lt;br /&gt;          &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set parameters and execute ***/&lt;br /&gt;          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$atomicnumber &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$latin &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Hydrogenium'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$english &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Hydrogen'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$abbr &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'H'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_stmt_execute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt; &lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set parameters and execute ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$atomicnumber &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$latin &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Helium'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$english &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Helium'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$abbr &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'He'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_stmt_execute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;       &lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set parameters and execute ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$atomicnumber &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$latin &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Lithium'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$english &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Lithium'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$abbr &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Li'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_stmt_execute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set parameters and execute ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$atomicnumber &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;4&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$latin &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Beryllium'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$english &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Beryllium'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$abbr &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Be'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_stmt_execute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set parameters and execute ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$atomicnumber &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;5&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$latin &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Borum'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$english &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Boron'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$abbr &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'B'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_stmt_execute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    exit();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt;We see above a more modular solution to the issue. We can create code blocks also that are easy to read and easy to manage. Just a note on binding parameters. Lets examine the mysqli_stmt_bind_param() line of code... &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;mysqli_stmt_bind_param($stmt, 'isss', $atomicnumber, $latin, $english, $abbr);&lt;br /&gt;&lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt;This function binds the parameters to the query and tells the database what the parameters are. Next is the "isss" arguement. This listes the types of data that the parameters are. The &lt;span class="codechar"&gt;i&lt;/span&gt; tells mysql that the parameter is an integer. The &lt;span class="codechar"&gt;s&lt;/span&gt; character tells mysql that the parameter is a string. This arguement may be one of four types. &lt;/p&gt; &lt;ul&gt;&lt;li&gt;i - integer&lt;/li&gt;&lt;li&gt;d - double&lt;/li&gt;&lt;li&gt;s - string&lt;/li&gt;&lt;li&gt;b - BLOB&lt;/li&gt;&lt;/ul&gt; &lt;p&gt; You must have one of these for each parameter. Prepared statements separate data from logic, thereby aiding us in our quest for secure data. By telling mysql what type of data to expect we can help minimise the risk of SQL injection vulnerabilities in our code. Should any values from external sources be used it is critical that they be sanitized and validated. More on this topic later... &lt;/p&gt;  &lt;h4&gt;&lt;a class="anchor" name="8.3"&gt;INSERT multiple records with PDO&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;If you have followed the previous example with mysqli, the step to using PDO is small. The PDO interface allows us to use bound parameters and prepared statements also. The great benifit of PDO is exceptions and use of arrays for queries. For multiple a mulitple INSERT statement would could use an array as a parameter. The PDO interface will also give easy access to the use of transactions. In the following example we will see how to quickly deal with a multiple INSERT. Once again we will use only a small portion of the table to demonstrate for obvious reasons. &lt;/p&gt; &lt;div class="codebox"&gt;  &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** database name ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"mysql:host=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;;dbname=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message saying we have connected ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected to database&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set the PDO error mode to exception ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;setAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ATTR_ERRMODE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ERRMODE_EXCEPTION&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** begin the transaction ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;beginTransaction&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** our SQL statememtns ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO elements (atomicnumber, latin, english, abbr)&lt;br /&gt;    VALUES (1, 'Hydrogen', 'Hydrogen', 'H')"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO elements (atomicnumber, latin, english, abbr)&lt;br /&gt;    VALUES (1, 'Hydrogenium', 'Hydrogen', 'H')"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO elements (atomicnumber, latin, english, abbr)&lt;br /&gt;    VALUES (2, 'Helium', 'Helium', 'He')"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO elements (atomicnumber, latin, english, abbr)&lt;br /&gt;    VALUES (3, 'Lithium', 'Lithium', 'Li')"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO elements (atomicnumber, latin, english, abbr)&lt;br /&gt;    VALUES (4, 'Beryllium', 'Beryllium', 'Be')"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO elements (atomicnumber, latin, english, abbr)&lt;br /&gt;    VALUES (5, 'Borum', 'Boron', 'B')"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** commit the transaction ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;commit&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message to say the database was created ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Data entered successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;}&lt;br /&gt;catch(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDOException $e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** roll back the transaction if we fail ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;rollback&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo the sql statement and error message ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;getMessage&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt; &lt;/div&gt; &lt;p&gt;Because PDO is a total package it will of course allow us to use prepared statements and bound parameters also. Bound parameters are superior in that they minimize bandwidth the the server as you need send only the parameters each time, and not the whole query. This also reduces parsing time as the preparation on the query is done only once. Lets look at the PDO method of preparation and binding of parameters. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** database name ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"mysql:host=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;;dbname=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message saying we have connected ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected to database&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set the PDO error mode to exception ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;setAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ATTR_ERRMODE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ERRMODE_EXCEPTION&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** prepare the sql ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;prepare&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO elements (atomicnumber, latin, english, abbr)&lt;br /&gt;    VALUES (:atomicnumber, :latin, :english, :abbr)"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;bindParam&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;':atomicnumber'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$atomicnumber&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;bindParam&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;':latin'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$latin&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;bindParam&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;':english'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$english&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;bindParam&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;':abbr'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$abbr&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** insert a row ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$atomicnumber &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$latin &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Hydrogenium'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$english &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Hydrogen'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$abbr &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'H'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;execute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** insert another row ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$atomicnumber &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$latin &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Helium'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$english &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Helium'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$abbr &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'He'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;execute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** insert another row ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$atomicnumber &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$latin &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Lithium'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$english &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Lithium'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$abbr &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Li'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;execute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** insert another row ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$atomicnumber &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;4&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$latin &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Beryllium'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$english &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Beryllium'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$abbr &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Be'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;execute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** insert another row ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$atomicnumber &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;5&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$latin &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Borum'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$english &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Boron'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$abbr &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'B'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;execute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message to say the database was created ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Records entered successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;}&lt;br /&gt;catch(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDOException $e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo the sql statement and error message ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;getMessage&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt; &lt;/div&gt; &lt;h3&gt;&lt;a class="anchor" name="9"&gt;SELECT data with MySQL&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;This will be the most used feature of your database. Up to this point we have concentrated on getting connected and putting information into the database and now we will see various methods of getting this information out of the database, and of displaying it correctly on a web page. This is what dynamic web sites are all about. To access our information in the database we need to query (ask) the database with the SELECT command. SELECT will do exactly as the name describes and select what ever we tell it to. Once we SELECT the data from the database we need to be able to display it onto a web page. PHP has many tools for this job and makes life rather simple for us. But lets just get an example happening and more will be clear. &lt;/p&gt; &lt;h4&gt;&lt;a class="anchor" name="9.1"&gt;SELECT with mysql&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;Once again we still need to be able to connect to the database server and select the database we wish to extract information from. So much of the code we have used before remains the same in that respect. It is only when we wish to SELECT and display this information that things are new. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;table&gt;&lt;tr&gt;&lt;td&gt;Atomic Number&lt;/td&gt;&lt;td&gt;Latin&lt;/td&gt;&lt;td&gt;English&lt;/td&gt;&lt;td&gt;Abbreviation&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql database name ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** connect to the database ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_connect&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check if the link is a valid resource ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;is_resource&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** select the database we wish to use ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_select_db&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) === &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;TRUE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;        {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to SELECT information***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"SELECT * FROM elements"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** run the query ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$result &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check if the result is a valid resource ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;is_resource&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$result&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;            {&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check if we have more than zero rows ***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_num_rows&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$result&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) !== &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;                {&lt;br /&gt;                while(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_fetch_array&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$result&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;                    {&lt;br /&gt;                    echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;tr&gt;&lt;br /&gt;                    &lt;td&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'atomicnumber'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;].&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/td&gt;&lt;br /&gt;                    &lt;td&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'latin'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;].&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/td&gt;&lt;br /&gt;                    &lt;td&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'english'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;].&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/td&gt;&lt;br /&gt;                    &lt;td&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'abbr'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;].&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/td&gt;&lt;br /&gt;                    &lt;/tr&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;                    }&lt;br /&gt;                }&lt;br /&gt;            else&lt;br /&gt;                {&lt;br /&gt;                &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if zero results are found.. ***/&lt;br /&gt;                &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Zero results found'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        else&lt;br /&gt;            {&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if the resource is not valid ***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'No valid resource found'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to select the database show an error ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;else&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to select database '&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close the connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we fail to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt;&lt;/table&gt;&lt;br /&gt;&lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt; The table that results from this code will look like this, Only first 5 records shown for the sake of sanity. &lt;/p&gt; &lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;Atomic Number&lt;/th&gt;&lt;th&gt;Latin&lt;/th&gt;&lt;th&gt;English&lt;/th&gt;&lt;th&gt;Abbreviation&lt;/th&gt;&lt;/tr&gt; &lt;tr&gt;     &lt;td&gt;1&lt;/td&gt;     &lt;td&gt;Hydrogenium&lt;/td&gt;     &lt;td&gt;Hydrogen&lt;/td&gt;     &lt;td&gt;H&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt;     &lt;td&gt;2&lt;/td&gt;     &lt;td&gt;Helium&lt;/td&gt;     &lt;td&gt;Helium&lt;/td&gt;     &lt;td&gt;He&lt;/td&gt;                     &lt;/tr&gt; &lt;tr&gt;     &lt;td&gt;3&lt;/td&gt;     &lt;td&gt;Lithium&lt;/td&gt;     &lt;td&gt;Lithium&lt;/td&gt;     &lt;td&gt;Li&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt;     &lt;td&gt;4&lt;/td&gt;     &lt;td&gt;Beryllium&lt;/td&gt;     &lt;td&gt;Beryllium&lt;/td&gt;     &lt;td&gt;Be&lt;/td&gt;     &lt;/tr&gt; &lt;tr&gt;     &lt;td&gt;5&lt;/td&gt;     &lt;td&gt;Borum&lt;/td&gt;     &lt;td&gt;Boron&lt;/td&gt;     &lt;td&gt;B&lt;/td&gt;     &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;p&gt; OK, so there is a little to digest here. We see our connection as we have been using it in all our previous examples. The same applies for the selection of the database. If all is well up to that point, ie: no errors, then we see our SQL query.&lt;br /&gt;&lt;span class="codechar"&gt;SELECT * FROM elements&lt;/span&gt;&lt;br /&gt;The &lt;span class="codechar"&gt;*&lt;/span&gt; character is a wildcard in SQL meaning everything. What we are doing is SELECTing all records from the elements table, five of them in this example. The next line of code runs our query for us and assigns the resulting data to a variable called $result. &lt;/p&gt; &lt;p&gt;If the $result variable is a valid resource, we continue with our code. We then use the function mysql_num_rows() to check that we have more than zero amount of rows returned. The is_resource() function does not tell us if we SELECTed zero results. We could have done something like this. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;is_resource&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$result&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) &amp;amp;&amp;amp; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_num_rows&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$result&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) !== &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;display results here&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;}&lt;br /&gt;  else&lt;br /&gt;    {&lt;br /&gt;    echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'An error has occurred'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt;The above line would tell us that the result is valid AND we have more than zero lines. So why not have them together and make our code smaller? The answer lays in error checking. We may wish to handle the errors differently. If there is a problem we would not know which problem it is. Is it a problem with the result or is it a problem with the number of rows returned. Sometime verbose code is better for readability sake. &lt;/p&gt; &lt;p&gt;From there, assuming we have a valid result and more than zero rows in our result, we use the function mysql_fetch_array() to put all the results into an array that we can iterate over with a while() loop. The while() loop executes the statement within&lt;br /&gt;$row=mysql_fetch_array($result)&lt;br /&gt;as long as it is TRUE. This means it will continue looping over the result set and assigning the $row array variable which we can then access and output along with our HTML table structure. &lt;/p&gt;  &lt;h4&gt;&lt;a class="anchor" name="9.2"&gt;SELECT with mysqli&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;In our previous INSERT with mysqli we saw how to bind parameters which helped to shorten up our code base. The mysqli extension also supports bound results or "bound output parameters". Binding results helps us as we no longer need to explicitly assign results to a variable. Lets see it in action. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;table&gt;&lt;tr&gt;&lt;th&gt;Atomic Number&lt;/th&gt;&lt;th&gt;Latin&lt;/th&gt;&lt;th&gt;English&lt;/th&gt;&lt;th&gt;Abbreviation&lt;/th&gt;&lt;/tr&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql database name ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** create a new mysqli object with default database***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/* check connection */&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_connect_errno&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected Successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** our SELECT query ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"SELECT atomicnumber, latin, english, abbr FROM elements"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** prepare statement ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;prepare&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;        {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** execute our SQL query ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;execute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** bind the results ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;bind_result&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$atomicnumber&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$latin&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$english&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$abbr&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** loop over the result set ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;while (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;fetch&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;            {&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo our table rows ***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;tr&gt;&lt;td&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$atomicnumber&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/td&gt;&lt;td&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$latin&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/td&gt;&lt;td&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$english&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/td&gt;&lt;td&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$abbr&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/td&gt;&lt;/tr&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    exit();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt;&lt;/table&gt;&lt;br /&gt;&lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt; The use of bound paramenters and prepared statements are a quantum leap for developers as the increased performance, security and convenience can make scalable, large-scale developments much faster and easier to create. &lt;/p&gt;  &lt;h4&gt;&lt;a class="anchor" name="9.3"&gt;SELECT with PDO&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;The PDO extension does not have the richness of features the mysqli extension has but this is more then compensated for by the addition of the SPL class and its ability to create your own extended classes. Here we will use these built in classes to create our table. &lt;/p&gt;  &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;table style="border: solid 1px black; width: 400px;"&gt;&lt;br /&gt;&lt;tr&gt;&lt;th&gt;Atomic Number&lt;/th&gt;&lt;th&gt;Latin&lt;/th&gt;&lt;th&gt;English&lt;/th&gt;&lt;th&gt;Abbr&lt;/th&gt;&lt;/tr&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** extend the RecursiveIteratorIterator ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;class &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;TableRows &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;extends &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;RecursiveIteratorIterator&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;{&lt;br /&gt;&lt;br /&gt;function &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;__construct&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$it&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;){&lt;br /&gt;  &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** here we use the parent class and use LEAVES_ONLY to ***/&lt;br /&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;parent&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;__construct&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$it&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;self&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;LEAVES_ONLY&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** use the current method to wrap up our &lt;td&gt;s ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;function &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;current&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(){&lt;br /&gt;  return &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;td style="width: 150px; border: 1px solid black;"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;parent&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;current&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;().&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/td&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** use the beginChildren method to begin a table row ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;function &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;beginChildren&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(){&lt;br /&gt;  echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;tr&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** use the endChildren method to close a table row ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;function &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;endChildren&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;() { &lt;br /&gt;  echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/tr&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"\n"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;} &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;// end class&lt;br /&gt;&lt;br /&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** database name ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"mysql:host=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;;dbname=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set the PDO error mode to exception ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;setAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ATTR_ERRMODE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ERRMODE_EXCEPTION&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;prepare&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'SELECT * FROM elements'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** exceute the query ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;execute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/* by setting the FETCH mode we can set&lt;br /&gt;     *the resulting arrays to numerical or associative&lt;br /&gt;    */&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$result &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;setFetchMode&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;FETCH_ASSOC&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** the result should be an instance of PDOStatement ***/&lt;br /&gt;    /*** IteratorIterator converts it and after that you can do any iterator operation with it ***/&lt;br /&gt;    /*** The iterator will fetch the results for us. ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;foreach(new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;TableRows&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;RecursiveArrayIterator&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;fetchAll&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())) as &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$k&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$v&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$v&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dsn &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    } &lt;br /&gt;catch (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDOException $e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;    print &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"Error!: " &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;getMessage&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;() . &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;/span&gt; &lt;br /&gt;&lt;/table&gt;&lt;br /&gt;&lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt; So, the combination of internal PHP classes and interfaces gives a far superior and standardised method of interacting with MySQL for the purposes of displaying data. &lt;/p&gt;  &lt;h4&gt;&lt;a class="anchor" name="9.4"&gt;SELECT Mulitple Conditions&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;We have seen above how to SELECT information FROM a table in MySQL. But what if you wanted to be a little more conditional on what it is you are SELECTing. Eg: you may wish to SELECT only records that have an id of 1. Perhaps only SELECTing elements 21 and 38. To do this we use the two new concepts, &lt;span class="codechar"&gt;WHERE&lt;/span&gt; and &lt;span class="codechar"&gt;AND&lt;/span&gt;. Whichever method you are using to access the database, mysql, mysqli, or PDO, all you need do is change your SQL query as follows to SELECT records which have an atomic number of 21 &lt;span class="codechar"&gt;AND&lt;/span&gt; latin name is Scanadium. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to select with 2 conditions ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"SELECT * FROM elements WHERE atomicnumber=21 AND latin='Scanadium'"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt;  &lt;p&gt; From the above SQL query we recieve only a single result. To take this one step further you could introduce the &lt;span class="codechar"&gt;OR&lt;/span&gt; operator. It works in much the same manner except is conditional on one of the values being true rather than both. This SQL statement shows how. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to select with two possible conditions ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"SELECT * FROM elements WHERE atomicnumber=88 OR latin='Scanadium'"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt;Now we see that the we recieve two results back in our dataset as the SQL has returned any row where either of the possibilities is true. You can, of course, use more than one &lt;span class="codechar"&gt;AND&lt;/span&gt; or &lt;span class="codechar"&gt;OR&lt;/span&gt; in a SQL query to test as many conditions as you wish. &lt;/p&gt;  &lt;h3&gt;&lt;a class="anchor" name="10"&gt;Deleting Data&lt;/a&gt;&lt;/h3&gt; &lt;p&gt; Deleting data from a MySQL database is the same as running and command as we saw earlier in this tutorial. If you have read the first few sections of this tutorial then the syntax should look familiar. In this example we will delete the record with the atomic number of 4. In real world situations you would most likely get the number from a $_GET or $_POST variable from a form. This entails vigorous checking of variables for sanity and validity. This concept is covered in the tutorial &lt;a href="http://www.phpro.org/tutorials/Securing-PHP-Forms.html"&gt;Securing PHP Forms&lt;/a&gt;.  &lt;/p&gt; &lt;h4&gt;&lt;a class="anchor" name="10.1"&gt;Delete with mysql.&lt;/a&gt;&lt;/h4&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'root'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'rootpassword'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** connect to the database ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_connect&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check if the link is a valid resource ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;is_resource&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to create a database ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'DELETE FROM periodic_table WHERE atomicnumber=4'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** run the sql query ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Record Deleted&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;    else&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to Delete Record: &lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_error&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;        }&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close the connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we fail to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt; As you can see, this is exactly the same procedure with other SQL statements that do not return a result set. Lets move on with mysqli. &lt;/p&gt; &lt;h4&gt;&lt;a class="anchor" name="10.2"&gt;Delete with mysqli&lt;/a&gt;&lt;/h4&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'root'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'rootpassword'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** create a new mysqli object ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/* check connection */&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_connect_errno&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected Successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** our sql statement ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'DELETE FROM periodic_table WHERE atomicnumber=4'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) === &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;TRUE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Record Deleted successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;    else&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to Delete Record&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;error&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    exit();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt;  &lt;h4&gt;&lt;a class="anchor" name="10.3"&gt;Delete with PDO&lt;/a&gt;&lt;/h4&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'root'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'rootpassword'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"mysql:host=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;;dbname=mysql"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message saying we have connected ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected to database&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set the PDO error mode to exception ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;setAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ATTR_ERRMODE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ERRMODE_EXCEPTION&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;   &lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** our sql statement ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'DELETE FROM periodic_table WHERE atomicnumber=4'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** we use PDO::exec because no results are returned ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message to say all is well ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Record Deleted successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;catch(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDOException $e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo the sql statement and error message ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to Delete Record&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;getMessage&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt;   &lt;h3&gt;&lt;a class="anchor" name="11"&gt;UPDATING Data in MySQL&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;We have seen in previous sections how to SELECT, INSERT and DELECT data from mysql, but there are times when we need to simply UPDATE the existing information. The UPDATE key word is used by MySQL for this purpose. &lt;/p&gt; &lt;h3&gt;&lt;a class="anchor" name="11.1"&gt;UPDATE with MySQL&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;To update an existing record with the MySQL functions is much the same as the INSERT function with the only appreciable difference being the SQL query has changed. For our purposes here, we will be updating a single record and renaming the a field to lowercase, with a capital letter. There are other methods of doing this, but this example shows well the process of UPDATE. Lets see how it fits together. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** connect to the database ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_connect&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check if the link is a valid resource ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;is_resource&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** select the database we wish to use ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_select_db&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"periodic_table"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) === &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;TRUE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;        {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to UPDATE an existing record ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"UPDATE elements SET latin = 'Hydrogenium' WHERE atomicnumber=1"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** run the sql query ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;            {&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** get the number of rows UPDATE has affected ***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$affected_rows &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_affected_rows&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;            echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$affected_rows&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;' Records UPDATED successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;            }&lt;br /&gt;        else&lt;br /&gt;            {&lt;br /&gt;            echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to UPDATE data: &lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_error&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to select the database show an error ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;else&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to select database'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close the connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we fail to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt; From the code above we get the result&lt;br /&gt;Connected successfully&lt;br /&gt;1 Records UPDATED successfully &lt;/p&gt; &lt;p&gt;We have used the mysql_affected_rows() function to tell us how many rows have been updated. It should be noted here that if the SQL statement were executed with the WHERE clause, ALL of the records would be updated with the new value. This is why it is extremely important excersise caution when using UPDATE. &lt;/p&gt;  &lt;h3&gt;&lt;a class="anchor" name="11.2"&gt;UPDATE with mysqli&lt;/a&gt;&lt;/h3&gt; &lt;p&gt; Once again, this is similar to the INSERT statement with just the SQL changing to perform the UPDATE. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql database name ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** create a new mysqli object with default database***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/* check connection */&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_connect_errno&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected Successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to UPDATE an existing record ***/&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"UPDATE elements SET latin = 'Hydogenium' WHERE atomicnumber=1"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** execute the query ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) === &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;TRUE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_affected_rows&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;). &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;' Records UPDATED successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;    else&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to UPDATE Records: '&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;error&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to connect ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    exit();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt;Above we see again that the record has been updated. This time we have used the mysqli_affected_rows() function to tell us how many records have been affected by the query, in this case, just the single record. &lt;/p&gt; &lt;h3&gt;&lt;a class="anchor" name="11.3"&gt;UPDATE with PDO&lt;/a&gt;&lt;/h3&gt; &lt;p&gt; The PDO class has a much simpler approach for UPDATING as it does with INSERT and all database interactions. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** database name ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"mysql:host=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;;dbname=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message saying we have connected ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected to database&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set the PDO error mode to exception ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;setAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ATTR_ERRMODE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ERRMODE_EXCEPTION&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;   &lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to UPDATE an existing record ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"UPDATE elements SET latin = 'Hydrogenium' WHERE atomicnumber=1"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** we use PDO::exec because no results are returned ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message to say the UPDATE succeeded ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Records UPDATED successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;catch(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDOException $e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo the sql statement and error message ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'UPDATE Failed: '&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;getMessage&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt; The above code of course returns&lt;br /&gt;Records UPDATED successfully&lt;br /&gt;However, this does not give us the number of records affected but the UPDATE. The PDO class itself has no PDO::affected_rows. If you need this functionality, you need to use the PDOStatement::rowCount() method. It looks like this: &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** database name ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"mysql:host=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;;dbname=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message saying we have connected ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected to database&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set the PDO error mode to exception ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;setAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ATTR_ERRMODE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ERRMODE_EXCEPTION&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;   &lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to UPDATE an existing record ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"UPDATE elements SET latin = 'Hydrognium' WHERE atomicnumber=1"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** Prepare statement ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;prepare&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** execute the query ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;execute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message to say the UPDATE succeeded ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;rowCount&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;().&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;' Records UPDATED successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;catch(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDOException $e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo the sql statement and error message ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'UPDATE Failed: '&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;getMessage&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt;Now of course we can see the number of rows affected. It is important to note that if the new value is exactly the same as the existing value in the database, then MySQL will not report change anything, and zero will be returned by mysql_affected_rows(), mysqli_affected_rows and PDOStatement::rowCount(). &lt;/p&gt;   &lt;h3&gt;&lt;a class="anchor" name="12"&gt;LIMIT Data Selections&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;MySQL provides a LIMIT clause that, as the name suggests, limits the number of records affected by an SQL query or statement. Imagine we wished to SELECT all the records from 1 - 20 inclusive in our SELECT query. As this is strictly a MySQL issue no PHP code is provided. Here we will display the SQL need to SELECT. The PHP code is exactly the same as in the above SELECT section. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;$sql = "SELECT * FROM periodic_table LIMIT 20";&lt;br /&gt;&lt;/span&gt; &lt;/code&gt;&lt;/div&gt;  &lt;p&gt;When run, the above SQL query will return the first twenty results which you can then do with what you wish. But what If I want to offset the beginning of the records and SELECT records 6-15 inclusive? Mysql provides a partner to cater for offsets called OFFSET. When used with LIMIT this will provide a result set beginning with the OFFSET number, and limited to the number of LIMIT. Lets see it working. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;/*** SQL query with LIMIT and OFFSET ***/&lt;br /&gt;$sql ="SELECT * FROM periodic_table 5, 10&lt;br /&gt;&lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt;This can be very effective for use in pagination of PHP pages used for photo sites or any series of data objects stored in a mysql table. &lt;/p&gt;  &lt;h3&gt;&lt;a class="anchor" name="13"&gt;Configuration Options&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;The Mysql extension comes some very handy configuration options to help change the runtime behaviour as described in php.ini. The options include: &lt;/p&gt; &lt;ul&gt;&lt;li&gt;mysql.allow_persistent&lt;/li&gt;&lt;li&gt;mysql.max_persistent&lt;/li&gt;&lt;li&gt;mysql.max_links&lt;/li&gt;&lt;li&gt;mysql.trace_mode&lt;/li&gt;&lt;li&gt;mysql.default_port&lt;/li&gt;&lt;li&gt;mysql.default_socket&lt;/li&gt;&lt;li&gt;mysql.default_host&lt;/li&gt;&lt;li&gt;mysql.default_user&lt;/li&gt;&lt;li&gt;mysql.default_password&lt;/li&gt;&lt;li&gt;mysql.connect_timeout&lt;/li&gt;&lt;/ul&gt; &lt;p&gt; At a glance it is not difficult to see what these options do. But we will step through the just the same. &lt;/p&gt; &lt;h4&gt;mysql.allow_persistent&lt;/h4&gt; &lt;p&gt;This option defines whether to allow mysql_pconnect() to allow persistant connections to the database. The default value for this is On and reasons for disabling this option may be that many scripts running persistant connections could have the MySQL server quickly reach its maximum number of allowed connections due to the persistant connections not being closed when the sctipt is finished, nor when mysql_close() is called. &lt;/p&gt; &lt;h4&gt;mysql.max_persistent&lt;/h4&gt; &lt;p&gt;Of course, if the above scenario looks likely and you still wish to allow a sane level of persistant connections, this option may be set. The default value of this option is -1 which allows unlimitted connections. &lt;/p&gt; &lt;h4&gt;mysql.max_links&lt;/h4&gt; &lt;p&gt;This option differs from the above by setting the maximum number of connections, including persistant connections, per process. The default value of -1 means no limit is set. &lt;/p&gt; &lt;h4&gt;mysql.trace_mode&lt;/h4&gt; &lt;p&gt;When the trace_mode option is set to On, warnings for table and index scans and and SQL errors will be displayed. For good reason, the default value for this option is set to Off. &lt;/p&gt; &lt;h4&gt;mysql.default_port&lt;/h4&gt; &lt;p&gt;Setting the default port with this option will allow the use of mysql_connect() to connect to a pre-defined port number. If this option is not set, then it will default to the default port as specified by $MYSQL_TCP_PORT or for windows, the compile time defined MYSQL_PORT. *nix users may also find it uses the mysql-tcp port entry in /etc/services or the compile time defined MYSQL_PORT. Note: *nix systems will check for the default port in the order of $MYSQL_TCP_PORT, mysql-tcp port entry in /etc/services, MYSQL_PORT. &lt;/p&gt; &lt;h4&gt;mysql.default_socket&lt;/h4&gt; &lt;p&gt;By setting this option you may set the default socket name for local MySQL connections. If this option is left empty, the MySQL default is used. &lt;/p&gt; &lt;h4&gt;mysql.default_host&lt;/h4&gt; &lt;p&gt; This option sets the default host used for mysql_connect(). Note: This option is not available in Safe Mode. &lt;/p&gt; &lt;h4&gt;mysql.default_user&lt;/h4&gt; &lt;p&gt;Similar to the above, it sets the default username for use with mysql_connect(). Note: This option is not available in Safe Mode. &lt;/p&gt; &lt;h4&gt;mysql.default_password&lt;/h4&gt; &lt;p&gt;And to complment the above, but sets the default password for mysql_connect. Note: this is a Bad Thing(tm). Any system user with read access to php.ini can see this or even worse, a php script with such as this can see the value: &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;cfg_get_var&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"mysql.default_password"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;h4&gt;mysql.connect_timeout&lt;/h4&gt; &lt;p&gt;As the name suggests, this sets the default time out, in seconds, for MySQL connectios. The default is 60 seconds and a value of -1 means no limit it set. Note: Do not set this to -1. &lt;/p&gt;  &lt;h3&gt;&lt;a class="anchor" name="14"&gt;Formatting Dates with MySQL&lt;/a&gt;&lt;/h3&gt; &lt;p&gt; When SELECTing a data value from MySQL it is generally returned as a MySQL timestamp that looks like this:&lt;br /&gt;2007-10-23 22:23:00&lt;br /&gt;This is generally not useful for end users who know nothing of MySQL timestamps. There are several ways we can deal with dates, and of course, wherever there are different methods of doing something, holy wars arise and prophets sprout up from under their rocks to tell you the "right" way to do it, and should you vary from thier chosen path, you are of course an idiot. We will make no such judgement here today and simply show you two methods of date formatting. The first we will select the date from MySQL as a UNIX TIMESTAMP rather than the default MySQL timestamp. We can then use the PHP date functions for format the date into whatever order we choose. &lt;/p&gt; &lt;p&gt;We will create a temporary table with a date field for the purpose and INSERT a record. The first MySQL TIMESTAMP field in a table will automatically populate with the current time and date, ie: NOW(). We will then SELECT the date_field as it comes from the database. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** database name ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"mysql:host=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;;dbname=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message saying we have connected ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected to database&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set the PDO error mode to exception ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;setAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ATTR_ERRMODE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ERRMODE_EXCEPTION&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** begin the transaction ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;beginTransaction&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** CREATE a TEMPORARY TABLE and INSERT a record  ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"CREATE TEMPORARY TABLE  date_table (id INT(2), date_field TIMESTAMP(14),&lt;br /&gt;    INDEX USING BTREE (id)) ENGINE = MEMORY"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO date_table (id) VALUES (1)"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** commit the transaction ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;commit&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message to say the database was created ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Data entered successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** SQL to fetch the date ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"SELECT * FROM date_table"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** prepare the query ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;prepare&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    if (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;execute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;        {&lt;br /&gt;        while (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;fetch&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;            {&lt;br /&gt;            echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'date_field'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;];&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;}&lt;br /&gt;catch(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDOException $e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** roll back the transaction if we fail ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;rollback&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo the sql statement and error message ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;getMessage&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt; When the above code is run, it returns a result similar (with different date and time) to this:&lt;br /&gt;Connected to database&lt;br /&gt;Data entered successfully&lt;br /&gt;2007-01-05 02:30:07 &lt;/p&gt; &lt;p&gt; This is the standard format of a MySQL TIMESTAMP, but it means little to most end users. &lt;/p&gt;  &lt;h3&gt;&lt;a class="anchor" name="14.1"&gt;Format Date with PHP&lt;/a&gt;&lt;/h3&gt;To get the date_field value into a UNIX TIMESTAMP that can be used by PHP date functions we need to change the SQL statement to this:&lt;br /&gt;"SELECT *, UNIX_TIMESTAMP(date_field) AS my_date FROM date_table"&lt;br /&gt;Lets run through the script a second time and show the new date value, then format the value with PHP date() function.   &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** database name ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"mysql:host=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;;dbname=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message saying we have connected ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected to database&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set the PDO error mode to exception ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;setAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ATTR_ERRMODE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ERRMODE_EXCEPTION&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** begin the transaction ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;beginTransaction&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** CREATE a TEMPORARY TABLE and INSERT a record  ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"CREATE TEMPORARY TABLE  date_table (id INT(2), date_field TIMESTAMP(14),&lt;br /&gt;    INDEX USING BTREE (id)) ENGINE = MEMORY"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO date_table (id) VALUES (1)"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** commit the transaction ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;commit&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message to say the database was created ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Data entered successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** SQL to fetch the date ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"SELECT *, UNIX_TIMESTAMP(date_field) AS my_date FROM date_table"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** prepare the query ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;prepare&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    if (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;execute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;        {&lt;br /&gt;        while (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;fetch&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;            {&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** assingn a variable ***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$unix_timestamp &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'my_date'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;];&lt;br /&gt;            echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"UNIX TIMESTAMP: &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$unix_timestamp&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;&lt;br /&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo the Formatted date ***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"Formatted Date: "&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;date&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'D, d M Y H:i:s T'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;}&lt;br /&gt;catch(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDOException $e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** roll back the transaction if we fail ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;rollback&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo the sql statement and error message ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;getMessage&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt; The above code will produce output similar to this:&lt;br /&gt;Connected to database&lt;br /&gt;Data entered successfully&lt;br /&gt;UNIX TIMESTAMP: 1167948339&lt;br /&gt;Formatted Date: Fri, 05 Jan 2007 09:05:39 EST &lt;/p&gt; &lt;p&gt; From the code above we can see the use of the PHP date() function to make the UNIX TIMESTAMP into a human readable interpretation using the line&lt;br /&gt;echo "Formatted Date: ".date(\'D, d M Y H:i:s T\');&lt;br /&gt;Of course, you may want your date formatted differently to this, so PHP offers a wide range of variations and can seen in PHP manual date section. &lt;/p&gt;  &lt;h3&gt;&lt;a class="anchor" name="14.2"&gt;Format Date with MySQL&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;MySQL also has the ability to format dates. The MySQL built in function DATE_FORMAT() is used for this purpose. This allows the formatting of date directly in the database when the SELECT is made, and no PHP code is needed to alter the result. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** database name ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"mysql:host=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;;dbname=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message saying we have connected ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected to database&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set the PDO error mode to exception ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;setAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ATTR_ERRMODE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ERRMODE_EXCEPTION&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** begin the transaction ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;beginTransaction&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** CREATE a TEMPORARY TABLE and INSERT a record  ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"CREATE TEMPORARY TABLE  date_table (id INT(2), date_field TIMESTAMP(14),&lt;br /&gt;    INDEX USING BTREE (id)) ENGINE = MEMORY"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO date_table (id) VALUES (1)"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** commit the transaction ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;commit&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message to say the database was created ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Data entered successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** SQL to fetch the date ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"SELECT *, DATE_FORMAT(date_field, '%W, %d %Y %r') AS my_date FROM date_table"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** prepare the query ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;prepare&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    if (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;execute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;        {&lt;br /&gt;        while (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;fetch&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;            {&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo the formatted date ***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"Formatted Date: "&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'my_date'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;];&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;}&lt;br /&gt;catch(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDOException $e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** roll back the transaction if we fail ***/&lt;br /&gt;&lt;br /&gt;    /*** echo the sql statement and error message ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;getMessage&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt; The above code uses the the SQL statement:&lt;br /&gt;"SELECT *, DATE_FORMAT(date_field, '%W, %d %Y %r') AS my_date FROM date_table"&lt;br /&gt;This line has MySQL do the date formatting for us and makes for some shorter PHP code. The output is similar and presented like this:&lt;/p&gt; &lt;p&gt; Connected to database&lt;br /&gt;Data entered successfully&lt;br /&gt;Formatted Date: Friday, 05 2007 10:06:30 AM &lt;/p&gt; &lt;p&gt; There are many other formatting options available in MySQL along with a wide range of date manipulation features. The MySQL manual is recommended reading for more information. &lt;/p&gt;  &lt;h3&gt;&lt;a class="anchor" name="14.3"&gt;Format Date with PHP and MySQL&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;We have seen in the two sections above different methods for formatting dates. Both work well and produce similar output. However their is one difference that is quite important. We note in the first example that the timezone is Eastern Standard Time (EST) and in the second example, no timezone information is avialable. This could lead to problems when, as is often the case, the HTTP server and the MySQL server are on two different machines, and, in two different timezones. We can use a combination of PHP and MySQL to fix this problem and set a default timezone all our date information. Here we will discard the time settings, and focus on the DATE only. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** database name ***/&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"mysql:host=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;;dbname=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message saying we have connected ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected to database&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set the PDO error mode to exception ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;setAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ATTR_ERRMODE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ERRMODE_EXCEPTION&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** begin the transaction ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;beginTransaction&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** CREATE a TEMPORARY TABLE and INSERT a record  ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"CREATE TEMPORARY TABLE  date_table (id INT(2), date_field TIMESTAMP(14),&lt;br /&gt;    INDEX USING BTREE (id)) ENGINE = MEMORY"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO date_table (id) VALUES (1)"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** commit the transaction ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;commit&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message to say the database was created ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Data entered successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** SQL to fetch the date ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"SELECT * FROM date_table"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** prepare the query ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;prepare&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    if (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;execute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;        {&lt;br /&gt;        while (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$stmt&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;fetch&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;            {&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** assign the date to a variable ***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$my_date &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'date_field'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;];&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** the server timezones ***/&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysql_timezone &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;DateTimeZone&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"Europe/London"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$httpd_timezone &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;DateTimeZone&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"Australia/Sydney"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysql_datetime &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;date_create&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$my_date &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysql_timezone&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set the local timezone ***/&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$httpd_timezone &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;DateTimeZone&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"Australia/Sydney"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$httpd_datetime &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;Datetime&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$my_date &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$httpd_timezone&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** display the local time ***/&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Sydney Time: '&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$httpd_datetime&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;format&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'D, d M Y H:i:s'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;).&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** show the time difference ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$offset &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$httpd_timezone&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;getOffset&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysql_datetime&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** modify the time with the offet ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$httpd_datetime&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;modify&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$offset&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt; seconds"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** show the London time ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'London Time: '&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$httpd_datetime&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;format&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'D, d M Y H:i:s'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;).&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;catch(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDOException $e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** roll back the transaction if we fail ***/&lt;br /&gt;&lt;br /&gt;    /*** echo the sql statement and error message ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;getMessage&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;catch(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;Exception $e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;    echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;getMessage&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt;  &lt;p&gt;This sort of time juggling can come in handy in many situations. Even if you choose only to display a single date from your database with the DateTime class, you will be able to do so in an environment that allows you to set you default timezone. &lt;/p&gt;  &lt;h3&gt;&lt;a class="anchor" name="15"&gt;Load a CSV File&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;If you are reading this section then you know what a CSV file is. Lets take a look at a quicky that would represent data to go into our periodic table of elements. We will name this file periodic_table.csv and it will look like this. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;1, HYDROGENIUM, Hydrogen, H&lt;br /&gt;2, HELIUM, Helium, He&lt;br /&gt;3, LITHIUM, Lithium, Li&lt;br /&gt;4, Beryllium, Beryllium, Be&lt;br /&gt;5, Borum, Boron, B&lt;br /&gt;&lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt;We have kept the number of elements to 5 for the sake of brevity and sanity. Using any of the mysql, mysqli, or PDO methods of executing a query will work. All that changes is the SQL statement used to load the file. To load the file into the respective fieds, which are&lt;br /&gt;atomicnumber, latin, english, abbr&lt;br /&gt;we use the following SQL statement &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"LOAD DATA INFILE '/path/to/periodic_table.csv' REPLACE INTO TABLE elements&lt;br /&gt;FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (atomicnumber, latin, english, abbr)"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt; Then by running the query you will have successfully loaded the file. &lt;/p&gt;   &lt;h3&gt;&lt;a class="anchor" name="16"&gt;Preventing SQL Injection&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;What is SQL injection?&lt;br /&gt;SQL injection is a type of attack that allows users to execute SQL statements via a web form. This definition is a little concise, as SQL injection relates to a whole class of attacks. You will pick it up quickly as we progress. All problems relating to this type of attack come from a single source, not checking or validating user input. Before we go on, lets see a little example of what an attack using SQL injection looks like. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;form method&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"post"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;&gt;&lt;br /&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;input type&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"text" &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"searchtext" &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;/&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;br &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;/&gt;&lt;br /&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;select name&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"fieldname"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;&gt;&lt;br /&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;option value&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"atomicnumber"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;Atomic Number&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;&lt;/&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;option&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;&gt;&lt;br /&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;option value&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"latin"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;Latin&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;&lt;/&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;option&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;&gt;&lt;br /&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;option value&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"english"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;English&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;&lt;/&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;option&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;&gt;&lt;br /&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;option value&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"abbr"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;Abbreviation&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;&lt;/&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;option&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;&gt;&lt;br /&gt;&lt;/&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;select&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;&gt;&lt;br /&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;input type&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"submit" &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;value&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"Query" &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;/&gt;&lt;br /&gt;&lt;/&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;form&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;&gt;&lt;br /&gt;&lt;?&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;php&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(isset(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$_POST&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'searchtext'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;], &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$_POST&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'fieldname'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;]) &amp;amp;&amp;amp; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$_POST&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'searchtext'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;] != &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;''&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql database name ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** connect to the database ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_connect&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check if the link is a valid resource ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;is_resource&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;        {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** select the database we wish to use ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_select_db&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) === &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;TRUE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;            {&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to SELECT information***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"SELECT * FROM elements WHERE "&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$_POST&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'fieldname'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;].&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;" = '"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$_POST&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'searchtext'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;].&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"'"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo the sql query ***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;h3&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/h3&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;   &lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** run the query ***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$result &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check if the result is a valid resource ***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;is_resource&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$result&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;                {&lt;br /&gt;                &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check if we have more than zero rows ***/&lt;br /&gt;                &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_num_rows&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$result&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) !== &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;                    {&lt;br /&gt;                    echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;table&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;                    while(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_fetch_array&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$result&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;                        {&lt;br /&gt;                        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;tr&gt;&lt;br /&gt;                        &lt;td&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'atomicnumber'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;].&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/td&gt;&lt;br /&gt;                        &lt;td&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'latin'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;].&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/td&gt;&lt;br /&gt;                        &lt;td&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'english'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;].&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/td&gt;&lt;br /&gt;                        &lt;td&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'abbr'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;].&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/td&gt;&lt;br /&gt;                        &lt;/tr&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;                        }&lt;br /&gt;                    echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/table&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;                    }&lt;br /&gt;                else&lt;br /&gt;                    {&lt;br /&gt;                    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if zero results are found.. ***/&lt;br /&gt;                    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Zero results found'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;                    }&lt;br /&gt;                }&lt;br /&gt;            else&lt;br /&gt;                {&lt;br /&gt;                &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if the resource is not valid ***/&lt;br /&gt;                &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'No valid resource found'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to select the database show an error ****/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;else&lt;br /&gt;            {&lt;br /&gt;            echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to select database '&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;            }&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close the connection ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;        }&lt;br /&gt;    else&lt;br /&gt;        {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we fail to connect ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Please Choose An Element'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt;The form and PHP script above is not uncommon to many that populate the web. The form will work without issue when given the correct data to work with. Like many PHP scripts, it relies on data supplied from userland, in this case from POST. The values may come from GET or even curl. Lets look at the problem. If we were to enter the number one (1) in the search text box and the dropdown menu set to Atomic Number, we would get a result that looked like this: &lt;/p&gt; &lt;div class="displaybox"&gt; &lt;h3&gt;SELECT * FROM elements WHERE atomicnumber = '1'&lt;/h3&gt; &lt;table&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;Hydrognium&lt;/td&gt; &lt;td&gt;Hydrogen&lt;/td&gt; &lt;td&gt;H&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;/div&gt; &lt;p&gt;The results are as expected and the script works quite well, there is plenty of runtime error checking etc, but.. a malicious user may be able to enter, or inject, their own SQL code into the query. Lets change the search text to look like this. &lt;/p&gt; &lt;div class="codebox"&gt; 1' OR latin='Helium &lt;/div&gt; &lt;p&gt; Now when we run the query the results look like this: &lt;/p&gt; &lt;div class="displaybox"&gt; &lt;h3&gt;SELECT * FROM elements WHERE atomicnumber = '1' OR latin='Helium'&lt;/h3&gt;&lt;table&gt; &lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Hydrognium&lt;/td&gt;&lt;td&gt;Hydrogen&lt;/td&gt;&lt;td&gt;H&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;Helium&lt;/td&gt;&lt;td&gt;Helium&lt;/td&gt;&lt;td&gt;He&lt;/td&gt;&lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;/div&gt; &lt;p&gt;We have now successfully exploited the script and injected our own SQL into it. Well, big deal you might say, you can see an extra row. As it stands, this is quite harmless. But suppose our malicious user is a total ass and wants to play dirty. Lets now change the search text to look like this: &lt;/p&gt; &lt;div class="displaybox"&gt; 1' OR 1='1 &lt;/div&gt; &lt;p&gt; The SQL query will now look like this: &lt;/p&gt; &lt;div class="displaybox"&gt; SELECT * FROM elements WHERE atomicnumber = '1' OR 1='1' &lt;/div&gt; &lt;p&gt;Now when we submit the form, the results show the entire table! Most web sites will have a user database, so lets add one to our periodic_table database. Here is a dump of what it may look like. &lt;/p&gt; &lt;div class="codebox"&gt; CREATE TABLE p_users (&lt;br /&gt;  user_id tinyint(2) NOT NULL auto_increment,&lt;br /&gt;  user_name varchar(50) NOT NULL,&lt;br /&gt;  user_pass varchar(50) NOT NULL,&lt;br /&gt;  PRIMARY KEY  (user_id)&lt;br /&gt;) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;&lt;br /&gt;&lt;br /&gt;INSERT INTO p_users (user_id, user_name, user_pass)&lt;br /&gt;VALUES (1, 'admin', 'secretpassword'),&lt;br /&gt;(2, 'lame_user', 'lame_password');&lt;br /&gt;&lt;/div&gt;  &lt;p&gt;With the p_users table in place, our simple SQL inject exploit now takes on tragic proportions. With a little craftiness we can guess that there is an username called admin, or perhaps a regular users username may be known. We can construct a query now to guess what the password may be. In our current p_users database the admin password is secretpassword. Our query could be to test if the first letter is the character 'a'. So in our search text field we put this code: &lt;/p&gt; &lt;div class="codebox"&gt; ' OR EXISTS(SELECT * FROM p_users WHERE user_name='admin' AND user_pass LIKE 'a%') AND ''=' &lt;/div&gt; &lt;p&gt; This will produce an output like this &lt;/p&gt; &lt;div class="displaybox"&gt; &lt;h3&gt;SELECT * FROM elements WHERE atomicnumber = '' OR EXISTS(SELECT * FROM p_users WHERE user_name='admin' AND user_pass LIKE 'a%') AND ''=''&lt;/h3&gt; Zero results found &lt;/div&gt; &lt;p&gt; This means the password does not begin with an a, lets see what happens if we wish to see if it begins with an 's'. We simple change our search text to this: &lt;/p&gt; &lt;div class="codebox"&gt; ' OR EXISTS(SELECT * FROM p_users WHERE user_name='admin' AND user_pass LIKE 's%') AND ''=' &lt;/div&gt; &lt;p&gt; Now we see a minor change to the query and it looks like this: &lt;/p&gt; &lt;div class="displaybox"&gt; &lt;h3&gt;SELECT * FROM elements WHERE atomicnumber = '' OR EXISTS(SELECT * FROM p_users WHERE user_name='admin' AND user_pass LIKE 's%') AND ''=''&lt;/h3&gt; &lt;/div&gt; &lt;p&gt; But below this we see the contents of the table. This tells us the query was successful and the password does indeed begin wthe character 's'. It is now simple task to follow along and get the remaining characters by simply altering the search text again, we can move along the password string. &lt;/p&gt; &lt;div class="codebox"&gt; ' OR EXISTS(SELECT * FROM p_users WHERE user_name='admin' AND user_pass LIKE '_e%') AND ''=' &lt;/div&gt; &lt;p&gt;This process can be continued till the password is correctly guessed. Then, we are at the mercy of user. The same method can be used to find a username also. By using wildcards it is possible to see if a username contains the letter 'b'. With a little trial and error, the username and password can quickly fall into the wrong hands. &lt;/p&gt;&lt;div class="codebox"&gt; ' OR EXISTS(SELECT * FROM p_users WHERE user_name='bill' AND user_pass LIKE '%b%') AND ''=' &lt;/div&gt; &lt;p&gt;Of course, this type of attack is common in open source environments where it is trivial to access the source code to see what the table names are. But even with closed source, it is still possible to see the table names. Security through obscurity is a myth. &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;div class="codebox"&gt; /* Does the database contain the letter p? */&lt;br /&gt;' OR EXISTS(SELECT 1 FROM dual WHERE database() LIKE '%p%') AND ''='&lt;br /&gt;/* check if there is a table called foo in database periodic_table */&lt;br /&gt;' OR EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='periodic_table' AND TABLE_NAME='foo') AND ''='&lt;br /&gt;/* check if there is more than one table in the database containing the letter p? */ ' OR (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA LIKE '%p%')&gt;1 AND ''=' &lt;/div&gt;  &lt;p&gt;Of course, most haXorZ just want to gain access and will try to brute force their way in. A clever hacker though, may try something like this if promted for a username and password to access the system. &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;div class="codebox"&gt; ' OR ''='  &lt;/div&gt; &lt;p&gt;If this is provided for both the username and the password fields, it will always evaluate as true and entry is gained. You lose again. So, how do you protect yourself from this sort of thing? We begin with a simple caveat when accepting variables from users... &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;ol&gt;&lt;li&gt;NEVER TRUST USER INPUT&lt;/li&gt;&lt;li&gt;NEVER TRUST USER INPUT&lt;/li&gt;&lt;li&gt;NEVER TRUST USER INPUT&lt;/li&gt;&lt;li&gt;NEVER TRUST USER INPUT&lt;/li&gt;&lt;li&gt;NEVER TRUST USER INPUT&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;These five simple rules should make it clear that we need to be careful with information we gain from outside sources. With proper filtering of variables, and escaping of data, we can protect ourselves from these types of attacts. Lets run through them. &lt;/p&gt; &lt;h3&gt;&lt;a class="anchor" name="16.1"&gt;Prevent with MySQL&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;MySQL comes with a client function named mysql_real_escape_string that is used for escaping data to prevent SQL injection. The PHP conterpart to this function is mysql_real_escape_string(). When we accept data from external sources, such as a web form or via the query string, it is essential to that the varaible values be checked to be sure we are getting what we are expecting. Checks also need to be made that each variable is safe. Here we show how this may be achieved using the standard MySQL functions. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;form method="post"&gt;&lt;br /&gt;&lt;input type="text" name="searchtext"&gt;&lt;br /&gt;&lt;br /&gt;&lt;select name="fieldname"&gt;&lt;br /&gt;&lt;option value="atomicnumber"&gt;Atomic Number&lt;/option&gt;&lt;br /&gt;&lt;option value="latin"&gt;Latin&lt;/option&gt;&lt;br /&gt;&lt;option value="english"&gt;English&lt;/option&gt;&lt;br /&gt;&lt;option value="abbr"&gt;Abbreviation&lt;/option&gt;&lt;br /&gt;&lt;/select&gt;&lt;br /&gt;&lt;input type="submit" value="Query"&gt;&lt;br /&gt;&lt;/form&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;filter_has_var&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;INPUT_POST&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'searchtext'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) &amp;amp;&amp;amp; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;filter_has_var&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;INPUT_POST&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'fieldname'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;    {&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check the variables for content ***/&lt;br /&gt;   &lt;br /&gt;    /*** a list of filters ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$filters &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= array(&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'searchtext' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&gt; array( &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'filter' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;FILTER_CALLBACK&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'options'  &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&gt; &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'mysql_real_escape_string'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;),&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'fieldname'  &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&gt; array( &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'filter' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;FILTER_CALLBACK&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'options'  &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&gt; &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'mysql_real_escape_string'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    );&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** escape all POST variables ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$input &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;filter_input_array&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;INPUT_POST&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$filters&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check the values are not empty ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(empty(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$input&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'fieldname'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;]) || empty(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$input&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'searchtext'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;]))&lt;br /&gt;        {&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Invalid search'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;    else&lt;br /&gt;        {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;   &lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql database name ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'periodic_table'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** connect to the database ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_connect&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check if the link is a valid resource ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;is_resource&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;            {&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** select the database we wish to use ***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_select_db&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) === &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;TRUE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;                {&lt;br /&gt;                &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to SELECT information***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;sprintf&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"SELECT * FROM elements WHERE %s = '%s'"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$input&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'fieldname'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;], &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$input&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'searchtext'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;]);&lt;br /&gt;&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo the sql query ***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;h3&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/h3&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;   &lt;br /&gt;                &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** run the query ***/&lt;br /&gt;                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$result &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;                &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check if the result is a valid resource ***/&lt;br /&gt;                &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;is_resource&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$result&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;                    {&lt;br /&gt;                    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check if we have more than zero rows ***/&lt;br /&gt;                    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_num_rows&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$result&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) !== &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;                        {&lt;br /&gt;                echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;table&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;                        while(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_fetch_array&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$result&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;                            {&lt;br /&gt;                            echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;tr&gt;&lt;br /&gt;                            &lt;td&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'atomicnumber'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;].&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/td&gt;&lt;br /&gt;                            &lt;td&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'latin'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;].&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/td&gt;&lt;br /&gt;                            &lt;td&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'english'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;].&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/td&gt;&lt;br /&gt;                            &lt;td&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$row&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'abbr'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;].&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/td&gt;&lt;br /&gt;                            &lt;/tr&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;                            }&lt;br /&gt;                echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;/table&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;                        }&lt;br /&gt;                    else&lt;br /&gt;                        {&lt;br /&gt;                        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if zero results are found.. ***/&lt;br /&gt;                        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Zero results found'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;                        }&lt;br /&gt;                    }&lt;br /&gt;                else&lt;br /&gt;                    {&lt;br /&gt;                    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if the resource is not valid ***/&lt;br /&gt;                    &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'No valid resource found'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;                    }&lt;br /&gt;                }&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to select the database show an error ****/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;else&lt;br /&gt;                {&lt;br /&gt;                echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to select database '&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;                }&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close the connection ***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;            }&lt;br /&gt;        else&lt;br /&gt;            {&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we fail to connect ***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;else&lt;br /&gt;    {&lt;br /&gt;    echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Please Choose An Element'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt;The code above will accept our input queries as expected, however, now if we try to submit any nastiness in the search text like that below our results will be different than we have previously seen. &lt;/p&gt; &lt;div class="displaybox"&gt; 1' OR latin='Helium &lt;/div&gt; &lt;p&gt; By using the above searchtext string we now see that the query and results look like this: &lt;/p&gt; &lt;div class="displaybox"&gt; &lt;h3&gt;SELECT * FROM elements WHERE atomicnumber = '1\' OR latin=\'Helium'&lt;/h3&gt; &lt;table&gt; &lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Hydrognium&lt;/td&gt;&lt;td&gt;Hydrogen&lt;/td&gt;&lt;td&gt;H&lt;/td&gt;&lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;/div&gt; &lt;p&gt;As you can see, the search string has been escaped and is no longer a threat to our SQL query. We have used the php filter_input_array with a callback to mysql_real_escape_string to filter the variables from the POST array. This allows the use of the variables safely in our query. Prior to that, we added a check to see if the values were empty. This will prevent a malicious users trying to use other input methods to match two null values, which would evaluate to TRUE. &lt;/p&gt; &lt;h3&gt;&lt;a class="anchor" name="16.2"&gt;Prevent with MySQLi&lt;/a&gt;&lt;/h3&gt; &lt;h3&gt;&lt;a class="anchor" name="16.3"&gt;Prevent with PDO&lt;/a&gt;&lt;/h3&gt;  &lt;h3&gt;&lt;a class="anchor" name="17"&gt;Last Insert ID&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;The MySQL extensions permit the fetching of the ID of the last INSERT on a table with an AUTO_INCREMENT field. This means when a record is added, the ID can be retrieved immediately. &lt;/p&gt; &lt;p&gt;The following code examples assume a database with the fields user_id, user_name, user_email and user_dob. The schema would look like this. Note that the user_id field is an AUTO_INCREMENT field. &lt;/p&gt; &lt;div class="displaybox"&gt; &lt;pre&gt;CREATE TABLE users (&lt;br /&gt; user_id int(11) NOT NULL AUTO_INCREMENT,&lt;br /&gt; user_name varchar(25) NOT NULL,&lt;br /&gt; user_email varchar(100) NOT NULL,&lt;br /&gt; user_dob datetime NOT NULL,&lt;br /&gt; PRIMARY KEY (user_id)&lt;br /&gt;) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;&lt;br /&gt;&lt;/pre&gt; &lt;/div&gt;    &lt;h3&gt;&lt;a class="anchor" name="17.1"&gt;Last Insert ID With MySQL&lt;/a&gt;&lt;/h3&gt; &lt;p&gt; To fetch the last insert id with the mysql extension the mysql_insert_id() function is used. This snippet shows how. &lt;/p&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** connect to the database ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_connect&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check if the link is a valid resource ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;is_resource&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;    {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** select the database we wish to use ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_select_db&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"test"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) === &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;TRUE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;        {&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to INSERT a new record ***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO users (user_name, user_email, user_dob)&lt;br /&gt;                VALUES ( 'Kevin', 'kevin@example.com', '1995-08-22')"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** run the sql query ***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;            {&lt;br /&gt;                &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** assign the last insert id ***/&lt;br /&gt;                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$last_id &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_insert_id&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;( &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;                echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"New record created successfully with id of &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$last_id&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt; &lt;br /&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;            }&lt;br /&gt;            else&lt;br /&gt;            {&lt;br /&gt;                echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to INSERT data: &lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_error&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to select the database show an error ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;else&lt;br /&gt;        {&lt;br /&gt;            echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to select database'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close the connection ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysql_close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we fail to connect ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt;  &lt;h3&gt;&lt;a class="anchor" name="17.2"&gt;Last Insert ID With mysqli&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;The mysqli extension provides the mysqli::insert_id() method and the mysqli_insert_id() function. This offers the user two choices with the first used in object oriented coding and the second for procedural code. Both will return the last ID of generated by a table with an AUTO_INCREMENT field when an INSERT or UPDATE is performed, such as in the code that follows. &lt;/p&gt; &lt;h4&gt;Object Oriented&lt;/h4&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql database name ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'test'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** create a new mysqli object with default database***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/* check connection */&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_connect_errno&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;    {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are successful ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected Successfully&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to INSERT a new record ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO users (user_name, user_email, user_dob)&lt;br /&gt;                VALUES ( 'Kevin', 'kevin@example.com', '1995-08-22')"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;        if(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;) === &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;TRUE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;        {&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** assign the last insert id ***/&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$last_id &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;insert_id&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;            echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"New record created successfully With ID of &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$last_id&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;        else&lt;br /&gt;        {&lt;br /&gt;            echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;error&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close connection ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$mysqli&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** if we are unable to connect ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to connect'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        exit();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt;  &lt;h4&gt;Procedural&lt;/h4&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql database name ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'test'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** connection ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= @&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_connect&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** check for connection error ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_connect_errno&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;())&lt;br /&gt;    {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;printf&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"Connect failed: %s\n"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_connect_error&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;());&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** the sql inser ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO users (user_name, user_email, user_dob)&lt;br /&gt;               VALUES ( 'Kevin', 'kevin@example.com', '1995-08-22')"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** run the query ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;if(!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_query&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;))&lt;br /&gt;        {&lt;br /&gt;            echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Unable to add record'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;        else&lt;br /&gt;        {&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$last_id &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_insert_id&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;            echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"New record created successfully with id of &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$last_id&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** close connection ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;mysqli_close&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$link&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt;  &lt;h3&gt;&lt;a class="anchor" name="17.3"&gt;Last Insert ID With PDO&lt;/a&gt;&lt;/h3&gt; &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql hostname ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'localhost'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql username ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'username'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** mysql password ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** database name ***/&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'test'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;    try&lt;br /&gt;    {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** connection string ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"mysql:host=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$hostname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;;dbname=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbname&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$username&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$password&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo a message saying we have connected ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'Connected to database&lt;br /&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** set the PDO error mode to exception ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;setAttribute&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ATTR_ERRMODE&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDO&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;::&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;ERRMODE_EXCEPTION&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** sql to INSERT a new record ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"INSERT INTO users (user_name, user_email, user_dob)&lt;br /&gt;                VALUES ( 'Kevin', 'kevin@example.com', '1995-08-22')"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** we use PDO::exec because no results are returned ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;exec&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;);&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** show the last insert id ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$last_id &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$dbh&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;lastInsertId&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;        echo &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"New record created successfully With ID of &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$last_id&lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;    catch(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;PDOException $e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;)&lt;br /&gt;    {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;/*** echo the sql statement and error message ***/&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;echo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$sql &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(221, 0, 0);"&gt;'&lt;br /&gt;' &lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;$e&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;-&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;getMessage&lt;/span&gt;&lt;span style="color: rgb(0, 119, 0);"&gt;();&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 187);"&gt;?&gt;&lt;br /&gt;&lt;/span&gt; &lt;/span&gt; &lt;/code&gt;&lt;/div&gt; &lt;p&gt;  &lt;/p&gt;   &lt;h3&gt;&lt;a class="anchor" name="45"&gt;Useful Tips&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;By now you should have the gist of how to interact with MySQL using PHP. Here are a tips to help you get going when you are confronted with other situations &lt;/p&gt;  &lt;div class="codebox"&gt; &lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;/*** set the initial mysql password ***/&lt;br /&gt;mysqladmin -u root password 'my_password'&lt;br /&gt;&lt;br /&gt;/*** select yesterday ***/&lt;br /&gt;SELECT * FROM blah WHERE TO_DAYS(datetime_col) = ( TO_DAYS(NOW()) - 1 )&lt;br /&gt;&lt;br /&gt;/*** reset auto increment ***/&lt;br /&gt;ALTER TABLE tbl_name AUTO_INCREMENT = 1&lt;br /&gt;&lt;br /&gt;/*** start auto increment from 100 ***/&lt;br /&gt;ALTER TABLE tbl_name AUTO_INCREMENT = 100&lt;br /&gt;&lt;br /&gt;/*** select all records from today ***/&lt;br /&gt;SELECT * FROM table WHERE DATE_FORMAT(date_column, '%Y-%m-%d')=DATE_FORMAT(NOW(), '%Y-%m-%d')&lt;br /&gt;&lt;br /&gt;/*** select records from last 30 minutes ***/&lt;br /&gt;select DATE_SUB(NOW(),INTERVAL 30 MINUTE);&lt;br /&gt;&lt;br /&gt;/*** select records from last hour ***/&lt;br /&gt;select DATE_SUB(NOW(),INTERVAL 1 HOUR);&lt;br /&gt;&lt;br /&gt;/*** select records from last week ***/&lt;br /&gt;select DATE_SUB(NOW(),INTERVAL 1 WEEK);&lt;br /&gt;&lt;br /&gt;/*** get the last day of next month ***/&lt;br /&gt;SELECT LAST_DAY('2006-03-06' + INTERVAL 1 MONTH) AS pay_day;&lt;br /&gt;&lt;br /&gt;/*** select only records that appear once ***/&lt;br /&gt;SELECT foo FROM table GROUP BY foo HAVING ( COUNT(foo) = 1 )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;/*** select only records that appear more than once ***/&lt;br /&gt;SELECT foo, COUNT(foo) AS NumOccurrences FROM table GROUP BY foo HAVING ( COUNT(foo) &gt; 1 )&lt;br /&gt;&lt;br /&gt;/*** select count of records for each hour ***/&lt;br /&gt;SELECT HOUR(timestamp_col) AS hour , COUNT(*) AS count FROM your_table GROUP BY HOUR(timestamp_col)&lt;br /&gt;&lt;br /&gt;/*** rename a table ***&lt;br /&gt;"RENAME TABLE periodic_table.elements TO periodic_table.nice_elements";&lt;br /&gt;&lt;br /&gt;/*** import a csv file ***/&lt;br /&gt;LOAD DATA INFILE '/path/to/xxx.csv' INTO TABLE tablename FIELDS TERMINATED BY ',' LINES TERMINATED BY "\n" (col1,col2,col3)&lt;br /&gt;&lt;br /&gt;/*** select from table1 where records not in table2 ***/&lt;br /&gt;SELECT table1.some_id FROM table1 LEFT OUTER JOIN table2 ON table1.some_id=table2.some_id WHERE table2.some_id IS NULL&lt;br /&gt;&lt;br /&gt;/*** natural sorting for MySQL results ***/&lt;br /&gt;SELECT some_numbers FROM your_table ORDER BY some_numbers + 0 ASC&lt;br /&gt;&lt;br /&gt;/*** update text in field named description ***/&lt;br /&gt;UPDATE my_table set description=REPLACE(description, 'Old Text', 'New Text')&lt;br /&gt;&lt;/span&gt; &lt;/code&gt;&lt;/div&gt;   &lt;!--content ends --&gt;   &lt;div id="right"&gt; &lt;div id="download"&gt;   &lt;!-- &lt;a href=""&gt;&lt;img src="/images/white.jpg" alt="Download" name="Download" /&gt;&lt;/a&gt;&lt;br /&gt; --&gt; &lt;h4&gt;&lt;a href="http://www.phpro.org/rss/all.rss"&gt;&lt;img src="http://www.phpro.org/images/fam/rss_go.png" alt="rss" /&gt; RSS Feed&lt;/a&gt;&lt;/h4&gt;  &lt;h3&gt;Search&lt;/h3&gt; &lt;form method="get" action="http://www.phpro.org/search" target="_top"&gt; &lt;table bgcolor="#ffffff" border="0"&gt; &lt;tbody&gt;&lt;tr&gt;&lt;td valign="top" align="left" height="32" nowrap="nowrap"&gt; &lt;br /&gt;&lt;/td&gt; &lt;td nowrap="nowrap"&gt; &lt;input name="domains" value="www.phpro.org" type="hidden"&gt; &lt;label for="sbi" style="display: none;"&gt;Enter your search terms&lt;/label&gt; &lt;input name="q" size="31" maxlength="255" value="" id="sbi" type="text"&gt; &lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;/td&gt; &lt;td nowrap="nowrap"&gt; 	&lt;table&gt; 	&lt;tbody&gt;&lt;tr&gt; 	&lt;td&gt; 	&lt;input name="sitesearch" value="" id="ss0" type="radio"&gt; 	&lt;label for="ss0" title="Search the Web"&gt;&lt;span style="font-size:-1;color:#000000;"&gt;Web&lt;/span&gt;&lt;/label&gt;&lt;/td&gt; 	&lt;td&gt; 	&lt;input name="sitesearch" value="www.phpro.org" checked="checked" id="ss1" type="radio"&gt; 	&lt;label for="ss1" title="Search www.phpro.org"&gt;&lt;span style="font-size:-1;color:#000000;"&gt;www.phpro.org&lt;/span&gt;&lt;/label&gt;&lt;/td&gt; 	&lt;/tr&gt; 	&lt;/tbody&gt;&lt;/table&gt; &lt;label for="sbb" style="display: none;"&gt;Submit search form&lt;/label&gt; &lt;input name="sa" value="Google Search" id="sbb" type="submit"&gt; &lt;input name="client" value="pub-1399602330070151" type="hidden"&gt; &lt;input name="forid" value="1" type="hidden"&gt; &lt;input name="ie" value="ISO-8859-1" type="hidden"&gt; &lt;input name="oe" value="ISO-8859-1" type="hidden"&gt; &lt;input name="safe" value="active" type="hidden"&gt; &lt;input name="cof" value="GALT:#E1771E;GL:1;DIV:#999999;VLC:336633;AH:center;BGC:FFFFFF;LBGC:FF9900;ALC:E1771E;LC:E1771E;T:000000;GFNT:666666;GIMP:666666;FORID:11" type="hidden"&gt; &lt;input name="hl" value="en" type="hidden"&gt; &lt;/td&gt;&lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;/form&gt;  &lt;h3&gt;PHPRO.ORG Poll&lt;/h3&gt; &lt;!-- the poll form --&gt;&lt;br /&gt;&lt;div id="poll"&gt; &lt;form action="/poll" id="poll_form" method="post"&gt; &lt;h3&gt;Who do you rate lowest?&lt;/h3&gt;&lt;input name="poll_response_id" onclick="this.form.submit()" value="13" type="radio"&gt; RIAA&lt;br /&gt;&lt;input name="poll_response_id" onclick="this.form.submit()" value="14" type="radio"&gt; Microsoft&lt;br /&gt;&lt;input name="poll_response_id" onclick="this.form.submit()" value="15" type="radio"&gt; Lawyers&lt;br /&gt;&lt;input name="poll_response_id" onclick="this.form.submit()" value="16" type="radio"&gt; Swedes&lt;br /&gt;&lt;input name="poll_response_id" onclick="this.form.submit()" value="17" type="radio"&gt; Prostitutes&lt;br /&gt;&lt;/form&gt; &lt;/div&gt; &lt;!-- end poll form --&gt; &lt;p&gt; &lt;b&gt;Warning:&lt;/b&gt; Participation in PHPRO.ORG polls may incorrectly lead you to believe your opinions matter. &lt;/p&gt;  &lt;!-- &lt;ul&gt; &lt;li&gt;&lt;a href=""&gt;Link&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href=""&gt;Link&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href=""&gt;Link&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href=""&gt;Link&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; --&gt; &lt;/div&gt; &lt;/div&gt;   &lt;!--content ends --&gt;  &lt;!--footer starts --&gt;   &lt;div class="blogger-post-footer"&gt;http://w3cworld.blogspot.com/feeds/posts/

http://w3cworld.blogspot.com/feeds/comment/&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2359453265019837844-8870924309888200428?l=w3cworld.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://w3cworld.blogspot.com/feeds/8870924309888200428/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://w3cworld.blogspot.com/2009/06/mysql-and-php.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2359453265019837844/posts/default/8870924309888200428'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2359453265019837844/posts/default/8870924309888200428'/><link rel='alternate' type='text/html' href='http://w3cworld.blogspot.com/2009/06/mysql-and-php.html' title='MYSQL AND PHP'/><author><name>Imran Malik</name><uri>http://www.blogger.com/profile/13742779381556383378</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='http://3.bp.blogspot.com/_bYJnR7vm4kE/TJtq9K9FMdI/AAAAAAAABmk/EtUpHKcAerw/S220/ASDF+(128).jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2359453265019837844.post-4811768168755393000</id><published>2009-06-01T09:00:00.000-07:00</published><updated>2009-06-01T09:07:04.274-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C AND C++'/><title type='text'>Phil's C Course</title><content type='html'>&lt;h2&gt;&lt;a name="Aims"&gt;Aims of this Course&lt;/a&gt;&lt;/h2&gt;  This course is intended to help a good programmer (pause for mass exodus), particularly someone familiar with DEC &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt;, start programming in &lt;span style="color:Blue;"&gt;C&lt;/span&gt;, but should actually provide a reasonable introduction to &lt;span style="color:Blue;"&gt;C&lt;/span&gt; for people who have used any programming language before. There are a number of program examples, copies of which you can download as &lt;a href="http://www.pottsoft.com/home/c_course/phils_c_examples.zip"&gt; phils_c_examples.zip&lt;/a&gt;. A detached &lt;a href="http://www.pottsoft.com/home/c_course/phils_c_examples.sig"&gt;PGP signature file&lt;/a&gt; (which it is not necessary to download unless you know what it's for) is provided so you can be sure the archive has not been altered. When you unzip the archive on a Windows machine, note the workspace file &lt;code&gt;phils_c_examples.dsw&lt;/code&gt;. You should open this with Microsoft Visual Studio 98 and Microsoft Visual C++ 6.0, then "batch build" everything. VMS users should use &lt;code&gt;unzip -a phils_c_examples.zip&lt;/code&gt; to get the correct file attributes for the text files, like the &lt;code&gt;.c&lt;/code&gt; source files and &lt;code&gt;MAKE.COM&lt;/code&gt;, the VMS command file which you can use to &lt;a href="http://www.pottsoft.com/home/c_course/course.html#Using_MAKE.COM"&gt;build the programs&lt;/a&gt;.   &lt;p&gt;  There are also several &lt;em&gt;programming challenges&lt;/em&gt;. Have a go at these, nicking as much code as you can from the examples ! Using &lt;span style="color:Blue;"&gt;C&lt;/span&gt; is the best way to learn it, and making mistakes is definitely the best way to find out how it really works. I mention the ANSI &lt;span style="color:Blue;"&gt;C&lt;/span&gt; standard, ANSI/ISO 9899-1990, a lot in this document. Always try to adhere to the standard; experience has shown that it pays off in the long term. Some of the points I make are stylistic. However, many of these suggestions are made for one of two reasons; either the majority of the &lt;span style="color:Blue;"&gt;C&lt;/span&gt; programming world has reached consensus that the style is good (which will make it easier for you to read and learn from other peoples' code) or I have found that you can avoid errors by doing things in a particular way. I reckon that you can "learn" &lt;span style="color:Blue;"&gt;C&lt;/span&gt; in about an hour, then spend the next year wishing you hadn't done things in a particular way the hour after that. This course should help you avoid some of the pitfalls that are so easy to fall into (and, in fact, dig for yourself) because of the total control, power, and 0 to 60 ACCVIOS in under 10 seconds that &lt;span style="color:Blue;"&gt;C&lt;/span&gt; can deliver to the programmer.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;h2&gt;&lt;a name="History"&gt;Why is the language called C ?&lt;/a&gt;&lt;/h2&gt;   &lt;span style="color:Blue;"&gt;C&lt;/span&gt; was developed under Unix on the PDP-11 in 1972 by Dennis Ritchie, building on the language B, written by Ken Thompson in 1970 on a PDP-7, also running Unix. B was in turn, based on BCPL which was developed in 1967 by Martin Richards (and which is still available for the BBC Micro).  &lt;p&gt;  BCPL and B were typeless languages - variables were all multiples of byte or word sized bits of memory. &lt;span style="color:Blue;"&gt;C&lt;/span&gt; is more strongly typed than B, BCPL or &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt;. Its basic types are &lt;code&gt;char&lt;/code&gt;, &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;float&lt;/code&gt; and &lt;code&gt;double&lt;/code&gt;, which are characters, integers and single and double precision floating point numbers. An important addition, compared to &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt;, is the pointer type, which points to the other types (including other pointers). All these types can be combined in structures or unions to provide composite types.  &lt;/p&gt; &lt;p&gt;  The main shock to &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt; programmers is the fact that &lt;span style="color:Blue;"&gt;C&lt;/span&gt; has no built-in string type, and consequently you have to make a function call to compare two strings, or assign one string to another. Luckily, the ANSI standard describes a set of string manipulation routines that MUST be present if an implementation is described as ANSI &lt;span style="color:Blue;"&gt;C&lt;/span&gt;. Similarly, a good set of standard IO, time manipulation and even sorting routines exist. HELP CC RUN-TIME_FUNCTIONS will give you information on all of these, and even tell you which header files you should include to use them. For example HELP CC RUN PRINTF will inform you that you need the header file &lt;code&gt;stdio.h&lt;/code&gt; .  &lt;/p&gt; &lt;p&gt;  In the early days of &lt;span style="color:Blue;"&gt;C&lt;/span&gt;, different compiler vendors all had their own flavours of &lt;span style="color:Blue;"&gt;C&lt;/span&gt;, usually based on the book, &lt;a href="http://www.pottsoft.com/home/c_course/course.html#Bibliography"&gt;The C Programming Language&lt;/a&gt;, by Brian Kernighan and Dennis Ritchie. These older compilers are often referred to as "Classic C" or "K&amp;amp;R C". As C gained in popularity, the need to standardize certain features became apparent, and in 1983 the American National Standards Institute established the X3J11 technical committee, which published the ANSI &lt;span style="color:Blue;"&gt;C&lt;/span&gt; standard in 1988.  &lt;/p&gt; &lt;p&gt;  If you only buy one book on &lt;span style="color:Blue;"&gt;C&lt;/span&gt;, get the second edition of the &lt;a href="http://www.pottsoft.com/home/c_course/course.html#Bibliography"&gt;K&amp;amp;R book&lt;/a&gt;. If you want to buy two books add &lt;a href="http://www.pottsoft.com/home/c_course/course.html#Bibliography"&gt;Expert C Programming: Deep C Secrets&lt;/a&gt; by Peter van der Linden. If you really want to be a language lawyer and contribute to threads like "Is i = i++ + --i legal ?" in the comp.lang.c newsgroup, then get "The Annotated ANSI C Standard", annotated by Herbert Schildt. Personally I think a line of code like &lt;code&gt;i = i++ + --i&lt;/code&gt; should be taken out and shot.  &lt;/p&gt; &lt;p&gt;  The DEC &lt;span style="color:Blue;"&gt;C&lt;/span&gt; compiler is a good ANSI compiler, and any code you write should pass through this compiler (with its default qualifiers) without so much as an informational murmer. If it doesn't you are storing up big trouble and intermittent bugs for the future. Even if you decide to do nonstandard things, there are techniques to do them in a standard way (!), which will be explained later.  &lt;/p&gt; &lt;p&gt; &lt;a name="Hello_World"&gt; OK, enough waffle. Let's look at a "Hello World" program in &lt;/a&gt; &lt;span style="color:Blue;"&gt;C&lt;/span&gt;. &lt;/p&gt; &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;/*---- Hello World C Example ("hello.c") -------------------------------------*/&lt;br /&gt;&lt;br /&gt;/* ANSI C Headers */&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;stdlib.h&gt;&lt;br /&gt;&lt;br /&gt;/* Main Program starts here */&lt;br /&gt;int main( int argc, char *argv[] )&lt;br /&gt;{&lt;br /&gt;   int i = 0;&lt;br /&gt;/*  End of declarations ... */&lt;br /&gt;&lt;br /&gt;   for ( i = 0; i &lt; 10; i++ )&lt;br /&gt;   {&lt;br /&gt;     printf("%d Hello World !\n",i);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   exit(EXIT_SUCCESS);&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; As you have probably gathered, comments in &lt;span style="color:Blue;"&gt;C&lt;/span&gt; are delimited by &lt;code&gt;/*&lt;/code&gt; and &lt;code&gt;*/&lt;/code&gt;, and comments must NOT be nested, or you will get some very interesting bugs. The perceived need for nested comments is usually for commenting out (say) a debug piece of code, and this can be done in a better way, which will be explained later. Some &lt;span style="color:Blue;"&gt;C&lt;/span&gt; compilers let you use the trailing  &lt;span style="color:DarkBlue;"&gt;C++&lt;/span&gt; style comments &lt;span style="color:DarkBlue;"&gt;//&lt;/span&gt;, which are a like a trailing ! in DEC &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt;. NEVER USE THESE IN &lt;span style="color:Blue;"&gt;C&lt;/span&gt; PROGRAMS. It is not ANSI standard, and immediately confuses people as to whether they are looking a &lt;span style="color:Blue;"&gt;C&lt;/span&gt; or &lt;span style="color:DarkBlue;"&gt;C++&lt;/span&gt; code (and some meanings can subtly change).  &lt;/p&gt; &lt;p&gt;  To compile this program under DEC &lt;span style="color:Blue;"&gt;C&lt;/span&gt;  (both &lt;a href="http://www.pottsoft.com/home/vms/vms.html#alpha"&gt;Alpha&lt;/a&gt;'s and &lt;a href="http://www.pottsoft.com/home/vms/vms.html#vax"&gt;VAX&lt;/a&gt; should be using DEC &lt;span style="color:Blue;"&gt;C&lt;/span&gt; now. VAX &lt;span style="color:Blue;"&gt;C&lt;/span&gt; was retired around 1993, and you really should switch to DEC &lt;span style="color:Blue;"&gt;C&lt;/span&gt; for both platforms now)  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;$ CC HELLO&lt;br /&gt;$ LINK HELLO&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; &lt;a name="Using_MAKE.COM"&gt;Alternatively, you can use the &lt;code&gt;MAKE.COM&lt;/code&gt;&lt;/a&gt; DCL command file, as shown below. On Alphas the resulting executable will have file type &lt;code&gt;.EXE_ALPHA&lt;/code&gt;, and on VAX machines it will be &lt;code&gt;.EXE&lt;/code&gt;. &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;$ @MAKE HELLO&lt;br /&gt; DEV$DISK:[PHIL.PHILS_C_EXAMPLES]&lt;br /&gt;CC/PREFIX=ALL HELLO.C  -&gt; HELLO.OBJ_ALPHA&lt;br /&gt;LINK HELLO  -&gt; HELLO.EXE_ALPHA&lt;br /&gt;Exiting&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt;  If you must use VAX &lt;span style="color:Blue;"&gt;C&lt;/span&gt; (and you mustn't :-) the link step will whinge about unresolved symbols, so change the line to &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;$ LINK HELLO, VAXCRTL/OPT&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; where the &lt;code&gt;VAXCRTL.OPT&lt;/code&gt; options file contains the line &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;SYS$SHARE:VAXCRTL/SHARE&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt;  You are now ready to RUN HELLO . Not too many surprises there. Note the form of the code. The main entry point in a standard &lt;span style="color:Blue;"&gt;C&lt;/span&gt; is always called &lt;code&gt;main&lt;/code&gt;, though you can override this on &lt;a href="http://www.pottsoft.com/home/vms/vms.html"&gt;VMS&lt;/a&gt; platforms, as we will &lt;a href="http://www.pottsoft.com/home/c_course/course.html#MAIN_PROGRAM"&gt;discover&lt;/a&gt;. The main program in &lt;span style="color:Blue;"&gt;C&lt;/span&gt; is declared as &lt;code&gt;int main(some funny stuff)&lt;/code&gt;. This is because the main program should always return a value (usually to DCL or the Unix shell) indicating how things went. This is done by the call to &lt;code&gt;exit(EXIT_SUCCESS)&lt;/code&gt;. There are two ANSI standard return codes, EXIT_SUCCESS and EXIT_FAILURE, both defined in &lt;code&gt;&lt;stdlib.h&gt;&lt;/code&gt; . Always use these values, and don't do what a lot of Unix programmers do which is &lt;code&gt;exit(0)&lt;/code&gt; or some other magic number just because "everybody knows that &lt;code&gt;exit(0)&lt;/code&gt; means success". You can return VMS condition codes, e.g. &lt;code&gt;exit(SS$_NORMAL)&lt;/code&gt;, but this should be avoided unless really necessary, and even then there are ways to fall back to the standard return codes if your code is compiled on a non-VMS machine.  &lt;/p&gt; &lt;p&gt;  The &lt;code&gt;(some funny stuff)&lt;/code&gt; is the argument list, or the "formal parameters" of function &lt;code&gt;main&lt;/code&gt;. Imagine &lt;code&gt;main&lt;/code&gt; as a function called from your command shell (DCL on VMS, or the DOS window on  Windows NT). The declaration &lt;code&gt;int main( int argc, char *argv[] )&lt;/code&gt; means that main is a function returning an integer, which takes two arguments. The first is an integer, and is the number of arguments passed to main by DCL, and the second is a pointer to arrays of characters. The latter are, in fact, any command line arguments, as will be demonstrated in &lt;code&gt;args.c&lt;/code&gt;, a demo programming coming soon to a disk near you. The body of a function is delimited by &lt;code&gt;{&lt;/code&gt; and &lt;code&gt;}&lt;/code&gt;. Because &lt;span style="color:Blue;"&gt;C&lt;/span&gt; is largely a free format language, the whole function can be on one line if you really want, but that tends to be unreadable and confusing. I like to start the function with a &lt;code&gt;{&lt;/code&gt; in column one, just after the function declaration (which I can then nick for prototyping), and end the function with a &lt;code&gt;}&lt;/code&gt; in the same column.  &lt;/p&gt; &lt;p&gt;  Notice how each statement ends with a semicolon. The ";" is known as a statement terminator. It is also a "sequence point", as are the comma operator, and various other logical comparison operators, and the standard guarantees that side effects of expressions will be over once a sequence point is reached. This basically means that all the things you made happen in one statement will have happened by the time you start on the next statement or expression.  &lt;/p&gt; &lt;p&gt;  The &lt;code&gt;printf(...)&lt;/code&gt; statement is a call to a routine defined in &lt;code&gt;&lt;stdio.h&gt;&lt;/code&gt;, and enables formatted output to the &lt;code&gt;stdout&lt;/code&gt; stream. In &lt;span style="color:Blue;"&gt;C&lt;/span&gt;, three default output streams are defined. These are stdin, stdout and stderr, and they correspond to &lt;code&gt;SYS$INPUT&lt;/code&gt;, &lt;code&gt;SYS$OUTPUT&lt;/code&gt; and &lt;code&gt;SYS$ERROR&lt;/code&gt; under VMS. The first argument is a format string containing conversions characters, each preceded by the &lt;code&gt;%&lt;/code&gt; sign (use &lt;code&gt;%%&lt;/code&gt; if you actually want a &lt;code&gt;%&lt;/code&gt; sign), which tell the routine how to interpret the variable number of arguments to be printed. In this case the integer &lt;code&gt;i&lt;/code&gt; is to be printed in decimal, so &lt;code&gt;"%d"&lt;/code&gt; is used. There are corresponding functions, &lt;code&gt;sprintf&lt;/code&gt; to write directly into a character string array, and &lt;code&gt;fprintf&lt;/code&gt; to write to a file. Similar formatted input routines, &lt;code&gt;sscanf&lt;/code&gt; and &lt;code&gt;fscanf&lt;/code&gt; are also available. The table below, nicked off the network, summarizes the conversion characters:   &lt;/p&gt;&lt;pre&gt;&lt;b&gt;&lt;br /&gt;                   Clive Feather's Excellent Table:&lt;br /&gt;&lt;br /&gt;    Types of arguments for the various fprintf and fscanf conversions&lt;br /&gt;    &lt;br /&gt;        Conversion        fprintf          fscanf&lt;br /&gt;        ----------------------------------------------------&lt;br /&gt;        d  i              int              int *&lt;br /&gt;        o  u  x  X        unsigned int     unsigned int *&lt;br /&gt;        hd hi             int              short *&lt;br /&gt;        ho hu hx hX       [see note 1]     unsigned short *&lt;br /&gt;        ld li             long             long *&lt;br /&gt;        lo lu lx lX       unsigned long    unsigned long *&lt;br /&gt;        e  E  f  g  G     double           float *&lt;br /&gt;        le lE lf lg lG    [invalid]        double *&lt;br /&gt;        Le LE Lf Lg LG    long double      long double *&lt;br /&gt;        c                 int              [see note 2]&lt;br /&gt;        s                 [see note 2]     [see note 2]&lt;br /&gt;        p                 void *           void **&lt;br /&gt;        n                 int *            int *&lt;br /&gt;        hn                short *          short *&lt;br /&gt;        ln                long *           long *&lt;br /&gt;        [                 [invalid]        [see note 2]&lt;br /&gt;&lt;br /&gt;    Note 1: the type that (unsigned short) is promoted to by the integral&lt;br /&gt;            promotions. This is (int) if USHORT_MAX &lt;= INT_MAX, and&lt;br /&gt;            (unsigned int) otherwise.&lt;br /&gt;    Note 2: any of (char *), (signed char *), or (unsigned char *).&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt; Don't worry about the "*"s for now. They can be read as "pointer to thing named before them", so &lt;code&gt;int *&lt;/code&gt; means pointer to int. Similar summaries can be found in K &amp;amp;R II pages 154 and 158.  &lt;/p&gt;  &lt;cite&gt; &lt;/cite&gt;&lt;pre&gt;      Programming Challenge 1&lt;br /&gt;     _______________________&lt;br /&gt;    &lt;br /&gt;       Have a  go at adapting "hello.c"  to print out the value of &lt;code&gt;i&lt;/code&gt; in&lt;br /&gt;     hexadecimal.  Fiddle about with the format string -  remove the &lt;code&gt;"\n"&lt;/code&gt;&lt;br /&gt;     for example, and see what happens to your output.&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt;  Unlike &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt;, whitespace is significant in C, and there are reserved keywords. These reserved keywords should not appear as any type of identifier, even a structure member. The list below shows both &lt;span style="color:Blue;"&gt;C&lt;/span&gt; and &lt;span style="color:DarkBlue;"&gt;C++&lt;/span&gt; reserved keywords.  &lt;/p&gt;  &lt;pre&gt;&lt;b&gt;&lt;br /&gt;   asm&lt;sup&gt;1 &lt;/sup&gt;        continue    float       new&lt;sup&gt;1 &lt;/sup&gt;        signed      try&lt;sup&gt;1 &lt;/sup&gt;&lt;br /&gt;   auto        default     for         operator&lt;sup&gt;1 &lt;/sup&gt;   sizeof      typedef&lt;br /&gt;   break       delete&lt;sup&gt;1 &lt;/sup&gt;     friend&lt;sup&gt;1 &lt;/sup&gt;     private&lt;sup&gt;1 &lt;/sup&gt;    static      union&lt;br /&gt;   case        do          goto        protected&lt;sup&gt;1 &lt;/sup&gt;  struct      unsigned&lt;br /&gt;   catch&lt;sup&gt;1 &lt;/sup&gt;      double      if          public&lt;sup&gt;1 &lt;/sup&gt;     switch      virtual&lt;sup&gt;1 &lt;/sup&gt;&lt;br /&gt;   char        else        inline&lt;sup&gt;1 &lt;/sup&gt;     register    template&lt;sup&gt;1 &lt;/sup&gt;   void&lt;br /&gt;   class&lt;sup&gt;1 &lt;/sup&gt;      enum        int         return      this&lt;sup&gt;1 &lt;/sup&gt;       volatile&lt;br /&gt;   const       extern      long        short       throw&lt;sup&gt;1 &lt;/sup&gt;      while&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;  The items marked like this&lt;sup&gt;1 &lt;/sup&gt; are &lt;span style="color:DarkBlue;"&gt;C++&lt;/span&gt;, not &lt;span style="color:Blue;"&gt;C&lt;/span&gt; keywords, but it makes sense to avoid both. Avoid using a language name like &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt; too.  &lt;/p&gt;  &lt;h2&gt;&lt;a name="Variables"&gt;§2 Variables, Types and Functions&lt;/a&gt;&lt;/h2&gt;   Variables are like little boxes with numbers on them, a bit like houses, and inside the boxes ... naaahh ! Just kidding. You all know what variables are, and I think we all understand that &lt;code&gt;x = x + 1&lt;/code&gt; isn't a contradictory algebraic statement.   &lt;p&gt;  &lt;span style="color:Blue;"&gt;C&lt;/span&gt;, unlike &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt;, has case sensitive variable and other identifier names. Therefore the variable &lt;code&gt;NextPage&lt;/code&gt; is completely different to &lt;code&gt;nextpage&lt;/code&gt;. The same is true for functions. Some people like to use the capitalized-first-letter form of naming, others prefer underbars, e.g. GetNextPage() or get_next_page() . Many professional library packages tend towards TheCapitalizedFormat. Some people like Microsoft's &lt;em&gt;Hungarian Notation&lt;/em&gt; which involves prefixing variable names with their type, e.g. &lt;code&gt;uiCount&lt;/code&gt; for an &lt;code&gt;unsigned int&lt;/code&gt; counter variable . It all depends how good you are with the Shift key :-) Whatever method you choose, try and be clear and consistent.  &lt;/p&gt; &lt;p&gt;  In &lt;span style="color:Blue;"&gt;C&lt;/span&gt;, local variable definitions can be at the start of any {block}, and aren't restricted to the top of the module as in  &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt;. Be careful if you take advantage of this feature, because you may run into scoping problems where the innermost variable definition hides an outer one. If you are used to &lt;span style="color:DarkBlue;"&gt;C++&lt;/span&gt;, remember that the variable definitions can only be at the &lt;em&gt;start of the {block}&lt;/em&gt; before the first statement (e.g. expression, function call or flow control statement). If you try and intersperse definitions, &lt;span style="color:DarkBlue;"&gt;C++&lt;/span&gt; style, the &lt;span style="color:Blue;"&gt;C&lt;/span&gt; compiler will issue some sort of "bad statement" warning.  &lt;/p&gt; &lt;p&gt;  Variables declared at the beginning of the {function body} are local to the function, variables declared at the "top" of the file (or compilation unit to be pedantic), in the header files, or outside any function bodies, are  global to the compilation unit (and are externally visible symbols, unless declared as &lt;code&gt;static&lt;/code&gt;). More will be said about this later. For now, suffice  it to say that you should avoid using global variables wherever possible. &lt;/p&gt; &lt;p&gt; A brief example will illustrate the scope of variables: &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;/*---- Variable Scope Example ( "scope.c" ) ----------------------------------*/&lt;br /&gt;/* ANSI C Headers */&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;stdlib.h&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:Red;"&gt;/* Global variables, visible externally too (i.e. to things linked  */&lt;br /&gt;/* against this) Generally they should be avoided as far as */&lt;br /&gt;/* possible, because it can be very difficult to discover which */&lt;br /&gt;/* routine changes their value, and they introduce "hidden" dependencies */&lt;br /&gt;int some_counter;&lt;br /&gt;double double_result;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;/* Function prototypes */&lt;br /&gt;void set_double_result(void);&lt;br /&gt;&lt;br /&gt;/* Main Program starts here */&lt;br /&gt;int main( int argc, char *argv[] )&lt;br /&gt;{&lt;br /&gt;   int j;&lt;br /&gt;   int i_am_local; /* .. to main */&lt;br /&gt;/*  End of declarations ... */&lt;br /&gt;&lt;br /&gt;   i_am_local = 1;&lt;br /&gt;   printf("i_am_local = %d (in main)\n\n", i_am_local );&lt;br /&gt;  &lt;br /&gt;   for ( j = 0; j &lt; 10; j++ ) {&lt;br /&gt;     &lt;span style="color:Red;"&gt;int i_am_local; /* .. to this loop - Not necessarily a good idea */&lt;br /&gt;                     /* because it can cause confusion as to which */&lt;br /&gt;                     /* variable we actually want to access */&lt;/span&gt;&lt;br /&gt;     i_am_local = j;&lt;br /&gt;     printf("i_am_local = %d (inside loop)\n", i_am_local );&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   printf("\ni_am_local = %d (in main)\n\n", i_am_local );&lt;br /&gt;&lt;br /&gt;   /* Now let's look at the default initialization values of the globals */&lt;br /&gt;   printf("nsome_counter = %d (in main)\n", some_counter);&lt;br /&gt;   printf("double_result = %f (in main)\n\n", double_result);&lt;br /&gt;&lt;br /&gt;   /* Call a function that changes the global variables .. */&lt;br /&gt;   set_double_result();&lt;br /&gt;&lt;br /&gt;   /* .. and look at them again */&lt;br /&gt;   printf("some_counter = %d (in main)\n", some_counter);&lt;br /&gt;   printf("double_result = %f (in main)\n", double_result);&lt;br /&gt;&lt;br /&gt;   exit(EXIT_SUCCESS);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void set_double_result(void)&lt;br /&gt;{&lt;br /&gt;   ++some_counter;&lt;br /&gt;   double_result = 3.141;&lt;br /&gt;   printf("some_counter = %d (in set_double_result)\n", some_counter);&lt;br /&gt;   printf("double_result = %f (in set_double_result)\n\n", double_result);&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; The basic types in &lt;span style="color:Blue;"&gt;C&lt;/span&gt; are: &lt;/p&gt;  &lt;pre&gt;&lt;b&gt;&lt;br /&gt;    char - this defines a byte, which must be able to hold one character&lt;br /&gt;           in the local character set (normally, but not necessarily 8 bits);&lt;br /&gt;     int - holds an integer, usually in the machine's natural size.&lt;br /&gt;           They are 32 bits on both VAX and Alpha.&lt;br /&gt;   float - holds a single precision floating point number.&lt;br /&gt;           They are 32 bits on both VAX and Alpha.&lt;br /&gt;  double - Double precision floating point number, 64 bits on the VAX&lt;br /&gt;           and Alpha.&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt; These bit sizes are just to give you an idea. They should not be relied on, and you should code independently of them, unless you are addressing hardware registers or some equally hardware-specific task. &lt;/p&gt; &lt;p&gt; Some of these basic types can be modified with various qualifiers: &lt;/p&gt;  &lt;pre&gt;&lt;b&gt;&lt;br /&gt;    char - can be signed or unsigned;&lt;br /&gt;     int - can be long or short, signed or unsigned;&lt;br /&gt;  double - can be long, for (possibly) even more precision;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt; The &lt;code&gt;long&lt;/code&gt; modifier normally gives larger integers, but the compiler vendor is free to ignore it provided that &lt;/p&gt;  &lt;pre&gt;&lt;b&gt;&lt;br /&gt;            short &lt;= int &lt;= long&lt;br /&gt;            16 bits &lt;= short/int&lt;br /&gt;            32 bits &lt;= long&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;  Assignment to variables of these basic types is fairly intuitive, and can be done in the definition, rather like using the DATA statement in &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt; or the DEC &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt; extension &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;/*  C Example */                  &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;*     DEC  Fortran Example&lt;/span&gt;&lt;br /&gt;                                 &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;br /&gt;   int x, y; /* Not initialized*/&lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      INTEGER     X, Y&lt;/span&gt;&lt;br /&gt;   int counter = 0;              &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      INTEGER     COUNTER /0/&lt;/span&gt;&lt;br /&gt;   float total = 0.0;            &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      REAL        TOTAL   /0.0/&lt;/span&gt;&lt;br /&gt;   char c = 'A';                 &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      CHARACTER*1 C       /'A'/&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt;  Note that &lt;span style="color:Blue;"&gt;C&lt;/span&gt; uses single quote ' for character constants. The double quotes are used for strings. There are &lt;em&gt;escape sequences&lt;/em&gt; for getting nonprintable characters. These are listed on page 38 of K&amp;amp;R II. A few useful ones are '\n' to get a new line (&lt;span style="color:Blue;"&gt;C&lt;/span&gt; doesn't automatically add line feeds when you use printf() ), '\a' to get a bell (alert) sound, and '\0' to get the null character (which is NOT THE SAME as the &lt;code&gt;NULL&lt;/code&gt; pointer) used to terminate strings (arrays of characters). The initialization of non-static (discussed later) int and float variables is necessary before use. It doesn't have to be done in the definition, but you can't rely on their value being anything sensible, so whilst the initialization of COUNTER and TOTAL is redundant in the &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt; example (assuming non-recursive compilation), you do need to initialize the variables before use in &lt;span style="color:Blue;"&gt;C&lt;/span&gt;. It is good practise to initialize variables when they are defined. The compiler will probably optimize this away, but if you initialize at definition, it has the advantage of causing consistent behaviour  if the program later fails because you forget to set the value before it is used, which makes debugging easier. &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;     .&lt;br /&gt;{&lt;br /&gt;   int i; /* Avoid declaring variables without an initial value */&lt;br /&gt;   int j; /* like these are - this is just for illustration */&lt;br /&gt;   char *pString;&lt;br /&gt;     .&lt;br /&gt;   i = 0; /* i's value could be anything up to this point */&lt;br /&gt;     .&lt;br /&gt;   j = i*OFFSET; /* j's value could be anything up to this point */&lt;br /&gt;     .&lt;br /&gt;   /* This will probably fail but may not do so every time you run */&lt;br /&gt;   /* the program, because the pointer might point to a writeable */&lt;br /&gt;   /* location. Bugs like this are harder to find because they tend */&lt;br /&gt;   /* not cause a consistently reproducible crash */&lt;br /&gt;   *pString = "Hello World"; /* Could overwrite anything */&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void MyFunction(void)&lt;br /&gt;{&lt;br /&gt;   int i = 0; /* Best to set an initial value */&lt;br /&gt;   int j = 0;&lt;br /&gt;   char *pString = NULL;&lt;br /&gt;     .&lt;br /&gt;   *pString = "Hello"; /* This will cause a run time error every time */&lt;br /&gt;                       /* but it will be easy to detect the problem */&lt;br /&gt;     .&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; Global variables are guaranteed to be initialized to 0 (or 0.0 if floating type) but you can override this by specifying an initial value. &lt;/p&gt; &lt;p&gt;  Similar rules apply to float, double, and long double. There are two standard header files, &lt;code&gt;&lt;limits.h&gt;&lt;/code&gt; and &lt;code&gt;&lt;float.h&gt;&lt;/code&gt; which tell you the maximum and minimum values that can be stored in a particular type; for example &lt;code&gt;INT_MAX&lt;/code&gt; is 2147483647, and &lt;code&gt;FLT_MAX&lt;/code&gt; is 1.7014117e+38 on the VAX. &lt;/p&gt; &lt;p&gt; The signed or unsigned modifiers are fairly self explanatory. The default for int is signed, so it is rarely specified. Signed integer arithmetic is usually done in &lt;em&gt;Two's Complement&lt;/em&gt; form, but this need not be the case. Characters can be signed or unsigned by default - it is implementation defined. I find it best just to use &lt;code&gt;char&lt;/code&gt; with no qualifiers, and let the compiler do what it will.  &lt;/p&gt; &lt;p&gt;  This is probably a good point to introduce the &lt;code&gt;sizeof(thing)&lt;/code&gt; operator. It is an operator, not a function, and is evaluated at compile time. It returns the size of the argument, where the size of &lt;code&gt;char&lt;/code&gt; is defined to be 1. To be pedantic, it returns an unsigned integer type, &lt;code&gt;size_t&lt;/code&gt;, defined in &lt;code&gt;&lt;stddef.h&gt;&lt;/code&gt;, but is not often used in a way that requires a &lt;code&gt;size_t&lt;/code&gt; declaration. Here are some examples of its use (this is a "programming fragment" not a complete program).  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   size_t s;&lt;br /&gt;   int fred;        /* Integer */&lt;br /&gt;   char bob;        /* Character */&lt;br /&gt;   char *c_ptr;     /* Pointer to character */&lt;br /&gt;   char bloggs[6];  /* Array of 6 characters */&lt;br /&gt;        .&lt;br /&gt;   s = sizeof( fred );&lt;br /&gt;   s = sizeof( bob );&lt;br /&gt;   s = sizeof( c_ptr );&lt;br /&gt;   s = sizeof( long double ); /* Allowed to use types instead of variables */&lt;br /&gt;        .&lt;br /&gt;/*  Safe string copy, checks size of destination and allows for terminating */&lt;br /&gt;/*  null character (not to be confused with the NULL pointer discussed later) */&lt;br /&gt;   strncpy( bloggs, "Bloggs", sizeof(bloggs)-1 );&lt;br /&gt;        .&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; You can leave the brackets off after sizeof, e.g. &lt;code&gt;sizeof int&lt;/code&gt; is quite legal, but I think that the bracketed form is clearer. &lt;/p&gt;  &lt;cite&gt; &lt;/cite&gt;&lt;pre&gt;      Programming Challenge 2&lt;br /&gt;     _______________________&lt;br /&gt;    &lt;br /&gt;       Have a  go at adapting "hello.c"  to print  out  the size  of some&lt;br /&gt;     commonly  used types, e.g. int,  short int, long  int, float, double&lt;br /&gt;     and  so  on.  Try  some arithmetic to familiarize yourself  with the&lt;br /&gt;     basic  operators,  +, -,  *,  /,  and  one  that  doesn't appear  in&lt;br /&gt;     &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt;,  the modulus operator, %,  which acts on  integer types  to&lt;br /&gt;     yield the  remainder after division.  Use this  to determine whether&lt;br /&gt;     the year 2000 is a leap year.  The rule is that it is a leap year if&lt;br /&gt;     the year is divisible by 4, except if it is a multiple of 100 years,&lt;br /&gt;     unless it is also divisible by 400.&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt;  In addition to the integer and floating point types, there is a type called &lt;code&gt;void&lt;/code&gt;. The meaning of void changes according to context ! If you declare a function returning void, you mean that it returns no value, like a &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt; subroutine. A void in the argument list means that the function takes no arguments (you can have a void function that does take arguments by declaring arguments in the usual way, and you can have a function that does return a value but takes no arguments). Below is an example of a Fortran subroutine and &lt;span style="color:Blue;"&gt;C&lt;/span&gt; function:  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;/*     C Version */                    &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;*        Fortran Version&lt;/span&gt;&lt;br /&gt;                                      &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;br /&gt;void initialize_things( void )         &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      SUBROUTINE INITIALIZE_THINGS&lt;/span&gt;&lt;br /&gt;{                                      &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;*&lt;/span&gt;&lt;br /&gt;/*  Do cunning setup procedure     */  &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;*     Do cunning setup procedure  &lt;/span&gt;&lt;br /&gt;/*  No need for a return statement */  &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;*&lt;/span&gt;&lt;br /&gt;}                                      &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      END&lt;/span&gt;&lt;br /&gt;     .                                &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;       .&lt;/span&gt;&lt;br /&gt;/*  Call it */                         &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;*     Call it&lt;/span&gt;&lt;br /&gt;   initialize_things(); /* Note () */ &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      CALL INITIALIZE_THINGS&lt;/span&gt;&lt;br /&gt;     .                                &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;       .&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt;  The void qualifier also has yet another meaning, which will be discussed when we look at pointers. &lt;/p&gt; &lt;p&gt;  The void function above demonstrates the general form of functions in &lt;span style="color:Blue;"&gt;C&lt;/span&gt;. They have a function definition with the formal parameters, then a {body} enclosed by the {} brackets. Function arguments are always passed by value in &lt;span style="color:Blue;"&gt;C&lt;/span&gt;. The actual arguments are copied to the (local) function formal arguments, as if by assignment. The arguments may be expressions, or even calls to other functions. The order of evaluation of arguments is unspecified, so don't rely on it ! Here is a &lt;span style="color:Blue;"&gt;C&lt;/span&gt; function example, with a similar &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt; routine for comparison.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;/*     C Version */                    &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;*     Fortran Version&lt;/span&gt;&lt;br /&gt;                                      &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;br /&gt;int funcy( int i )                     &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      INTEGER FUNCTION FUNCY( I )&lt;/span&gt;&lt;br /&gt;{                                      &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      INTEGER I&lt;/span&gt;&lt;br /&gt;                                      &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;*&lt;/span&gt;&lt;br /&gt;   int j = 0;                         &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      INTEGER J&lt;/span&gt;&lt;br /&gt;/*  End of declarations ... */         &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;*     End of declarations ...&lt;/span&gt;&lt;br /&gt;   j = i;                             &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      J = I&lt;/span&gt;&lt;br /&gt;   i = i + 1;/* Only local i changed*/&lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      I = I + 1 ! Calling arg changed&lt;/span&gt;&lt;br /&gt;   j = i*j;                           &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      J = I*J&lt;/span&gt;&lt;br /&gt;                                      &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      FUNCY = J&lt;/span&gt;&lt;br /&gt;   return( j );                       &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      RETURN&lt;/span&gt;&lt;br /&gt;}                                      &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      END&lt;/span&gt;&lt;br /&gt;     .                                &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;       .&lt;/span&gt;&lt;br /&gt;/*  Call it */                         &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;*     Call it&lt;/span&gt;&lt;br /&gt;   k = 3;                             &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      K = 3&lt;/span&gt;&lt;br /&gt;   ival = funcy(k); /* ival is 12 */  &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      IVAL = FUNCY( K ) ! IVAL is 12&lt;/span&gt;&lt;br /&gt;     .              /* k is still 3 */&lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;       .                ! K is 4&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt;  Notice that changing the function parameter in the &lt;span style="color:Blue;"&gt;C&lt;/span&gt; function does not alter the actual argument, only the local copy. To change an actual argument, you would pass it by address, using the address operator, &amp;amp;, and declare the function argument as a pointer to type int. More will be said about this in the pointers section. Generally, you should avoid writing functions in &lt;span style="color:Blue;"&gt;C&lt;/span&gt; that change the actual arguments. It is better to return a function value instead, where possible.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;/*     C Version */                    &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;*     Fortran Version&lt;/span&gt;&lt;br /&gt;                                      &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;br /&gt;   myval = funcy( gibbon );           &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      CALL FUNCY( GIBBON, MYVAL )&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;    &lt;cite&gt; &lt;/cite&gt;&lt;pre&gt;      Programming Challenge 3&lt;br /&gt;     _______________________&lt;br /&gt;    &lt;br /&gt;       Hack  your copy of the "hello.c" to call some sort  of  arithmetic&lt;br /&gt;     function,  perhaps to return the square  of the argument.  Write the&lt;br /&gt;     function, and add a "prototype"  (these are discussed  later) for it&lt;br /&gt;     before the main program, e.g.&lt;br /&gt;&lt;br /&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;           .&lt;br /&gt;     /*  Function prototype */&lt;br /&gt;     int funcy( int myarg ); /* semicolon where function body would be */&lt;br /&gt;           .&lt;br /&gt;     /* Main Program starts here */&lt;br /&gt;     int main( int argc, char *argv[] )&lt;br /&gt;     {&lt;br /&gt;           .&lt;br /&gt;     }&lt;br /&gt;&lt;br /&gt;     /* The real McCoy - "Dammit Jim, I'm a function not a prototype" */&lt;br /&gt;     int funcy( int myarg )&lt;br /&gt;     {&lt;br /&gt;           .&lt;br /&gt;       /*  Do something and return() an int value */&lt;br /&gt;           .&lt;br /&gt;     }&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;     If  you are  feeling  really  cocky,  write a recursive  factorial()&lt;br /&gt;     function that calls itself. Hint:&lt;br /&gt;&lt;br /&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;              .&lt;br /&gt;         if ( n &gt; 0)&lt;br /&gt;         {&lt;br /&gt;           factorial = n * factorial( n-1 );&lt;br /&gt;         } else {&lt;br /&gt;           factorial = 1;&lt;br /&gt;         }&lt;br /&gt;              .&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;     Call it from you main program  and step through with the debugger to&lt;br /&gt;     convince yourself that it really is recursive.&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt;  When you write your own functions, try to avoid interpositioning, i.e. naming your function with the same name as a standard library (or system/Motif/X11/Xt library) function. Use &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;$ HELP CC RUN-TIME_FUNCTIONS your_function_name&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; to check for the existence of a similarly named DEC &lt;span style="color:Blue;"&gt;C&lt;/span&gt; RTL function. Or look in a book. It is a very bad idea to replace a standard function. If you need to write something with the same purpose as a standard function, but maybe with better accuracy or speed, call it something different, e.g. &lt;code&gt;my_fast_qsort()&lt;/code&gt; .  &lt;/p&gt; &lt;p&gt;  Three other modifiers I haven't yet explained are &lt;code&gt;static&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;extern&lt;/code&gt;. The static modifier is another one that changes meaning depending on its context. If you declare a global variable &lt;em&gt;or function&lt;/em&gt; as &lt;code&gt;static&lt;/code&gt;, it will still be visible throughout the same compilation unit (file to us), but will NOT be visible externally to programs linked against our routines. This is often used as a neat way of storing data that has to be visible to a number of related functions, but must not be accessible from outside. Some code fragments below illustrate this.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;/*---- C Fragments -----------------------------------------------------------*/&lt;br /&gt;/* Global Vars, NOT visible externally (i.e. to things linked against this)   */&lt;br /&gt;static int number_of_things;&lt;br /&gt;&lt;br /&gt;int AddToThings( int a_thing )&lt;br /&gt;{&lt;br /&gt;       .&lt;br /&gt;   number_of_things = number_of_things + 1;&lt;br /&gt;   return( number_of_things );&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int GetNumberOfThings(void)&lt;br /&gt;{&lt;br /&gt;   return( number_of_things );&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int RemoveThing( int a_thing )&lt;br /&gt;{&lt;br /&gt;       .&lt;br /&gt;   number_of_things = number_of_things - 1;&lt;br /&gt;   return( number_of_things );&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:Green;"&gt;&lt;br /&gt;*      Fortran (sort of) Equivalent&lt;br /&gt;*-----------------------------------------------------------------------&lt;br /&gt;     INTEGER FUNCTION ADD_TO_THINGS( A_THING )&lt;br /&gt;        .&lt;br /&gt;     INTEGER NUMBER_OF_THINGS&lt;br /&gt;     SAVE    NUMBER_OF_THINGS&lt;br /&gt;        .&lt;br /&gt;     NUMBER_OF_THINGS = NUMBER_OF_THINGS + 1&lt;br /&gt;     ADD_TO_THINGS_ = NUMBER_OF_THINGS&lt;br /&gt;     RETURN&lt;br /&gt;*&lt;br /&gt;     ENTRY FUNCTION GET_NUMBER_OF_THINGS()&lt;br /&gt;     GET_NUMBER_OF_THINGS = NUMBER_OF_THINGS&lt;br /&gt;     RETURN&lt;br /&gt;*&lt;br /&gt;     ENTRY REMOVE_THING( A_THING )&lt;br /&gt;        .&lt;br /&gt;     NUMBER_OF_THINGS = NUMBER_OF_THINGS - 1&lt;br /&gt;     REMOVE_THING = NUMBER_OF_THINGS&lt;br /&gt;     RETURN&lt;br /&gt;*&lt;br /&gt;     END&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt;  Another use of static is with variables that are local to a function. In this case it is similar to the &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt; SAVE statement, i.e. the variable will retain its value across function calls, and WILL BE INITIALIZED to 0 if it is an integer type, or 0.0 if a floating point type (even if the floating point representation of 0 on your machine is not all bits set to 0), or &lt;code&gt;NULL&lt;/code&gt; (pointer to nothing) if it is a pointer.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;/*---- C Example -------------------------------------------------------------*/&lt;br /&gt;int log_error( int code )&lt;br /&gt;{&lt;br /&gt;   static int total_number_of_errors;&lt;br /&gt;/*  End of declarations ... */&lt;br /&gt;&lt;br /&gt;/*  ++ is the same as  total_number_of_errors = total_number_of_errors + 1 */&lt;br /&gt;   return( ++total_number_of_errors );&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:Green;"&gt;&lt;br /&gt;*     Fortran Equivalent&lt;br /&gt;*-----------------------------------------------------------------------&lt;br /&gt;     SUBROUTINE LOG_ERROR( CODE )&lt;br /&gt;       .&lt;br /&gt;     INTEGER TOTAL_NUMBER_OF_ERRORS&lt;br /&gt;*     Not required for non-recursive DEC Fortran, but it documents your intent&lt;br /&gt;     SAVE    TOTAL_NUMBER_OF_ERRORS&lt;br /&gt;       .&lt;br /&gt;     TOTAL_NUMBER_OF_ERRORS = TOTAL_NUMBER_OF_EBRORS + 1&lt;br /&gt;     END&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; The &lt;code&gt;const&lt;/code&gt; modifier is used to flag a &lt;em&gt;read only&lt;/em&gt; quantity. For example, &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   const double pi = 3.14159265358979;&lt;br /&gt;     .&lt;br /&gt;/*  Arizona ? */&lt;br /&gt;   pi = 3.0;     /* Gives compiler error - try it in your test program */&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; The const modifier is useful for function prototype arguments which are passed by pointer, where you want to indicate that your function will not change the object pointed to. More will be said about function prototypes later. &lt;/p&gt;  &lt;cite&gt; &lt;/cite&gt;&lt;pre&gt;      Programming Challenge 4&lt;br /&gt;     _______________________&lt;br /&gt;    &lt;br /&gt;       Look   at   the   &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt;   example  above.  Spot  the  deliberate&lt;br /&gt;     mistake. The compiler would probably flag an error for it, but think&lt;br /&gt;     of  another instance where  perhaps you wanted to increment an array&lt;br /&gt;     element indexed by a  non-trivial expression. Using the ++  operator&lt;br /&gt;     in &lt;span style="color:Blue;"&gt;C&lt;/span&gt;  helps  avoid  typographical errors, and looks less clumsy (and&lt;br /&gt;     saves valuable bytes ;-) ).  There is a similar  operator, --, which&lt;br /&gt;     decrements by one. Read K&amp;amp;R II, pages 46-48, and pages 105-106. Make&lt;br /&gt;     sure  you  understand  the  difference  between prefix  and  postfix&lt;br /&gt;     versions  of ++ and --, and  try to rewrite the AddToThings() set of&lt;br /&gt;     functions using these operators. Great - that's saved  me  having to&lt;br /&gt;     explain it all.&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt;   The &lt;code&gt;extern&lt;/code&gt; qualifier is rather like EXTERN in &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt;, and basically gives type information for a reference that is to be resolved by the linker. You DO NOT need to use &lt;code&gt;extern&lt;/code&gt; with function declarations - &lt;code&gt;int funcy( int i );&lt;/code&gt; is the same as &lt;code&gt;extern int funcy( int i); &lt;/code&gt;. It is usually used when declaring global variables to indicate that they are referenced in the particular compilation unit, but not defined in it.  &lt;/p&gt; &lt;p&gt;  What is the difference between "definition" and "declaration" ? In short, a definition actually ALLOCATES SPACE for the entity, whereas a declaration tells the compiler what the entity is and what it is called, but leaves it up to the linker to find space for it ! A global variable, structure or function can have many declarations, but only one definition. This is explained in more details in the "Header Files" section which follows. &lt;/p&gt; &lt;p&gt;  Three less commonly used modifiers are &lt;code&gt;volatile&lt;/code&gt;, &lt;code&gt;auto&lt;/code&gt; and &lt;code&gt;register&lt;/code&gt;. The volatile modifier tells the compiler not to perform any optimization tricks with the variable, and is most often used with locations that refer to hardware, like memory-mapped IO, or shared memory regions which might change in a way the compiler cannot predict. The &lt;code&gt;auto&lt;/code&gt; qualifier may only be used for variables at function scope (inside {}) and is in fact the default. Auto variables are usually allocated off the stack (but this is up to the implementation). They will certainly not be retained across function calls. NEVER return the ADDRESS of an automatic variable from a function call (once you know about pointers). Because &lt;code&gt;new&lt;/code&gt; automatic variables are "created" every time you go into a function, this allows &lt;span style="color:Blue;"&gt;C&lt;/span&gt; functions to be called recursively. The register qualifier is really obsolete. It is a hint to the compiler that a variable is frequently used and should be placed in a register. The compiler is quite free to ignore this hint, and frequently does, because it generally knows far more about optimizing than you do (Microsoft Visual &lt;span style="color:DarkBlue;"&gt;C++&lt;/span&gt; or DEC &lt;span style="color:Blue;"&gt;C&lt;/span&gt; for example). Don't bother using register.  &lt;/p&gt; &lt;p&gt;  Enumerated types, &lt;code&gt;enum&lt;/code&gt;, are similar to &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt; integer &lt;span style="color:Green;"&gt;PARAMETER&lt;/span&gt;s, but nicer to use. The general form is &lt;code&gt;enum identifier { enumerator_list }&lt;/code&gt;, where "identifier" is optional but recommended. The comma-separated list of enumerated values starts at zero by default, but you can override this as shown in the example.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;     C Example&lt;br /&gt;&lt;br /&gt;/*----------------------------------------------------------------------------*/&lt;br /&gt;   enum timer_state_e { TPending, TExpired, TCancelled};&lt;br /&gt;   enum timer_trn_e { TmrSet=4401, TCancel=4414};&lt;br /&gt;        .&lt;br /&gt;   enum timer_state_e t_state;&lt;br /&gt;   enum timer_trn_e t_trn;&lt;br /&gt;        .&lt;br /&gt;   t_state = TExpired; /* t_state now contains 1 */&lt;br /&gt;   t_trn = TCancel;    /* t_trn now contains 4414 */&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:Green;"&gt;&lt;br /&gt;*     Fortran Example&lt;br /&gt;*------------------------------------------------------------------------&lt;br /&gt;     INTEGER TPENDING, TEXPIRED, TCANCELLED&lt;br /&gt;     INTEGER TSET, TCANCEL&lt;br /&gt;     PARAMETER (TPENDING = 0, TEXPIRED = 1, TCANCELLED = 2)&lt;br /&gt;     PARAMETER (TSET = 4401, TCANCEL = 4414)&lt;br /&gt;         .&lt;br /&gt;     INTEGER T_STATE, T_TRN&lt;br /&gt;         .&lt;br /&gt;     T_STATE = TEXPIRED&lt;br /&gt;     T_TRN = TCANCEL&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; When examining t_state or t_trn in the &lt;span style="color:Blue;"&gt;C&lt;/span&gt; program with the DEC debugger, the integer value will be converted to a name, e.g. &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;DBG&gt; EXAMINE t_trn&lt;br /&gt;PROG\main\t_trn:   TCancel&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; which is handy. Unfortunately, because the enumerated types are really type int, you can assign any integer value to &lt;code&gt;t_trn&lt;/code&gt; without a compiler whinge ! Types and storage class modifiers are discussed in more detail in K&amp;amp;R II, page 209 onwards, if you still thirst for knowledge.  &lt;/p&gt;  &lt;h2&gt;&lt;a name="Loop"&gt;§3 Loop and Flow Control Constructs&lt;/a&gt;&lt;/h2&gt;   &lt;span style="color:Blue;"&gt;C&lt;/span&gt; has three basic loop constructs. These are &lt;code&gt;for&lt;/code&gt; loops, &lt;code&gt;while&lt;/code&gt; loops and &lt;code&gt;do&lt;/code&gt; loops. An example is worth a thousand words:   &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;&lt;span style="color:Green;"&gt;*     Fortran Loops Example&lt;br /&gt;          .&lt;br /&gt;     INTEGER I&lt;br /&gt;     LOGICAL FIRST&lt;br /&gt;          .&lt;br /&gt;       PRINT *, I&lt;br /&gt;     ENDDO&lt;br /&gt;*&lt;br /&gt;     I = 0&lt;br /&gt;     DO WHILE ( I .LT. LIMIT )&lt;br /&gt;       I = I + 1&lt;br /&gt;       PRINT *, I&lt;br /&gt;     ENDDO&lt;br /&gt;*&lt;br /&gt;     FIRST = .TRUE.&lt;br /&gt;     DO WHILE ( FIRST  .OR.  I .LT. LIMIT )&lt;br /&gt;       IF ( FIRST ) FIRST = .FALSE.&lt;br /&gt;       PRINT *, I&lt;br /&gt;       I = I + 1&lt;br /&gt;     ENDDO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;/*---- C Loops Example ("loops.c") -------------------------------------------*/&lt;br /&gt;&lt;br /&gt;/* ANSI C Headers */&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;stdlib.h&gt;&lt;br /&gt;&lt;br /&gt;/* Defines and Macros */&lt;br /&gt;#define LMT 5&lt;br /&gt;&lt;br /&gt;/* Main Program starts here */&lt;br /&gt;int main( int argc, char *argv[] )&lt;br /&gt;{&lt;br /&gt;   int i;&lt;br /&gt;/*  End of declarations ... */&lt;br /&gt;   printf("LMT = %d\n", LMT);&lt;br /&gt;&lt;br /&gt;   printf("\n'for' loop - for ( i = 1; i &lt;= LMT; i++ ) {...}\n");&lt;br /&gt;   &lt;span style="color:Red;"&gt;for ( i = 1; i &lt;= LMT; i++ ) { /* More usual in C would be  i = 0; i &lt;&gt;&lt;br /&gt;     printf("%d\n", i );&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   printf("\ni = 0\n");&lt;br /&gt;   printf("'while' loop - while ( i++ &lt; LMT ) {...}\n");&lt;br /&gt;   i = 0;&lt;br /&gt;   while ( i++ &lt; LMT )&lt;br /&gt;   {&lt;br /&gt;     printf("%d\n", i );&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   printf("\ni = LMT\n");&lt;br /&gt;   printf("'do' loop - do {...} while ( ++i &lt; LMT ); - always executes at least once\n");&lt;br /&gt;   i = LMT;&lt;br /&gt;   do&lt;br /&gt;   {&lt;br /&gt;     printf("%d\n", i );&lt;br /&gt;   } while ( ++i &lt; LMT );&lt;br /&gt;&lt;br /&gt;   exit(EXIT_SUCCESS);&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; All these constructs are explained in detail in K&amp;amp;R II, chapter 3. The &lt;code&gt;for&lt;/code&gt; loop has the following general form:  &lt;/p&gt;&lt;p&gt;  &lt;code&gt; &lt;/code&gt;&lt;/p&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   for ( expression1; terminate_if_false_expression2; expression3 ) {&lt;br /&gt;     .&lt;br /&gt;   }&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; If "terminate_if_false_expression2" is missed out it is taken as being true, so an infinite loop results, &lt;code&gt;for (;;) {ever}&lt;/code&gt;. The "expression1" is evaluated once before the loop starts and is most often used to initialize the loop count, whereas "expression3" is evaluated on every pass through the  loop, just before starting the next loop, and is frequently used to modify the loop counter. It is quite legal, in &lt;span style="color:Blue;"&gt;C&lt;/span&gt;, to modify the loop counter within the loop, and the loop control variable retains its value when the loop terminates. Obviously "terminate_if_false_expression2" causes the loop to end if it is false, and is used to test the termination condition.  &lt;/p&gt; &lt;p&gt;  The "while" looks like this: &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   while ( expression )&lt;br /&gt;   {&lt;br /&gt;     .&lt;br /&gt;   }&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; and keeps going for as long as "expression" is true. It &lt;em&gt;zero trips&lt;/em&gt; (that is, the code in it is never executed) if "expression" is false on the first encounter. The &lt;code&gt;for&lt;/code&gt; loop above could be written using &lt;code&gt;while&lt;/code&gt;.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;    expression1;&lt;br /&gt;   while ( terminate_if_false_expression2 )&lt;br /&gt;   {&lt;br /&gt;     .&lt;br /&gt;     expression3;&lt;br /&gt;   }&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; It isn't a good idea to do this though, because someone will spend ages looking at your code wondering why you didn't write a &lt;code&gt;for&lt;/code&gt; loop, expecting some cunning algorithm.  &lt;/p&gt; &lt;p&gt;  Finally, before time, the old enemy, makes us leave Loopsville City Limits, let's look at the "do-while" construct. The loop body &lt;em&gt;is always executed at least once&lt;/em&gt; &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   do&lt;br /&gt;   {&lt;br /&gt;     .&lt;br /&gt;   } while ( expression ); /* Semicolon needed */&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; and the loop will be repeated if "expression" is true at the end of the current loop. There is a keyword, &lt;code&gt;break&lt;/code&gt;, which lets you leave the innermost loop early, transferring control to the statement immediately after the loop.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   for ( i = 0; i &lt; strlen(string); i++) {&lt;br /&gt;     if ( string[i] == '$' )&lt;br /&gt;     {&lt;br /&gt;       found_dollar = TRUE;&lt;br /&gt;/*      Once we've found the dollar no need to search rest of string */&lt;br /&gt;       break;&lt;br /&gt;     }&lt;br /&gt;   }&lt;br /&gt;/*  Jump to here on "break" */&lt;br /&gt;    .&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; A related keyword, &lt;code&gt;continue&lt;/code&gt;, skips to the end of the loop and continues with the next loop iteration.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   for ( i = 0; i &lt; strlen(string); i++) {&lt;br /&gt;/*    Don't  bother trying to upcase spaces */&lt;br /&gt;     if ( string[i] == ' ' ) continue; /* Move on to next character */&lt;br /&gt;/*      It wasn't a space so have a go */&lt;br /&gt;       string[i] = toupper( string[i] );&lt;br /&gt;   }&lt;br /&gt;/*  Jump to here on "break" */&lt;br /&gt;    .&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; This is most often used to avoid complex indenting and "if" tests. Don't use it like I just did, which was a silly example. &lt;/p&gt; &lt;p&gt;  You have already met the "if" construct. Here it is again, with the "else if" demonstrated too. &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   if ( expression )&lt;br /&gt;   {&lt;br /&gt;      .&lt;br /&gt;      /* Do something */&lt;br /&gt;      .&lt;br /&gt;   }&lt;br /&gt;   else if ( other_expression )&lt;br /&gt;   {   &lt;br /&gt;      .&lt;br /&gt;      /* Do something else */&lt;br /&gt;      .&lt;br /&gt;   }&lt;br /&gt;   else if ( final_expression )&lt;br /&gt;   {   &lt;br /&gt;      .&lt;br /&gt;      /* Do something different */&lt;br /&gt;      .&lt;br /&gt;   }&lt;br /&gt;   else&lt;br /&gt;   {&lt;br /&gt;      .&lt;br /&gt;/*    Catch all if none of above expressions are true */&lt;br /&gt;      .&lt;br /&gt;   }&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;    It is legal to write this kind of thing  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   if ( expression )       /* Avoid this form */&lt;br /&gt;     i = 1;&lt;br /&gt;   else&lt;br /&gt;     i = 2;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   The problem arises if you do this  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   if ( expression )       /* This is probably not what was intended */&lt;br /&gt;     i = 1;&lt;br /&gt;   else&lt;br /&gt;     i = 2;&lt;br /&gt;     &lt;span style="color:Red;"&gt;dont_forget_this = 3;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; You might think that if "expression" is true (i.e. non-zero) then you would set &lt;code&gt;i&lt;/code&gt; to 1, and if it were false you would set &lt;code&gt;i&lt;/code&gt; to 2 and &lt;code&gt;dont_forget_this&lt;/code&gt; to 3. In fact you will always set &lt;code&gt;dont_forget_this&lt;/code&gt; to 3, because only the first statement after the "else" is grouped with the "else". I never use this form, other than for a one liner like  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   if ( expression ) expression_was_true = TRUE;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; where the meaning is clear. Use the bracketed form which makes it totally unambiguous, and is easier to use with the debugger. &lt;/p&gt; &lt;p&gt;  &lt;span style="color:Blue;"&gt;C&lt;/span&gt; provides an alternative to lots of if - else if tests. This is the "switch" statement. The "expression_yielding_integer" is calculated, and matched against the "case" "const-int-expression"s. When one matches, the statements following are executed, or if none match, the statements following "default" are executed  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;    switch ( expression_yielding_integer ) {&lt;br /&gt;     case const-int-expression1:&lt;br /&gt;       statements1;&lt;br /&gt;     case const-int-expression2:&lt;br /&gt;       statements2;&lt;br /&gt;     case const-int-expression3:&lt;br /&gt;       statements3;&lt;br /&gt;         .&lt;br /&gt;         .&lt;br /&gt;     default:&lt;br /&gt;       statementsN;&lt;br /&gt;   }&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; Unfortunately a bad default behaviour was chosen for this. Each "case" drops through to the next one by default, so if, say, "expression_yielding_integer" matched "const-int-expression2", then "statements2" through to "statementsN" would ALL be executed. This is solved by using "break" again. &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;    switch ( expression_yielding_integer ) {&lt;br /&gt;     case const-int-expression1:&lt;br /&gt;       statements1;&lt;br /&gt;       break;                      /* Always use break by default */&lt;br /&gt;     case const-int-expression2:&lt;br /&gt;       statements2;&lt;br /&gt;       break;&lt;br /&gt;     case const-int-expression3:&lt;br /&gt;       statements3;&lt;br /&gt;       break;&lt;br /&gt;         .&lt;br /&gt;         .&lt;br /&gt;     default:&lt;br /&gt;       statementsN;&lt;br /&gt;       break;&lt;br /&gt;   }&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; The default behaviour is rarely what is required in practise, and it would have been far better to have a default "break" before each case, and maybe use "continue" to indicate fall-through. Remember that chars can be used as small integers, so the following is quite legal.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   char command_line_option;&lt;br /&gt;    .&lt;br /&gt;   switch ( command_line_option ) {&lt;br /&gt;     case 'v':&lt;br /&gt;       verbose_mode = TRUE;&lt;br /&gt;       break;&lt;br /&gt;     case 'l':&lt;br /&gt;       produce_listing = TRUE;&lt;br /&gt;       break;&lt;br /&gt;     case '?':               /* Following two cases deliberately fall thru */&lt;br /&gt;     case 'h':&lt;br /&gt;       display_help = TRUE;&lt;br /&gt;       break;&lt;br /&gt;     default:&lt;br /&gt;       use_default_options = TRUE:&lt;br /&gt;       break;&lt;br /&gt;   }&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;    &lt;h2&gt;&lt;a name="Arrays"&gt;§4 Arrays&lt;/a&gt;&lt;/h2&gt;  Arrays in &lt;span style="color:Blue;"&gt;C&lt;/span&gt; always start at the 0 element rather than 1, and there is &lt;b&gt;no array bound checking&lt;/b&gt; (gasps of horror). Here is a one-dimensional example array:   &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   int job[20]; /* job[0], job[1] .. job[19] */&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; and the dimension must be an integer greater than zero. This is how to declare a two-dimensional array [rows][columns] &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   int job[4][20]; /* Like 4 job[20] 's, job[0][0], job[0][1] .. job[3][19] */&lt;br /&gt;         .&lt;br /&gt;   i = job[2][0];  /* Good */&lt;br /&gt;         .&lt;br /&gt;   &lt;span style="color:Red;"&gt;i = job[2,0];   /* Bad - don't ever do this */&lt;/span&gt;&lt;br /&gt;         .&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt;  Multi-dimensional arrays are column major; that is, the right-most subscript varies fastest, unlike &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt;. Notice that you can't use commas to separate the indices. Separate pairs of square brackets are needed for each index. There is no limit to the number of dimensions other than those imposed by your compiler and the amount of memory available. In practice, multi-dimensional arrays are rarely used. Unfortunately, you can't (in &lt;span style="color:Blue;"&gt;C&lt;/span&gt;) use const int's as array bounds. You have to use &lt;code&gt;#define&lt;/code&gt;, like this:  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;#define MAX_SIZE&lt;br /&gt;     .&lt;br /&gt;   float floaty[MAX_SIZE];&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;        &lt;p&gt; More will be said about &lt;code&gt;#define&lt;/code&gt; later. Arrays can be initialized when they are defined:  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   int days_in_month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };&lt;br /&gt;   int matrix[2][3] = { { 0, 1, 2 }, { 3, 4, 5 } };&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; Remember that uninitialized arrays can contain anything at all, so don't expect them to be full of zeros. In addition, initialized arrays can't be "demand zero compressed". You can leave out the size of an array and have it use the number of initializers, like this  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   int array_initialization_pages_in_K_and_R_II[] = { 86, 112, 113, 219 };&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; which produces an array of 4 integers. You would probably want to use &lt;code&gt;sizeof()&lt;/code&gt; to determine the size of the array in this case &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;    nelements =   sizeof( array_initialization_pages_in_K_and_R_II )    /&lt;br /&gt;               /* ----------------------------------------------------- */&lt;br /&gt;                sizeof( array_initialization_pages_in_K_and_R_II[0] );&lt;br /&gt;       .&lt;br /&gt;    for ( i = 0; i &lt; nelements; i++)&lt;br /&gt;    {&lt;br /&gt;       .&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; Notice that you index up to LESS THAN the number of elements, because the last element is (nelements-1). If you accidentally go past the end of an array, then if you are reading from the array you can get strange values. A nastier siutation occurs if you are setting the values. &lt;/p&gt; &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;void MyFunction(void)&lt;br /&gt;{&lt;br /&gt; int Array[5];&lt;br /&gt; int iLoopLimit = 10;&lt;br /&gt;       .&lt;br /&gt; for ( i = 0; i &lt; iLoopLimit; i++)&lt;br /&gt; {&lt;br /&gt;   /* Oh dear - we are going to write to memory past the end of the array */&lt;br /&gt;   Array[i] = GetSomeValue();&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; The above code could have a particularly nasty effect on certain machine architectures. It is quite common for the return address to be stored on the stack, which is where the automatic variable Array is allocated from too. By writing past the end of the array, it is possible for you to corrupt the return value such that when MyFunction returns, you end up in a seemingly random place, such as half way through some function you have never (intentionally) called. This is bad enough just from the point of view of trying to debug your program. It gets worse though. Imagine that the hypothetical GetSomeValue() function got its data from some external source, e.g. this was part of some web server. If a naughty hacker could pass something, perhaps a long string, to corrupt the return pointer in a known way, then he or she would be able to run arbitrary code of their choice, with all the privileges of your server process. Many real world computer "viruses" and "worms" use exactly this technique to "hack into" people's web servers and systems. &lt;/p&gt;  &lt;p&gt; When initializing arrays or structs, you can initialize with less elements than there are in the array or structure, and remaining ones will be set to zero (or NULL for an arra of pointers). This saves typing, or adding more initializers if teh size or structure changes, and makes it easier to debug because you start with known values.  &lt;code&gt; &lt;/code&gt;&lt;/p&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt; int FirstArray[ARRAY_SIZE] = {0}; /* All elements will be zero */&lt;br /&gt; int Array2[ARRAY_SIZE] = {1}; /* 1st element is 1, remaining elements all 0 */&lt;br /&gt; struct point&lt;br /&gt; {&lt;br /&gt;   int x;&lt;br /&gt;   int y;&lt;br /&gt; };&lt;br /&gt; struct point origin = {0}; /* origin.x and origin.y will both be zero */&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;h2&gt;&lt;a name="Strings"&gt;§5 Strings&lt;/a&gt;&lt;/h2&gt;   Character arrays containing a contiguous sequence of characters terminated with the null character, '\0', are known as strings in &lt;span style="color:Blue;"&gt;C&lt;/span&gt;. Initialized character arrays become &lt;code&gt;static&lt;/code&gt; by default, i.e. retain their value across function calls, unless you change them. The initializer, or "string literal" is delimited by double quotes "like this" . You can split a string initializer over several lines, each part being in " quotes, and they will be concatenated together. The resultant string has the null character, represented by the escape sequence '\0', appended to the end of it.   &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   char random[80]; /* Could contain anything */&lt;br /&gt;   char title[] = "Phil's Ramblings"; /* Takes 17 bytes due to '\0' at end */&lt;br /&gt;&lt;br /&gt;   char longer_string[] = "Here is quite a long string split up over"&lt;br /&gt;                          "two lines. VAX C doesn't allow this, though."&lt;br /&gt;                          "Another good reason to switch to DEC C on"&lt;br /&gt;                          "VAX or Alpha VMS, or Visual C++ for Windows."&lt;br /&gt;&lt;br /&gt;   char string_with_quote[] = "Here is the quote \" character";&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt;  The name that you give to an array can be used as a pointer to the zeroeth element of the array. More will be said about this in the "Pointers" section. There are many functions in the standard library for manipulating character strings, and these all begin with "str". You will need to include &lt;code&gt;&lt;string.h&gt;&lt;/code&gt; to use them. Look in K&amp;amp;R II pages 249-250. These functions expect an array, or a pointer to characters, as their arguments. &lt;/p&gt; &lt;p&gt;  Finally, note that an empty string is not really empty. &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   char is_it_empty[] = ""; /* No, it contains one character, '\0' */&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; Always bear in mind that the string functions often copy trailing '\0' characters, so you must ensure that you allow space for this. It is a good idea to always use the "strn" versions of the calls, with &lt;code&gt;sizeof(destination)&lt;/code&gt; as the character limit, because that way you will avoid runaway (and hard to detect) memory overwriting. Remember to terminate the destination string, e.g.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   char destination[MAX_SIZE] = {'\0'};&lt;br /&gt;   strncpy( destination, source, sizeof(destination)/sizeof(destination[0]) );&lt;br /&gt;   destination[sizeof(destination)/sizeof(destination[0])-1] = '\0';   &lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; else you'll end up avoiding potential overwrites, but leave a potentially unterminated string to catch you out later. Why use sizeof(destination)/sizeof(destination[0])? Well for ANSI, you could argue that you could just use sizeof(destination) because the value returned by sizeof() is defined in terms of the size of char. However, as Unicode and "wide character" strings become more commonly used, then you need to think carefully about whether you mean "the number of bytes" (or more accurately, the size of something in terms of char) or the number of characters. For wide characters, you really don't want to pass, sizeof(widestring) to wcsncpy or you will end up causing a buffer overrun, i.e. you will write past the end of the wide character array or allocated space. Note also that you can't do this with a pointer. The sizeof(pointer) would return the size of the pointer, not the size of the thing it pointed to. &lt;/p&gt;  &lt;h2&gt;&lt;a name="Pointers"&gt;§6 Pointers&lt;/a&gt;&lt;/h2&gt;   Pointers are declared using the *, or "dereference operator".  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   int *i_ptr;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; declares a pointer to type int. As declared above, &lt;code&gt;i_ptr&lt;/code&gt; is most likely not yet pointing at a valid location. In order to make it point somewhere valid, you generally use the "address operator", &amp;amp;, like this  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   int i;&lt;br /&gt;   int j;&lt;br /&gt;   int *i_ptr;&lt;br /&gt;    .&lt;br /&gt;   i_ptr = &amp;i;&lt;br /&gt;    .&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; You can then change or read the value of &lt;code&gt;i&lt;/code&gt; by using the "dereference operator", and change the object pointed to, providing it is an object of the correct type.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   *i_ptr = 3; /* Set the int pointed to by i_ptr to 3 */&lt;br /&gt;   printf("%d\n", i );      /* i will be 3 */&lt;br /&gt;    .&lt;br /&gt;   i_ptr = &amp;j; /* Set the i_ptr to point to j now */&lt;br /&gt;    .&lt;br /&gt;   *i_ptr = 3; /* Set the int pointed to by i_ptr to 3 */&lt;br /&gt;   printf("%d\n", j );  /* j will be 3 */&lt;br /&gt;    .&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; This is a rather silly example, because you would obviously just use &lt;code&gt;i &lt;/code&gt; or &lt;code&gt;j&lt;/code&gt; directly. A more realistic use of pointers is with arrays:  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   char string[] = "Here is a string with a $ in it";&lt;br /&gt;   char *sptr;&lt;br /&gt;   int contains_dollar;&lt;br /&gt;    .&lt;br /&gt;   sptr = string; /* Remember that the array name is the same as &amp;amp;array[0] */&lt;br /&gt;   contains_dollar = FALSE;&lt;br /&gt;&lt;br /&gt;   while ( *sptr ) /* While thing pointed to is not 0 i.e. null character */&lt;br /&gt;   {&lt;br /&gt;     if ( *sptr == '$' )&lt;br /&gt;     {&lt;br /&gt;       contains_dollar = TRUE;&lt;br /&gt;       break; /* Leave the while loop early and safely */&lt;br /&gt;     }&lt;br /&gt;     ++sptr;&lt;br /&gt;   }   &lt;br /&gt;    .&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; When you increment pointers, they automatically increment the address they point to by the size of one of the objects to which they point. In the example above, that is one character, i.e. a byte. If the array was an array of int, then the pointer would increment by &lt;code&gt;sizeof(int)&lt;/code&gt; bytes. Just to frighten you, this loop could be written  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   while ( *sptr &amp;amp;&amp;amp; !( contains_dollar = *sptr++ == '$' ) );&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;cite&gt; &lt;/cite&gt;&lt;pre&gt;      Programming Challenge 5&lt;br /&gt;     _______________________&lt;br /&gt;    &lt;br /&gt;       You  guessed it. Figure out what is happening in the scary "while"&lt;br /&gt;     loop above. Now write your own (differently named) version of strcpy&lt;br /&gt;     using similar techniques to make it as short as possible. Note that&lt;br /&gt;     strcpy has no length limit. Why is this a bad thing? Hint: think about&lt;br /&gt;     the problems associated with writing past the end of arrays. Modify&lt;br /&gt;     your function to work like the safer strncpy function.&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; Arrays and pointers are closely related. They can be used in identical ways in many situations. For example: &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;     .&lt;br /&gt;   char string[80];&lt;br /&gt;   char *sptr;&lt;br /&gt;     .&lt;br /&gt;   sptr = &amp;amp;string[0]; /* This could be written as sptr = string */&lt;br /&gt;     .&lt;br /&gt;   *string       = 'A'; /* Using array name like pointer */&lt;br /&gt;   *(string+10)  = 'B'; /* Using array name like pointer */&lt;br /&gt;     .&lt;br /&gt;   sptr[0]       = 'A'; /* Using pointer like array */&lt;br /&gt;   sptr[10]      = 'B'; /* Using pointer like array */&lt;br /&gt;     .&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; This is because, in expressions or function calls, arrays and pointers are both converted to the form "*(pointer + index-offset)". The main thing to remember is that pointers are variables, and can be changed to point to different objects, whereas array names are not variables. The index-offset is automatically scaled according to the type of data pointed to. In this case, we are dealing with &lt;code&gt;char&lt;/code&gt; which, by definition, has a size of 1, but if the pointers were pointers to int, then on the VAX or Alpha, the index-offset would be automatically scaled by 4.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;     .&lt;br /&gt;   int array[20];&lt;br /&gt;   int another_array[20];&lt;br /&gt;   int *i_ptr;&lt;br /&gt;     .&lt;br /&gt;   i_ptr = array;         /* Legal */&lt;br /&gt;   i_ptr[12] = 3;&lt;br /&gt;     .&lt;br /&gt;   i_ptr = another_array; /* Legal */&lt;br /&gt;   i_ptr[2] = 4;&lt;br /&gt;     .&lt;br /&gt;   &lt;span style="color:Red;"&gt;array = another_array; /* Illegal !  */&lt;/span&gt;&lt;br /&gt;     .&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; Even multi-dimensional arrays get decomposed to the "*(pointer + index-offset)" by the compiler in say, a function call, which gives you no special knowledge of how they fold. Hence if you are using a pointer to a multi-dimensional array where the dimensions could vary, it is up to you to calculate the offset correctly, e.g. &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   int mda[ROWS][COLS];&lt;br /&gt;    .&lt;br /&gt;   i = funcy( mda, ROWS, COLS );&lt;br /&gt;    .&lt;br /&gt;int funcy( int *array, rows, cols )&lt;br /&gt;{&lt;br /&gt;    .&lt;br /&gt;   for ( i = 0; i &lt; rows; i++) {&lt;br /&gt;     for ( j = 0; j &lt; cols; j++) {&lt;br /&gt;       total += *(array + i*cols + j);&lt;br /&gt;     }&lt;br /&gt;   }&lt;br /&gt;    .&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; Of course, if the function was only expected to deal with arrays of set dimensions, you could just declare those in funcy(). &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   int mda[ROWS][COLS];&lt;br /&gt;    .&lt;br /&gt;   i = funcy( mda );&lt;br /&gt;    .&lt;br /&gt;int funcy( int array[ROWS][COLS] )&lt;br /&gt;{&lt;br /&gt;    .&lt;br /&gt;   for ( i = 0; i &lt; ROWS; i++) {&lt;br /&gt;     for ( j = 0; j &lt; COLS; j++) {&lt;br /&gt;       total += array[i][j];&lt;br /&gt;     }&lt;br /&gt;   }&lt;br /&gt;    .&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; The strange += assignment operator isn't a misprint. It is shorthand, so that &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   x = x + 4;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; can be written &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   x += 4;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   Similarly  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   y = y - 10;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; becomes &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   y -= 10;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; There must be NO SPACE between the operator and the = sign, and the operator comes immediately before the =. This notation is handy for more complex expressions, such as &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   array[ hash_value[index]*k + offset[i] ] += 4;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; so you only need maintain the expression in one place. Many binary operators have a similar assignment operator. Check K&amp;amp;R II page 50 and page 48 for the bitwise operators that can also be used in this way.  &lt;/p&gt; &lt;p&gt;  The other thing to remember is that whereas arrays allocate space, and hence the array name points to something valid, &lt;b&gt;pointers must never be used until they have been initialized to point to something valid&lt;/b&gt;. &lt;/p&gt; &lt;p&gt;  There is a special pointer value defined by the standard, called the &lt;code&gt;NULL&lt;/code&gt; pointer, which is used to indicate that the pointer doesn't point to anything. Normally, you cannot directly assign integers to pointers, but the &lt;code&gt;NULL&lt;/code&gt; pointer is an exception. Both the following lines make &lt;code&gt;p&lt;/code&gt; point to "nothing" (well, a guaranteed "not valid location" really).  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   i_ptr = 0;    /* Legal but not recommended */   &lt;br /&gt;   i_ptr = NULL; /* Recommended - it is clear that you refer to a pointer */&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; The &lt;code&gt;NULL&lt;/code&gt; macro (see Macros section later on), defined identically in &lt;code&gt;&lt;stddef.h&gt;&lt;/code&gt; and &lt;code&gt;&lt;stdio.h&gt;&lt;/code&gt; among other places, is often defined as  &lt;/p&gt;   &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;#define NULL     ((void *) 0)&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; even though "0" would do. This discourages its use as an integer, which you should never do. People often make the mistake of writing &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   &lt;span style="color:Red;"&gt;string[i] = NULL; /* Never do this - you really want '\0' */&lt;/span&gt;&lt;br /&gt;   &lt;span style="color:Red;"&gt;i = NULL;         /* Never do this if i is integer and you really mean 0 */&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; when what they actually mean is &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   string[i] = '\0'; /* The null character - that's more like it */&lt;br /&gt;   i = 0;            /* Integer zero */&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; A pointer of type &lt;code&gt;(void *)&lt;/code&gt; is a special type of pointer that is guaranteed to be able to point to any type of object, hence the &lt;code&gt;NULL&lt;/code&gt; pointer can be assigned to any pointer type. The &lt;code&gt;NULL&lt;/code&gt; pointer need not have all bits set to zero,  so don't rely on this.  &lt;/p&gt; &lt;p&gt;  Pointers are very useful as function arguments for routines that manipulate strings of unknown (at compile time) length. &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;int how_long( const char *s )&lt;br /&gt;{&lt;br /&gt;   int i = 0;&lt;br /&gt;/*  End of declarations ... */&lt;br /&gt;&lt;br /&gt;   while ( *s++ )&lt;br /&gt;   {&lt;br /&gt;     i++; /* Increment i until '\0' found */&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   return i;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt;  Even though the thing pointed to by &lt;code&gt;s&lt;/code&gt; is &lt;code&gt;const&lt;/code&gt;, note that it is quite legal to increment the pointer &lt;code&gt;s&lt;/code&gt; in the function, because &lt;code&gt;s&lt;/code&gt; is a local, variable pointer, pointing to whatever the calling argument to &lt;code&gt;how_long()&lt;/code&gt; was. Hence if you call &lt;code&gt;how_long(string)&lt;/code&gt;, you don't change string, you assign string to &lt;code&gt;s&lt;/code&gt; then increment &lt;code&gt;s&lt;/code&gt;. Any expression using array subscripting, for example &lt;code&gt;array[index]&lt;/code&gt;, is exactly the same in &lt;span style="color:Blue;"&gt;C&lt;/span&gt; as its pointer equivalent, in this case &lt;code&gt;*(array+index)&lt;/code&gt;. You have to be careful when using the &lt;code&gt;const&lt;/code&gt; modifier with pointers. The following examples should illustrate the point.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   int i = 0;&lt;br /&gt;   &lt;b&gt;const&lt;/b&gt; int *i_ptr = NULL;/* i_ptr can be used to point to a const int */&lt;br /&gt;   int * &lt;b&gt;const&lt;/b&gt; i_ptr = &amp;i; /* i_ptr is const, points to variable int */&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; Another important difference between pointers and arrays relates to the &lt;code&gt;sizeof()&lt;/code&gt; operator.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   int array[20] = {0};&lt;br /&gt;   int *i_ptr = array;&lt;br /&gt;   size_t s = 0;&lt;br /&gt;      .&lt;br /&gt;   s = sizeof(array);  /* s is 20*sizeof(int), which is 100 on the VAX */&lt;br /&gt;   s = sizeof(i_ptr);  /* s is sizeof(int *), which is 4 on the VAX    */&lt;br /&gt;      .&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; You can't deduce the size of an array from a pointer, only the size of the pointer. Because arrays as function arguments are treated the same as pointers, then even if you declare the function arguments as "func( int array[10] )" array is still treated like a pointer in the function body, so &lt;code&gt;sizeof(array)&lt;/code&gt; in the function will give you the size of pointer to &lt;code&gt;int&lt;/code&gt;, not 10 times size of &lt;code&gt;int&lt;/code&gt;.  &lt;/p&gt; &lt;p&gt;  It is quite legal to write a pointer definition like this: &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   int&lt;span style="color:Red;"&gt;*&lt;/span&gt; i_ptr; /* Not recommended */&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; This is best avoided, because it can be confusing. Consider &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   int* i_ptr1, &lt;span style="color:Red;"&gt;i_ptr2&lt;/span&gt;; /* Probably not what you intended */&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; At first glance it looks like you have just declared two pointers to int. In fact, i_ptr1 is a pointer, but i_ptr2 is an int. &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   int *i_ptr1, *i_ptr2; /* Better */&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; The second example keeps the * with the variable to which it relates, and is considered better style (by me at any rate) ! &lt;/p&gt; &lt;p&gt;  There are two standard library functions often used with pointer. They are declared in &lt;code&gt;&lt;stdlib.h&gt;&lt;/code&gt;, and are &lt;code&gt;malloc()&lt;/code&gt; and &lt;code&gt;free()&lt;/code&gt;. Both are AST reentrant under DEC &lt;span style="color:Blue;"&gt;C&lt;/span&gt;. The &lt;code&gt;malloc() &lt;/code&gt; function allocates an area of memory specified in bytes, and is declared as  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;void *malloc(size_t size);&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; and would be used like this &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;    .&lt;br /&gt;   int *i_ptr;&lt;br /&gt;    .&lt;br /&gt;   i_ptr = (int *)malloc( sizeof(int)*nelements_wanted );&lt;br /&gt;   if ( i_ptr != NULL )&lt;br /&gt;   {&lt;br /&gt;      .&lt;br /&gt;     i_ptr[i] = i;&lt;br /&gt;      .&lt;br /&gt;   }&lt;br /&gt;   else&lt;br /&gt;   {&lt;br /&gt;      .&lt;br /&gt;/*    Couldn't get the memory - do some cunning recovery */&lt;br /&gt;      .&lt;br /&gt;   }&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; It is good practise to "cast" the result of a &lt;code&gt;malloc()&lt;/code&gt; to the correct type. This helps the compiler to indirectly check whether you are using the correct type in the &lt;code&gt;sizeof()&lt;/code&gt; invocation too. If it complains about your cast, then (assuming the type is the same in the &lt;code&gt;sizeof()&lt;/code&gt; ) you are probably using the wrong type in both places, and might have allocated too little memory. There is no check if you wander off the allocated memory, out into memory space no man has seen before ! The memory returned by &lt;code&gt;malloc()&lt;/code&gt; can contain any values when you get it, i.e. it is not set to zero.  &lt;/p&gt; &lt;p&gt; The &lt;code&gt;free()&lt;/code&gt; function frees up the memory obtained from &lt;code&gt;malloc()&lt;/code&gt;. It is declared as  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;void free(void *pointer);&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; and would be used like this to free the memory obtained in the previous example &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   free( i_ptr );&lt;br /&gt;   i_ptr = NULL;  /* Good practise */&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; I like to set the pointer to &lt;code&gt;NULL&lt;/code&gt; immediately upon freeing the memory, because the pointer MUST NOT BE USED again after being &lt;code&gt;free()&lt;/code&gt;ed. By setting it to &lt;code&gt;NULL&lt;/code&gt;, you will (under VMS or Windows NT) get an ACCVIO if you try and dereference the pointer. This is safer than leaving it, having the memory reused elsewhere, then changing it via the duff pointer. This sort of mistake is very hard to track down. It is very important to always free &lt;code&gt;malloc()&lt;/code&gt;-ed memory when you are done with it, or you will cause what is known as a "memory leak".  &lt;/p&gt; &lt;p&gt;  There are a couple of functions related to &lt;code&gt;malloc()&lt;/code&gt;. One is &lt;code&gt;calloc()&lt;/code&gt;, which allows you to allocate memory and initialize it's value in one go.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;void *calloc(size_t number, size_t size);&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; The other function is &lt;code&gt;realloc()&lt;/code&gt;, which allows you to expand a region of memory obtained by &lt;code&gt;malloc()&lt;/code&gt;, whilst retaining its current contents.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;void *realloc(void *pointer, size_t size);&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; The new, expanded region of memory need not be in the same place as the original &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;   new_i_ptr = (int *)realloc( i_ptr, sizeof(int)*larger_nelements_wanted );&lt;br /&gt;   if ( new_i_ptr )&lt;br /&gt;   {&lt;br /&gt;     /* Successfully expanded */&lt;br /&gt;     i_ptr = new_i_ptr; /* Don't free anything here ! */&lt;br /&gt;   }&lt;br /&gt;   else&lt;br /&gt;   {&lt;br /&gt;     /* Couldn't get the extra memory, stick with the existing pointer */&lt;br /&gt;   }&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; so in this example the memory may have changed location, but the original content will have been copied to the new location. Note how I use a new pointer, &lt;code&gt;new_i_ptr&lt;/code&gt;, to check that the relocation was successful. This is &lt;em&gt;essential&lt;/em&gt; because if you directly assigned to the pointer to the memory you were trying to &lt;code&gt;realloc&lt;/code&gt; and the call failed (returning &lt;code&gt;NULL&lt;/code&gt;) you would have no way to free the memory originally pointed to by &lt;code&gt;i_ptr&lt;/code&gt;.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;/*  Never do this - always assign the return value to a different pointer */&lt;br /&gt;   &lt;span style="color:Red;"&gt;i_ptr&lt;/span&gt; = (int *)realloc( &lt;span style="color:Red;"&gt;i_ptr&lt;/span&gt;, sizeof(int)*larger_nelements_wanted );&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt;  A final couple of warnings about pointers. Firstly, the &lt;code&gt;[]&lt;/code&gt; operator has a higher precedence than the *, so &lt;code&gt;int *array[]&lt;/code&gt; means an array of pointers to int, not a pointer to an array of ints. Secondly, the following two statements are not equivalent:  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;extern int  is[]; /* This declares an int array, defined elsewhere */&lt;br /&gt;extern int *is;   /* This declares a pointer to int, defined elsewhere */&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; The compiler will actually generate code you did not intend, and probably cause an ACCVIO if you confuse these. This is because an access via a pointer first looks at the address of the pointer, gets the pointer value stored there, and uses that as the base address for lookups. Access via an array name uses the address of the array itself as the base address for lookups. Draw a diagram if you are confused ! Using the EXT and DEFINE_GLOBALS macros, explained later, should stop this ever happening to you. &lt;/p&gt;  &lt;h2&gt;&lt;a name="Structures"&gt;§7 Structures and Unions&lt;/a&gt;&lt;/h2&gt;   Structures and unions in &lt;span style="color:Blue;"&gt;C&lt;/span&gt; are pretty much like their &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt; counterparts. The general form of a structure declaration is   &lt;code&gt; &lt;/code&gt;&lt;pre&gt;struct optional_structure_identifier {what's in it} optional_instance;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; I suggest that you always specify optional_structure_identifier, then declare the instances of the structure later in a manner similar to the way we used &lt;code&gt;enum&lt;/code&gt;. Example: &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;/*     C Example */                    &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;*     Fortran Example&lt;/span&gt;&lt;br /&gt;                                      &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;br /&gt;struct oscar_location_s {              &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      STRUCTURE /OSCAR_LOCATION_S/&lt;/span&gt;&lt;br /&gt; int x;                               &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;        INTEGER X&lt;/span&gt;&lt;br /&gt; int y;                               &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;        INTEGER Y&lt;/span&gt;&lt;br /&gt;}; /* Note the semicolon ; */          &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      END STRUCTURE&lt;/span&gt;&lt;br /&gt;      .                               &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;           .&lt;/span&gt;&lt;br /&gt;int main( int argc, char *argv[] )     &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;           .&lt;/span&gt;&lt;br /&gt;{                                      &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;           .&lt;/span&gt;&lt;br /&gt;   struct oscar_location_s loc;       &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      RECORD /OSCAR_LOCATION_S/ LOC&lt;/span&gt;&lt;br /&gt;      .                               &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;           .&lt;/span&gt;&lt;br /&gt;   loc.x = 100;                       &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      LOC.X = 100&lt;/span&gt;&lt;br /&gt;   loc.y =  50;                       &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      LOC.Y =  50&lt;/span&gt;&lt;br /&gt;      .                               &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;           .&lt;/span&gt;&lt;br /&gt;}                                      &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;                                                                                   &lt;p&gt; Similarly with unions, the following trivial example shows how they might be declared and used: &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;/*     C Example */                    &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;*     Fortran Example&lt;/span&gt;&lt;br /&gt;                                      &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;br /&gt;union hat_u {                          &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      STRUCTURE /HAT_U/&lt;/span&gt;&lt;br /&gt; int   mileage;                       &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;        UNION&lt;/span&gt;&lt;br /&gt; float hotel_cost;                    &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;          MAP&lt;/span&gt;&lt;br /&gt;};                                     &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;            INTEGER MILEAGE&lt;/span&gt;&lt;br /&gt;      .                               &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;          END MAP&lt;/span&gt;&lt;br /&gt;int main( int argc, char *argv[] )     &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;          MAP&lt;/span&gt;&lt;br /&gt;{                                      &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;            REAL HOTEL_COST&lt;/span&gt;&lt;br /&gt;   int was_tow = 0;                   &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;          END MAP&lt;/span&gt;&lt;br /&gt;   union hat_u cost;                  &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;        END UNION&lt;/span&gt;&lt;br /&gt;      .                               &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      END STRUCTURE&lt;/span&gt;&lt;br /&gt;   if ( was_tow ) {                   &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;           .&lt;/span&gt;&lt;br /&gt;     cost.mileage = 100;              &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      IF ( WAS_TOW ) THEN&lt;/span&gt;&lt;br /&gt;   } else {                           &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;        COST.MILEAGE = 100&lt;/span&gt;&lt;br /&gt;     cost.hotel_cost =  45.50;        &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      ELSE&lt;/span&gt;&lt;br /&gt;   }                                  &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;        COST.HOTEL_COST =  45.50&lt;/span&gt;&lt;br /&gt;      .                               &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;      ENDIF&lt;/span&gt;&lt;br /&gt;      .                               &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;           .&lt;/span&gt;&lt;br /&gt;}                                      &lt;span style="color:Black;"&gt;|&lt;/span&gt;&lt;span style="color:Green;"&gt;           .&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; Notice that you don't need the &lt;span style="color:Green;"&gt;MAP - END MAP&lt;/span&gt; sequence in &lt;span style="color:Blue;"&gt;C&lt;/span&gt; that is used in DEC &lt;span style="color:Green;"&gt;Fortran&lt;/span&gt;. Everything in the &lt;code&gt;union { body }&lt;/code&gt; acts as though it is sandwiched between &lt;span style="color:Green;"&gt;MAP - END MAP&lt;/span&gt;.  &lt;/p&gt; &lt;p&gt;  Structures may contain pointer references to themselves, which is very handy for implementing linked lists: &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;struct list_s {&lt;br /&gt; struct list_s *prev;&lt;br /&gt; struct list_s *next;&lt;br /&gt; void *data_ptr;&lt;br /&gt;};&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; When you declare a pointer to a structure, let's call it &lt;code&gt;p&lt;/code&gt;, there is a potential trap in using the pointer because the binding of the structure member operator, &lt;code&gt;.&lt;/code&gt;, is higher than the * dereference operator. Hence &lt;code&gt;*p.thing&lt;/code&gt; means lookup the member "thing" of &lt;code&gt;p&lt;/code&gt;, and use that as an address for the dereference. What you really want is &lt;code&gt;(*p).thing&lt;/code&gt;. This is a bit ugly, so &lt;span style="color:Blue;"&gt;C&lt;/span&gt; provides the &lt;code&gt;-&gt;&lt;/code&gt; operator.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;     .&lt;br /&gt;   struct my_struct_s my_struct;&lt;br /&gt;   struct my_struct_s *struct_ptr;&lt;br /&gt;     .&lt;br /&gt;   struct_ptr = &amp;amp;my_struct;&lt;br /&gt;   (*struct_ptr).thing = 1; /* "thing" = 1 in struct pointed to by struct_ptr*/&lt;br /&gt;   struct_ptr-&gt;thing = 1;   /* Same as above */&lt;br /&gt;     .&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt; This is good place to introduce a program example kindly provided by Rob Cannings. This uses cunning (Cannings ?) pointer manipulation to create a binary sorted tree.  &lt;/p&gt;  &lt;code&gt; &lt;/code&gt;&lt;pre&gt;&lt;span style="color:Blue;"&gt;&lt;br /&gt;/*---- Illustration of pointer manipulation ("treesort.c") -------------------*/&lt;br /&gt;/* Example provided by Rob Cannings:                                          */&lt;br /&gt;/* (Excess white space removed by Phil O. ;-))                                */&lt;br /&gt;/* We implement a sorting routine with the sorted list stored in a tree.      */&lt;br /&gt;&lt;br /&gt;/* ANSI C Headers */&lt;br /&gt;#include &lt;stdlib.h&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;/* Structures */&lt;br /&gt;struct treeNode {&lt;br /&gt; int data;&lt;br /&gt; struct treeNode *pLeft;&lt;br /&gt; struct treeNode *pRight;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;/* Function prototypes */&lt;br /&gt;void AddNode(struct treeNode **ppNode,struct treeNode *pNewNode);&lt;br /&gt;void Dump(struct treeNode *pNode);&lt;br /&gt;&lt;br /&gt;/* Defines and macros */&lt;br /&gt;#define NUMBER_OF_NUMBERS 4&lt;br /&gt;&lt;br /&gt;/* Main Program starts here */&lt;br /&gt;int main(int argc,char *argv[])&lt;br /&gt;{&lt;br /&gt;   int i = 0;&lt;br /&gt;   int toBeSorted[NUMBER_OF_NUMBERS] = { 93, 27, 15, 47};&lt;br /&gt;   struct treeNode dataNode[NUMBER_OF_NUMBERS];&lt;br /&gt;   struct treeNode *pSortedTree = NULL;&
