db_search class can be scaled for any database system you might have.
By : smckone
//============================================
// NEWS
**future updates**
1) porting to other databases like postgreSQL and MSSQL
2) function to tie searches together (may be a diffrent script)
//============================================
// Function List
void = load_properties((string user_name,string password,string host_add,string database_ name);
void = connect(void);
// note the column can be an array when the table is a string
// this allows for searchs of more than one column in a table
// seting the column name to ALL_COLUMNS will make the script
// search all the columns of that table (if sensitive information
// is in that table do not do this).
void = load_tables(string table_name,string column_name||array);
void = load_tables(array table_names,array column_names||array);
void = add_table(string table_name,string column_name||array);
void = add_table(array table_names,array column_names||array);
void = all_columns(int table_num);
void = all_columns(string table_name);
void = exec(string search_text);
void = free_results(void);
void = close(void);
array = get_row(void);
array = get_exact_row(int row_number);
int = num_rows(void);
void = delete_row(int row_number);
//============================================
// sample of use
$search = new db_search("user name","password","sql.php.net","this site");
$tables = array("name","forum","text");
$columns = array("column","column","body");
$search->load_tables($tables,$columns);
$search->add_table("another","column");
$search->exec("search for");
$i = 0;
while($i < $search->num_rows()){
$row = $search->get_row();
for($n = 0;$n < count($row);$n++){
echo "$row[$n] ";
}
$i++;
echo "<br>";
}
// complex use of db_search
$search = new db_search("dev","not gonna tell you","","devsite");
$tables = array("users","articles");
$s = array("body","subject");
$columns = array("body","body",);
$search->load_tables($tables,$columns);
$search->add_table("forum_topic",$s);
$search->exec("php");
echo $search->num_rows();
<?php
//=======================================================
// db_search class can be scaled for any database system
// you might have.
// (Please send any updates to sam@evilwalrus.com)
//
// Sam McKone
// Oct. 2001
//========================================================
class db_search{
//=======================================================
// data members
//=======================================================
var $tables = "";
var $field = "";
var $results = "";
var $result_loc = 0;
var $host = "";
var $user_name = "";
var $pass_word = "";
var $data_base = "";
var $connection = "";
//=======================================================
// constructors
//=======================================================
function db_search($set_user,$set_pass,$set_host,$set_db_name){
$this->load_properties($set_user,$set_pass,$set_host,$set_db_name);
$this->connect(); // start the mySQL connection
}
//=======================================================
// Connection and initialization functions
//=======================================================
function load_properties($set_user,$set_pass,$set_host,$set_db_name){
$this->user_name = $set_user;
$this->pass_word = $set_pass;
$this->data_base = $set_db_name;
$this->host = $set_host;
}
function connect(){
if(!$this->host){
$this->error("Host was not set");
}elseif(!$this->user_name){
$this->error("User was not set");
}elseif(!$this->pass_word){
$this->error("Password was not set");
}
$this->connection = mysql_connect($this->host,
$this->user_name,$this->pass_word);
if(!$this->connection){
$this->error("Connection could not be established");
}
}
//=========================================================
// Table functions
//=========================================================
// warning running load_tables function more than once
// will overwrite prevously entered tables, to add another
// table use the add_table function
function load_tables($set_tables,$set_field){
$this->tables = $set_tables;
$this->field = $set_field;
$this->scan_tables();
}
function add_table($new_table,$set_field){
if(is_array($new_table) AND is_array($set_field)){
$i = count($this->tables);
for($j = 0;$j < count($new_table);$j++,$i++){
$this->tables[$i] = $new_table[$j];
$this->field[$i] = $set_field[$j];
}
}elseif(is_string($new_table)){
$i = count($this->tables);
$this->tables[$i] = $new_table;
$this->field[$i] = $set_field;
}else{
$this->error("Pass a string or an array of strings");
}
$this->scan_tables();
}
function all_columns($table){
if(is_string($table)){
$temp = $table;
$table = 0;
while($this->tables[$table] != $temp && $table < count($this->tables)){
$table++;
}
}
if($table >= count($this->tables)){
// if table was not yet entered
$this->error("table <b>".$temp."</b> is not in the records.");
return false;
}
if($this->connection){
$i = 0;
$query = "SHOW FIELDS FROM ".$this->tables[$table].";";
$tempory = mysql_db_query($this->data_base,$query,$this->connection);
$this->field[$table] = array();
while($row = mysql_fetch_assoc($tempory)){
$this->field[$table][$i] = $row[Field];
$i++;
}
}else{
$this->error("function <b>all_columns()</b> can only be run after "
."a connection is made.");
}
}
function scan_tables(){
for($i = 0;$i < count($this->tables);$i++){
if($this->field[$i] == "ALL_COLUMNS"){
$this->all_columns($i);
}
}
}
//=========================================================
// exacution functions
//=========================================================
function exec($search_string){
if($this->connection){
if($this->results){
$loc = count($this->results);
}else{
$loc = 0;
}
for($i = 0; $i < count($this->tables);$i++){
if(is_array($this->field[$i])){
$query = "SELECT * FROM ".$this->tables[$i]." WHERE ";
$query .= $this->field[$i][0];
$query .= " LIKE '%$search_string%'";
for($n = 1;$n < count($this->field[$i]);$n++){
$query .= " OR ".$this->field[$i][$n];
$query .= " LIKE '%$search_string%'";
}
}else{
$query = "SELECT * FROM ".$this->tables[$i]." WHERE ";
$query .= $this->field[$i]." ";
$query .= "LIKE '%$search_string%';";
}
$tempory = mysql_db_query($this->data_base,$query,$this->connection);
while($row = mysql_fetch_array($tempory)){
$this->results[$loc] = $row;
$loc++;
}
}
}else{
$this->error("function <b>exec()</b> can only be run after "
."a connection is made.");
}
}
//=========================================================
// memory release functions
//=========================================================
function close(){
$this->tables = "";
$this->field = "";
$this->host ="";
$this->user_name = "";
$this->pass_word = "";
$this->free_results();
$this->data_base = "";
$this->connection = "";
}
function free_results(){
$this->results = "";
$this->result_loc = 0;
}
//=======================================================
// Result functions
//=======================================================
function set_row_pos($pos){
if($pos <= count($this->results)){
$this->result_loc = $pos;
}
}
function get_row(){
return $this->results[$this->result_loc++];
}
function get_exact_row($row_num){
return $this->results[$row_num];
}
function num_rows(){
$count = 0;
while($this->results[$count])$count++;
return $count;
}
// Warning deleting rows will change the row order.
function delete_row($row_num){
for(;$row_num < count($this->results);$row_num++){
$this->results[$row_num] = $this->results[$row_num + 1];
}
$this->results[$row_num] = 0;
}
//=======================================================
// Internal functions
//=======================================================
function error($error_text){
echo $error_text . "<br>";
}
}
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Search Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM DB2 9.5 for Linux, UNIX, and Windows. DB2 9 is the result of a five-year development project that transformed traditional (static) database technology into an interactive data server that merges the high performance and ease of use of DB2 with the self-describing benefits of XML. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM Rational Developer for System i V7.1, which provides a complete development environment for traditional i5/OS application development. IBM Rational Developer for System i is a new eclipse-based workstation offering for i5/OS application development that provides a comprehensive Integrated Development Environment for edit/compile/debug of traditional RPG/COBOL/C/C++ i5/OS applications. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered! FREE! Go There Now!
|
|
|
|
This whitepaper provides areas to consider when evaluating any software configuration management solution. It addresses how the IBM solutions (Rational ClearCase and Rational ClearQuest) meet the needs and requirements of both project leaders and developers to provide successful Software Change and Configuration Management. FREE! Go There Now!
|
|
|
|
Regression testing -- in which code is thoroughly tested to ensure that changes have not produced unexpected results -- is an important part of any development process. But many testing environments neglect the terminal-based applications that still form the backbone of many industries. In this tutorial, you'll learn how the Rational Functional Tester Extension for Terminal-Based Applications works with other Rational Functional Tester to help test terminal-based applications quickly and easily. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Performance Tester V7.0.1, a load and performance testing solution for teams concerned about the scalability of their Web-based applications. Combining multiple ease-of-use features with granular detail, Rational Performance Tester simplifies the test-creation, load-generation and data-collection processes that help teams ensure the ability of their applications to accommodate required user loads. FREE! Go There Now!
|
|
|
|
Join the IBM Watchfire team for an informative discussion on techniques and best practices to proactively manage Web application security and how to effectively build application security testing into the software development lifecycle (SDLC). In this Software Delivery Platform webcast you will learn: How to better understand potential web application security vulnerabilities, best practices and how to effectively integrate application security testing into the software development lifecycle, the importance of detecting and removing software vulnerabilities during application development. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |