IPT home IPT A Virtual Approach by Peter Whitehouse
Quick Links:
PHP home
Information and Intelligent Systems Social and Ethical Implications Human Computer Interaction Software and Systems Engineering eXercise Files Course Outline and Assessment A-Z of Geeky Acronyms Terrace Work Program 2004 Sillybus FAQ = Frequently Asked Questions Help


Useful Code Snippets

PHP is a very particular language - missing punctuation and mis-placed commands can seriously impact on it's ability to do anything.

Rather than expect students to type out code from scratch, below are some useful templates that can be copy-pasted when needed. The BLUE bits need customization by you.

connect | issue a DDL command | get data (raw)
get data in table | don't cache | datestamp | other stuff

/* set the connections to the database */
$host     = 'localhost';
$username = 'enter username here';
$passwd   = 'enter password here';
$dbname   = 'enter the database name here';

/* build a bridge to the server */
$connection = mysqli_connect($host, $username, $passwd) or die('problems connecting');

/* connect to the database through that bridge */
mysqli_select_db($connection,$dbname) or die ('problems finding the database');

/* connect to the database first */
require ('insert filename of connect script file here');

/* issue command */
if (mysqli_query($connection,'insert DDL command here'))
   { echo 'insert success message here'; }
   { echo 'insert fail message here - '.mysqli_error(); }

require ('insert filename of connect here');
$thequery = 'type your query here';
/* attempt to retrieve the data */

$answertable = mysqli_query($connection,$thequery);
if (mysqli_num_rows($answertable)==0)
  echo 'insert empty answer table message here';
  while (list($col1, $col2,...) = mysqli_fetch_row($answertable))
      echo $col1 . ', ';
      echo $col2 . ', ';
      echo '<br>';


This again is an authenticated action (hence the need for the connect script). You put your query inbetween the quotes (I use SINGLE quotes as the container so you are able to use DOUBLE QUOTES in your query should you need to).
The WHILE(LIST section requires modification to ensure there are the same number of variables as there are COLUMNS in your answer table, thus allowing you to access all retrieved values one row at a time.
I like WHILE because it does not rely on knowing the number of rows coming back, it grinds away until it runs out of rows and then stops without error.
The above example gives a fairly primitive output, data is not aligned but you can surround it in the HTML you want to make it "pretty".

   echo '<table cellpadding="5" border="1" cellspacing="0">';
   while (list($col1, $col2, $col3, ...) = mysqli_fetch_row($answertable))
      echo '<tr>';
      echo '<td>'. $col1 . '</td>';
      echo '<td>'. $col2 . '</td>';
      echo '<td>'. $col3 . '</td>';
      echo '</tr>'; 
   echo '</table>';

note: take note of the careful use of single and double quotes

mysqli_num_rows($answertable) returns the number of rows returned


  <meta http-equiv="pragma" content="no-cache">         
    //put that in the <head> </head> secton

    //prepare a datetimestamp
    $rightnow = date('Y-m-d H:i:s');





©Copyright t 1992..2017+. Edition 25.150117
Creative Commons License
This work is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 2.1 Australia License