Iterate through the SQL and project files, querying the file for table name occurrences

Source: Internet
Author: User

Recently in the Project database content, there are two problems to solve, one problem is that some tables have not been run in the file, I need to filter out, a problem is still in use of those tables, in which files are running?

The data sheet I counted, there were 200 +. The controller file of the project, of course, is two folders, one about the foreground, one about the backstage.

And then, logically, go through the SQL file, take out all the table names, and run through each of the table names in two folders.

To write this program, there are three heads that are essential:

Header ("content-type:text/html; Charset=utf-8 "); error_reporting (0); set_time_limit (0);

In fact, usually wait until there is a problem, plus the three head of the ...

First of all, it is very easy to walk through the SQL file and remove the table name, but the regular expression is not good enough for me to use the Str_replace function instead.

    function sqlarr ($mdir)     {          $str =file_get_contents ($mdir);         if (! $str)         {             echo  ' <script> ';echo  ' alert (\ ' file path does not exist!) \‘);‘; echo  ' </script> ';return false;        }          $pattern  =  '/table Structure  \ '. *\ '/isu ';         preg_match_all ($pattern,  $str,  $match);          $match  = array_unique ($match [0]);         static   $arr  = array ();        foreach  ($match  as   $key  =>  $value)          {             $value  =  Str_replace ("'", "", $value);             $value  = str_replace ("Table structure  ", "", $value);              $v 2 = str_replace ("Head_", "", $value);             //cannot be an error file with a date             $ w =  '/-/isu ';             preg_match_all ( $w, $v 2, $mm);                 if   (! $mm [0])  {                      $arr [] =  $v 2;                 }        }       return  $arr;     }

$mdir is the path of the SQL file, because some of the date of the error file, I also filtered out, it is worth mentioning, this is phpmyadmin the SQL file, it is generally possible to find all the table name in this method.

Then you loop through the returned array, and each value iterates over the file.

$MSQLARR = Sqlarr (' URL ');        foreach ($mSqlArr as $k = + $v) {$k + +; Echo ' <br/><br/><b><font color= ' green > ' $v. ' ('. $k. ') </font></b><br/>        <br/> ';    Listdir ("./", $v); }

Listdir () is the traversal function. In this case, the sorting of the table name and table name is output. For more than 200 table names, this query takes a long time and the program needs to run for at least more than 10 minutes.

The next step is to listdir this recursive function, this is nothing to say, write to debug it, my code is this:

Function listdir ($dir, $keyword) {//Front desk $arra = array ();//valid file $arrb = array () in background; $a _ files =  ", $b _files = ";   if (Is_dir ($dir))     {       if  ($dh  = opendir ($dir))      {           while  ($file  = readdir ($DH))  !==  False)       {          if ((is_ Dir ($dir. " /". $file)"  &&  $file! = "."  &&  $file! = "...")         {            if ($file  ==  ' Action ') echo  "

This function is to let the table name into two folders to loop, output the name of the file containing the table, $arrA and $ARRB is to him to determine which are valid files (some files I know obsolete).

As seen from the path, this is the directory structure of the thinkphp, which is the folder where the controller is placed.

The first parameter is the path, and the second argument is the keyword, which is the table name.

Finally, I wrote a code that could simply query the status of a single table name.

$keyword = isset ($_post[' keyword ') "$_post[' keyword ']: ';    All lowercase, remove spaces $keyword = Str_replace (",", Strtolower ($keyword)); Echo ' <form method= "post" action= ". Getcururl (). ' Style= ' position:fixed;top:100px;right:200px;    ><input type= "text" name= "keyword"/><input type= "submit" value= "Search table name"/></form> "; echo ' Search for Table name: '. $keyword. '    <br/> '; if (! $keyword) {echo ' <script> '; echo ' alert (\ ' cannot be null \ '); ';    Echo ' </script> '; return false;} Start running Listdir ("./", $keyword);

Here is a function to get the current domain name:

    function getcururl ()     {         if (!empty ($_server["Request_uri"))         {              $scriptName  = $_server["Request_uri"];              $nowurl  =  $scriptName;         }        else         {            $ scriptname = $_server["Php_self"];             if (Empty ($_server["query_string"))              {               $nowurl  =   $scriptName;             }             else            {                $nowurl  = $ ScriptName. "?". $_server["Query_string"];            }         }        return  $nowurl;     }

Finally, it's done.

This article is from the "Flying Potato God teach" blog, please be sure to keep this source http://1105190775.blog.51cto.com/10048144/1871492

Iterate through the SQL and project files, querying the file for table name occurrences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.