dtxs9017 2010-03-15 14:48
浏览 51
已采纳

获取数据库的当前快照,并在相同的PHP脚本中将其发送到FTP:需要建议

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 highly appreciated.

Thanks !!!

  • 写回答

1条回答 默认 最新

  • doushang8846 2010-03-16 16:37
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突