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! |
You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months. FREE! Go There Now!
|
|
|
|
Build secure Web services with transport-level security using IBM Rational Application Developer V7 and IBM WebSphere Application Server V6.1. Follow this three-part series for step-by-step instructions about how to develop Web services and clients, configure HTTP basic authentication, and configure HTTP over SSL (HTTPS). This first part of the series walks you through building a Web service for a simple calculator application. You generate and test two different types of Web services clients: a Java Platform, Enterprise Edition (Java EE) client and a stand-alone Java client. You also handle user-defined exceptions in Web services. FREE! Go There Now!
|
|
|
|
This tutorial shows new users of IBM WebSphere Business Monitor Version 6.0.2 how to perform the "Hello World" equivalent for monitoring business process applications. It is intended to help you get familiar with the capabilities of the product. FREE! Go There Now!
|
|
|
|
Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered! FREE! Go There Now!
|
|
|
|
Join this webcast to discover the key requirements for successful change and release management. Learn how to extend your .NET environment to improve productivity and collaboration, and address core problems afflicting team development. In this webcast, we’ll review typical challenges faced by customers and how to resolve them with the IBM Rational Change and Release Management solution, including Rational ClearCase, Rational ClearQuest and Rational Build Forge. Replay is available for 9 months. FREE! Go There Now!
|
|
|
|
As organizations have grown increasingly dependent on online software, the risk of malicious attacks has also become far more serious. Fortunately, well-governed organizations can protect their Web applications by injecting vulnerability assessments and ethical hacks into their software development and delivery processes. This paper describes 12 of the most common hacker attacks and provides basic rules that you can follow to help create more hack-resistant Web applications. FREE! Go There Now!
|
|
|
|
You can now evaluate IBM Rational Asset Manager V7.0 online without installing or configuring it on your own system! Rational Asset Manager helps create, modify, govern, find, and reuse any type of development assets, including SOA and systems development assets. Rational Asset Manager helps you reduce software development costs and improve quality by facilitating the reuse of all types of software development-related assets. Visit developerWorks to learn more about this product and register to explore its capabilities online. FREE! Go There Now!
|
|
|
|
Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, where he will overview Rational’s new offerings and programs to help customers accelerate software innovation on System z. He will discuss how these solutions help organizations extend their core business processes toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
|
|
|
|
With IBM Rational Systems Development Solution, you can deliver products faster with higher quality. Within this kit, Read the “Model Driven Systems Development” white paper to see how to improve product quality and communication. Then check out the rest of the e-Kit to learn more about important topics that can affect the success of any software project through customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems. From start to finish, at every stage in your projects, Rational Systems Development Solution can help your company reach its full potential. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |