0
我'寻找中!PHP的工具!这将返回可能是一个数组或只是一些东西,我可以查找是否有任何更改。比较工具输出1/0 //是/否
其beeing用于把它变成一个JSON文件从MySQL中获取数据,如果JSON文件从MySQL输出不同,那么这个文件应该是新生成的。
所以我只需要键入一个函数或别的东西,如果有已经做出改变,(是/否)做十
难道你们知道的任何工具,将与我的想法工作?
谢谢你帮我。
我'寻找中!PHP的工具!这将返回可能是一个数组或只是一些东西,我可以查找是否有任何更改。比较工具输出1/0 //是/否
其beeing用于把它变成一个JSON文件从MySQL中获取数据,如果JSON文件从MySQL输出不同,那么这个文件应该是新生成的。
所以我只需要键入一个函数或别的东西,如果有已经做出改变,(是/否)做十
难道你们知道的任何工具,将与我的想法工作?
谢谢你帮我。
你的问题是模糊的,但如果我的理解没错,这里是一个想法:
<?php
class Tool {
var $db; // you will have to provide a valid db connection
function __construct (& $db) {
$this->db = $db;
}
public function execute ($query, $file){
$arr = $this->fetch($query);
$dbJSON = json_encode($arr);
$fileJSON = file_get_contents($file);
if(md5($dbJSON) != md5($fileJSON)){
$this->write($dbJSON, $file);
}
}
protected function fetch($sql) {
$arr = array();
$result = $this->db->query($sql);
if($result){
// Cycle through results
while ($row = $result->fetch_object()){
$arr[] = $row;
}
// Free result set
$result->close();
}
return $arr;
}
protected function write ($content, $file) {
$fp = fopen('w+', $file);
fwrite($fp, $content);
fclose($fp);
}
}
>
和用例:
<?php
$db = new mysqli('localhost','user','pass','database');
// Check for errors
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}else{
$tool = new Tool($db);
$tool->execute('SELECT * FROM users','/com/user.json');
}
?>
如果条件就可以使用。我认为如果条件允许,你可以这样做 –