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! |
Learn field-tested SOA principles, methodology, technology and implementation from the global SOA market leader - in a new e-book by an IBM SOA expert. Written by IBM Certified SOA Solution Designer Bobby Woolf, "Exploring IBM SOA Technology & Practice" is the ultimate insider's guide to SOA - a PDF e-book packed cover to cover with IBM's specific advice on how to make your SOA implementation a success. FREE! Go There Now!
|
|
|
|
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!
|
|
|
|
This whitepaper presents the benefits of successfully introducing static analysis into your organization using IBM Rational Software Analyzer. Additionally, it identifies some common pitfalls that can hinder the effective use of static analysis tooling as well as presents 10 simple strategies designed to help you quickly realize the value of static analysis using Rational Software Analyzer. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. FREE! Go There Now!
|
|
|
|
Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services. FREE! Go There Now!
|
|
|
|
Rational Modeling Extension for Microsoft .NET enhances usability for code generation supporting a more intelligent refactoring. The latest enhancements enable organizations with Java and .NET systems and software development maintain architectural integrity across heterogeneous platforms. 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!
|
|
|
|
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!
|
|
|
|
Join this Rational Talks to You teleconference, to hear how Enterprise Generation Language (EGL) eliminates the need for tedious and error-prone low level coding, so developers can focus on business requirements. EGL extends the Rational software development platform with a simplified programming language that enables developers who have little or no experience with Java, Web technologies or Service Oriented Architecture, to create enterprise-class applications and services quickly and easily. It also allows developers who may have little or no mainframe programming experience to quickly create traditional mainframe components. FREE! Go There Now!
|
|
|
|
The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |