Take Current Snapshot of DB and send it to FTP in same PHP Scripts: Advice Needed
Not sure if I can do it this way. I want to get current snapshot of the database and send it via FTP Server, both of this functionality should be implemented in PHP scripts.
Here are the steps I am thinking on right now.
In my php scripts(basically am extending an PDO into my Dao class and then preparing the query),
$qry = SELECT * FROM MyTablename;
$stmt = $this->prepare($qry);
$stmt = $this->execute();
Now I will store $stmt
in csv file using fputcsv
or I will execute the sql
command from the script itself and than try to store the result in the $file(csv file)
note here that I do not have any csv file with me at this point to basically I will have to create one and let's say its $file
, so then
$file = fputcsv($stmt); or $file = exec("Select * from MyTablename");
Will this put all records in the file ? If yes, then I will use FTP Functionality to transfer file to the FTP Folder.
I am not sure if this approach would work and also have concerns regarding the need of preparing the $qry
Any suggestions or different approach advised would be highl开发者_如何学Goy appreciated.
Thanks !!!
try something like this:
mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$list = array();
$q = mysql_query("SELECT * from table");
while($row = mysql_fetch_assoc($q)){
$list[] = $row;
}
$fp = fopen("file.csv", 'w+');
foreach ($list as $line) {
fputcsv ($fp, implode(',', $line));
}
then try:
$conn_id = ftpconnect('host',21);
$login_result = ftp_login($conn_id, 'user_name', 'user_pass');
ftp_fput($conn_id, 'file.csv', $fp, FTP_ASCII);
ftp_close($conn_id);
fclose($fp);
I write the code without testing it but should be something like that ;)
good luck.
精彩评论