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! |
Join us for this on demand webcast to learn about developing complex systems more quickly and efficiently. We'll cover market drivers for developing, governing and reusing systems software assets and how you can develop system software assets with Rational Asset Manager. 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!
|
|
|
|
Secure your Web applications with IBM Rational AppScan Standard Edition V7.7, previously known as Watchfire AppScan. This Web application security testing tool automates vulnerability assessments and scans and tests for common Web application vulnerabilities. Visit IBM developerWorks to download a free trial of IBM Rational AppScan Standard Edition V7.7. FREE! Go There Now!
|
|
|
|
As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br /> FREE! Go There Now!
|
|
|
|
Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services. FREE! Go There Now!
|
|
|
|
Whether you are creating new applications or modifying existing ones, managing integration of new components with traditional z/OS elements is a critical part of building and deploying modern applications. Listen to this webcast to see how IBM can help you optimize your development process using an IDE like Rational Developer for System z that integrates with management tools, such as ClearCase to manage your application development on mainframes. FREE! Go There Now!
|
|
|
|
In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications. FREE! Go There Now!
|
|
|
|
User communities play an important role in communication and collaboration around products, solutions and other areas of special interest to members. Successful communities are able to provide the right mix of content and services to deliver a value proposition that resonates with each audience. Join Tom Inman, VP of Marketing for Information and Platform Solutions as he introduces the new LeverageINFORMATION community. During this webcast, learn about the value provided by the community and how customers and partners derive value from the community in addressing their own technical and business challenges. FREE! Go There Now!
|
|
|
|
Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |