这里有一个mysql noob的位置我需要一个脚本,我将每晚用cron作业运行一次比较2个表并根据“House_number”和“postcode”列查找匹配 将识别匹配项,然后生成文本文件。 理想情况下,我希望通过sendmail或equivelant通过电子邮件发送给我。
这两个表名为 USER_TABLE business_table
提前致谢
答案 0 :(得分:0)
因为你是一个菜鸟,需要一些帮助我把它放在一起给你,请注意这个尚未经过测试,而且在优化和重新使用方面它非常粗糙但是这样的事情,它将取决于你测试:))
<?php
//store this in a seperate file or leave it here
$databaseDetails = array(
'host' => 'localhost',
'dbName' => 'databaseName',
'username' => 'user',
'password' => 'password'
);
//instantiate our final array
$houseArr = array();
try {
//connection using PDO (very secure)
$conn = new PDO('mysql:host='.$databaseDetails['host'].';dbname='.$databaseDetails['dbname'], $databaseDetails['username'], $databaseDetails['password']);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//This commented out section is to show you how to prepare a query
/*
$prepare = $conn->prepare('SELECT house_number, post_code FROM user_table WHERE created = :date');
$prepare->execute(array(
'date' => date('Y-m-d H:i:s', strtotime('Now'))
));
*/
//query for users
$data = $conn->query('SELECT id, house_number, post_code /*Additional fields here*/ FROM user_table');
//this will create an array of data that i will explain below
foreach($data as $row) {
$houseArr[$row['house_number']]['users'][$row['id']] = $row['post_code'];
}
//query for business
$data = $conn->query('SELECT id, house_number, post_code /*Additional fields here*/ FROM business_table');
//this will create an array of data that i will explain below
foreach($data as $row) {
$houseArr[$row['house_number']]['business'][$row['id']] = $row['post_code'];
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
exit();
}
if(!empty($houseArr)) {
/*
* the houseArr will have house_number as an index, then it will have multiple entries under either users or business
* for example
* user house 43 and postcode 7800 with database id 1900
* user house 43 and postcode 7801 with database id 1950
* business house 43 and postcode 7800 with database id 2000
* business house 43 and postcode 7801 with database id 2050
* user house 42 and postcode 5400 with database id 2100
* business house 42 and postcode 7600 with database id 2200
*
* the array will spit out
* array(
* 43 => This is where the house number will be
* 'user' => This is where all the users with house number x will be
* '1900' => '7800', This is a key pair value of the id of the user and the post code they entered
* '1910' => '7801'
* 'business' => This is where all the businesses will be with house number x
* '2000' => '7800', This is a key pair value of the id of the business and the post code they entered
* '2050' => '7801'
* 42 =>
* 'user' =>
* '2100' => 5400,
* 'business' =>
* '2200' => 7600
* )
*
*/
//this will write the txt file
file_put_contents('address.txt', print_r($houseArr, true));
}
?>