├── README.md
└── php-search-all-database.php
/README.md:
--------------------------------------------------------------------------------
1 | # PHP Search All Database
2 | This code can search the entire database, by narrowing down the tables & columns to search in. Giving flexibility and high-performance execution to execute faster searches by lowering the traversing nodes.
3 |
Simple PHP Searching tool
4 | User friendly - Plug and Play
5 | Tiny PHP search engine
6 |
7 | # How to use:
8 |
9 |
10 | Assign keyword to search to variable `$search_keyword`
11 |
12 | ```php
13 | $search_keyword = "KEYWORD"; // Enter keyword to search
14 | ```
15 |
16 | Enter `table names` & their respective `column names` in an associative array `$table_associative_array` to search the `given keyword`.
17 |
18 | ```php
19 | $table_associative_array = array(
20 | 'TABLE NAME 1' => array( // TABLENAME 1 to search in
21 | 'columnN_NAME_A', // column Name A to search in
22 | 'columnN_NAME_B' // column Name B to search in
23 | ),
24 | 'TABLE NAME 2' => array( // TABLENAME 2 to search in
25 | 'columnN_NAME_A', // column Name A to search in
26 | 'columnN_NAME_B' // column Name B to search in
27 | )
28 | );
29 | ```
30 |
31 | Call this fantastic function, with two parameters `php_search_all_database( "Keyword to search", "Table name array" )` as below
32 |
33 | ```php
34 | php_search_all_database( $search_keyword, $table_associative_array ); // call this Awesome function to run script
35 | ```
36 |
37 |
38 |
39 | Please STAR if you like this script.
40 |
--------------------------------------------------------------------------------
/php-search-all-database.php:
--------------------------------------------------------------------------------
1 | array( // TABLENAME 1 to search in
7 | 'columnN_NAME_A', // column Name A to search in
8 | 'columnN_NAME_B' // column Name B to search in
9 | ),
10 | 'TABLE NAME 2' => array( // TABLENAME 2 to search in
11 | 'columnN_NAME_A', // column Name A to search in
12 | 'columnN_NAME_B' // column Name B to search in
13 | )
14 | );
15 |
16 |
17 | php_search_all_database( $search_keyword, $table_associative_array ); // call this Awesome function to run script
18 |
19 |
20 |
21 | function php_search_all_database($search_keyword,$table_associative_array){
22 |
23 | global $conn; // Declared global variable to store database connection
24 |
25 | $db_hostname = 'DATABASE HOST NAME'; // Database hostname (default value: localhost)
26 | $db_username = 'DATABASE USERNAME'; // Database username (default value: root)
27 | $db_password = 'DATABASE PASSWORD'; // Database password
28 | $db_database_name = 'DATABASE NAME'; // Database name
29 |
30 | $conn = mysqli_connect($db_hostname, $db_username, $db_password, $db_database_name); // Establish Database Connection
31 |
32 | if(mysqli_connect_errno()){ // Check if database connection is ok
33 | echo "Failed to connect to MySQL: ".mysqli_connect_error();
34 | }
35 |
36 |
37 | echo "Given Keyword : ".$search_keyword.'
';
38 | echo "Given tables : ".implode($table_associative_array,', ').'
';
39 |
40 |
41 | if(count($table_associative_array) > 0){ // Check weather array of tables names and column names is not empty
42 |
43 | foreach($table_associative_array AS $table_name => $columnn_name){ // Iterate through array of table names
44 |
45 | echo $table_name; // Name of table
46 | echo "
";
47 | echo $columnn_name; // Name of Column in this table
48 | echo "
";
49 |
50 | foreach($columnn_name AS $column){ // Fetch data from array of column names
51 |
52 | $db_search_result_fields = $column." LIKE ('%".$search_keyword."%')"; // We have used wildcards as an example, You can replace as per your need
53 | $db_search_result = $conn->query("SELECT * FROM ".$table_name." WHERE ".$db_search_result_fields);
54 |
55 | if($db_search_result->num_rows > 0){ // Check weather 'keyword' found or not
56 |
57 | echo "Table :".$table_name.'';
58 |
59 | while( $row = $db_search_result->fetch_array() ){ // Fetch final result from records found
60 | $count++;
61 | echo "- Column Name: ".$column."
"; // Respective column Name
62 | echo "- Row: ".$row['ROW ID']."
"; // Primary key of respective table name, For example id/rowId
63 | echo "- Value: ".$row[$column]."
"; // Data stored in respective columns. i.e. The actual keyword we found
64 |
65 | } // End of while loop of final result
66 |
67 | echo "
";
68 |
69 | } // End of if condition to check table data count
70 |
71 | } // End of foreach of data fetching of every column
72 |
73 | echo $table_name." search End's Here
";
74 |
75 | } // End of foreach of data fetching of every table
76 |
77 | } // End of if condition to check empty input data
78 |
79 | } // Awesome function ends
80 |
--------------------------------------------------------------------------------