PHP
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 | other stuff
<?php
/* 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 = mysql_connect($host, $username, $passwd) or die("problems connecting message");
/* connect to the database through that bridge */
mysql_select_db($dbname) or die ("problems finding the database messagee");
?>
<?php
/* connect to the database first */
require ("insert filename of connect script file here");
/* issue command */
if (mysql_query("insert DDL command here"))
{ echo "insert success message here"; }
else
{ echo "insert fail message here - ".mysql_error(); }
?>
<?php
require ("insert filename of connect here");
$thequery = "type your query here";
/* attempt to retrieve the data */
$answertable = mysql_query($thequery, $connection);
if (!$answertable)
{
echo "insert empty answer table message here";
}
else
{
while (list($col1, $col2,...) = mysql_fetch_row($answertable))
{
echo $col1 . ", ";
echo $col2 . ", ";
etc...
echo "<br>";
}
}
?>
echo "<table cellpadding=5 border=1 cellspacing=0>";
while (list($col1, $col2, $col3, ...) = mysql_fetch_row($answertable))
{
echo "<tr>";
echo "<td>". $col1 . "</td>";
echo "<td>". $col2 . "</td>";
echo "<td>". $col3 . "</td>";
etc...
echo "</tr>";
}
echo "</table>";
<meta http-equiv="pragma" content="no-cache">
//put that in the <head> </head> secton
|