PHP Classes

File: test.php

Recommend this page to a friend!
  Classes of Paul Williamson   Simple SQL   test.php   Download  
File: test.php
Role: Example script
Content type: text/plain
Description: A Full test script and setup
Class: Simple SQL
MySQL wrapper to shortcut common tasks execution
Author: By
Last change: contents
Date: 20 years ago
Size: 3,971 bytes
 

Contents

Class file image Download
<?php
/*** EXAMPLE SCRIPT ***/
# Sample program to grab and print content from a database
# This script is an example of how to list contents in a database
# The theme is car models, a setup query is provided.
include('SimpleSQL.class.php');

//Name of database
$db_name="test_db";

/*** CONSTRUCT CLASS ***/
# Following code will setup your database connection varables
## Note: these variables can be set inside the class file
## Arguments:
#### db_name Name of Database we're connecting to
#### db_server Server we're connecting to
#### db_username Username to authincate
#### db_password Password to authincate
$SimpleSQL=new SimpleSQL($db_name,"localhost","root","");

/*** SETUP DATABASE ***/
$setup_db="USE $db_name;

CREATE TABLE `model` (
  `modelid` int(10) NOT NULL auto_increment,
  `modelname` varchar(255) NOT NULL default '',
  `active` tinyint(1) NOT NULL default '0',
  `location` varchar(255) NOT NULL default '',
  `content` varchar(255) NOT NULL default '',
  PRIMARY KEY (`modelid`)
) TYPE=MyISAM AUTO_INCREMENT=7 ;

INSERT INTO `model` VALUES (1, 'Thunderbird 4100', 0, 'Williamsburg, VA', 'http://www.google.com/');
INSERT INTO `model` VALUES (2, 'Dodge Viper', 1, 'Richmond, VA', 'http://www.google.com/');
INSERT INTO `model` VALUES (3, 'Range Rover', 1, 'Reston, VA', 'http://www.google.com/');
INSERT INTO `model` VALUES (4, 'Ford F150', 1, 'Farmville, VA', 'http://www.google.com/');
INSERT INTO `model` VALUES (5, 'Toyota Corral', 0, 'Richmond, VA', 'http://www.google.com/');
INSERT INTO `model` VALUES (6, 'BMW Z3', 1, 'Richmond, VA', 'http://www.google.com/');"
;

$SimpleSQL->_query($setup_db);
$SimpleSQL->_kill();

/*** GET_CONTENT ***/
# Following code will retrieve data from the database
## Arguments:
#### db_table Table being queried
#### db_where WHERE clause
###### The WHERE clause will work like this:
###### WHERE active='1'
###### This will grab all rows from the database where active is = 1
###### Note: you may ommit typing WHERE, the script will fix incorrect
###### SQL syntax for you.
#### db_order ORDER BY clause
###### The ORDER BY clause will work like this:
###### ORDER BY modelname
###### This will order the result alphabetically by modelname
###### Note: you may ommit typing ORDER BY, the script will fix incorrect
###### SQL syntax for you.
$SimpleSQL->get_content("model","WHERE active='1'","ORDER BY modelname");

/*** CREATE RESULT OUTPUT ***/
# Following code will generate your output with hyperlinks as content
## Note: The result of the get_content() function is stored inside the
## public variable 'result'; to access this content, we will treat it
## as an array. The array of resutls works like this:
#### $SimpleSQL->result[3]['modelname']
## The first array index is index number from the query result
## So the output of the above PHP will be the modelname for row 4 of the result query
## The second array index is the column data from the first index result
## Here is the example of how to print out the data in the format you requested
?>
<table border=1>
<tr>
<th>modelid</th>
<th>modelname</th>
<th>active</th>
<th>location</th>
<th>content</th>
</tr>
<?
foreach($SimpleSQL->result as $row)
{
    echo
"<tr>";
    echo
"<td>".$row['modelid']."</td>";
   
# The next echo statment will be a link with column 'content' being it's href
   
echo "<td><a href=\"".$row['content']."\">".$row['modelname']."</a></td>";
    echo
"<td>".$row['active']."</td>";
    echo
"<td>".$row['location']."</td>";
    echo
"<td>".$row['content']."</td>";
    echo
"</tr>";
}
# The above code loops through each FIRST array index
# The $row varable now can be used to grab different column content.
# Hope this all makes since now. Only about 3 real lines of PHP was required
# to do a complictated > 5 lines of mysql functions!
# If you have any more questions, please e-mail me @ webmaster@protonage.net
?>
</table>