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 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line