Location: PHPKode > projects > The Referral Database > referralsdb/example.php
<?php

/* * * * example.mysql.boolean.php * * * * * * * * * * * * * * * * * * * * *
 *
 *	The following file contains sample data to demonstrate the
 *	capability of the functions contained in funcs.mysql.boolean.php:
 *      http://davidaltherr.net/web/php_functions/boolean/funcs.mysql.boolean.txt
 *	To see the example, load the data into MySQL then run this script
 *
 * 	Copyright 2001 David Altherr
 *		hide@address.com
 *		www.davidaltherr.net
 *
 *	All material granted free for use under MIT general public license
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */



/* * * * Example Implementation * * * * * * * * * * * * * * * * * * * * *

   // MySQL Database //

	// architecture //
	CREATE DATABASE news;
	USE news;

	CREATE TABLE quotes (
		id int(9) NOT NULL auto_increment,
		author varchar(255),
		content text,
		PRIMARY KEY  (id),
		UNIQUE KEY id (id),
		FULLTEXT KEY author (author,content)
	) TYPE=MyISAM;

	// data //
	INSERT INTO quotes VALUES(
		10000,'George Stephanopolous',
		'The President has kept all the promises he intended to keep.');
	INSERT INTO quotes VALUES(
		10001,'Dan Quayle',
		'It is wonderful to be here in the great state of Chicago.');
	INSERT INTO quotes VALUES(
		10002,'Marion Barry',
		'Outside of the killings, Washington has one of the lowest crime rates in the country.');
	INSERT INTO quotes VALUES(
		10003,'David Dinkins',
		'I haven\'t committed a crime. What I did was fail to comply with the law.');
	INSERT INTO quotes VALUES(
		10004,'Dan Quayle',
		'It isn\'t pollution that\'s harming the environment. It\'s the impurities in our air and water that are doing it.');
	INSERT INTO quotes VALUES(
		10005,'George Dubya',
		'One word sums up probably the responsibility of any Governor, and that one word is \' to be prepared \'.');
	INSERT INTO quotes VALUES(
		10006,'George Dubya',
		'The most important job is not to be Governor, or First Lady in my case.');


 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


    // PHP Script //

	// form input //
	$table_name = 'news.quotes';
	$search_string = 'george (governor,president) -responsibility';

	// database connection //
	$db_host = 'localhost';
	$db_user = 'root';
	$db_pwd = 'test';
	$db_connect = mysql_connect($db_host,$db_user,$db_pwd) or die(mysql_error());

	// sql construction //
	require_once('funcs.mysql.boolean.php');
	$fulltext_key = get_fulltext_key($table_name,$db_connect);
	$sql =  "SELECT id, author, content, \n"
		.boolean_sql_select(
			boolean_inclusive_atoms($search_string),
			$fulltext_key)." as relevance \n"
		."FROM $table_name \n"
		."WHERE \n"
		.boolean_sql_where($search_string,$fulltext_key)." \n"
		."HAVING relevance>0 \n"
		."ORDER BY relevance DESC \n";

	// data query //
	$result = mysql_query($sql,$db_connect) or die(mysql_error());
	$result_rows = mysql_num_rows($result);

	// get results //
	$output = "
		<table border=1>
		 <thead>
		  <tr>
		   <th>id</th>
		   <th>author</th>
		   <th>content</th>
		   <th>relevance</th>
		  </tr>
		 </thead>
		 <tbody>";
	for($ith=0;$ith<$result_rows;$ith++){
		$ir=mysql_fetch_row($result);
		$output .= "
			<tr>
			 <td>$ir[0] </td>
			 <td> $ir[1] </td>
			 <td> $ir[2] </td>
			 <td> $ir[3]</td>
			</tr>\n";
	}
	$output .= "
		 </tbody>
		</table>\n";

	// get user readable statement //
	$parsed_as = boolean_parsed_as($search_string);

	// display process //
	echo 	"<h5>Input Statement</h5>\n"
		."<p>$search_string</p>\n"
		."<h5>Parsed As</h5>\n"
		."<p>$parsed_as</p>\n"
		."<h5>SQL Generated</h5>\n"
		."<p>".nl2br($sql)."</p>\n"
		."<h5>Query Results</h5>\n"
		."<p>$output</p>\n";

?>
Return current item: The Referral Database