简体   繁体   English

PHP检查.sql转储文件中是否存在行

[英]PHP check if row exists in .sql dump file

I am wondering how to check if a row exists in a MYSQL .sql dump file without loading it into a database. 我想知道如何检查MYSQL .sql转储文件中是否存在一行而不将其加载到数据库中。 I also wish to check 3 fields. 我也希望检查3个字段。 FieldA,FieldB, FieldC with userinfo1,userinfo2,userinfo3, all of those are strings. 具有userinfo1,userinfo2,userinfo3的FieldA,FieldB,FieldC,所有这些都是字符串。 The filename is database1.sql and is stored on an FTP server. 文件名是database1.sql,存储在FTP服务器上。 Is there a way to do this wiht leaving the file on the other server and not importing it into another database? 有没有一种方法可以将文件保留在另一台服务器上而不将其导入到另一个数据库中?

Ok so first of all there is no way to do any processing on the file without having it on the server where PHP is running. 好的,首先,如果没有在运行PHP的服务器上进行文件处理,就无法对文件进行任何处理。 You can however have a PHP script which logs into the FTP and automatically downloads and processes the file. 但是,您可以具有一个PHP脚本,该脚本可以登录FTP并自动下载和处理文件。

As to the SQL processing, the easiest way is to read the file line by line, first use strpos("INSERT INTO .....) to check for the beginning of the correct query type (insert, update ... whatever it might be and then use something like: 对于SQL处理,最简单的方法是逐行读取文件,首先使用strpos(“ INSERT INTO .....)来检查正确查询类型(插入,更新...)的开头。可能是,然后使用类似:

https://code.google.com/p/php-sql-parser/wiki/ParserManual and go over the generated array and compare the values. https://code.google.com/p/php-sql-parser/wiki/ParserManual并检查生成的数组并比较值。 This is fairly simple and easy to do. 这是相当简单和容易做到的。 However the library comes with some overhead so if speed is an issue or files are very big, you will need some complex preg_match regular expression to get the necessary values and checksum them. 但是,该库会带来一些开销,因此,如果速度是一个问题或文件很大,您将需要一些复杂的preg_match正则表达式来获取必要的值并对它们进行校验和。 After that you can store them in a temporary array and go over this checksums array each time to match whether you already have those values... 之后,您可以将它们存储在一个临时数组中,并每次遍历此校验和数组以匹配您是否已经具有这些值...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM