duanqizao6492 2012-01-07 06:19
浏览 66
已采纳

如何使用PHP脚本通过.sql文件更新远程服务器的数据库?

I want to create a utility in PHP like phpMyAdmin's import option, which should allow database updates to the remote server via a .sql file without creating a new database.

Since it's a client side utility, access to cpanel is not allowed.

The app has two kinds of working environments, offline & online.

If the client works offline, they need to take the backup of database and should update the database with remote server similar for online.

Then they have to update the database of remote server.

  • 写回答

4条回答 默认 最新

  • donglanzhan7151 2012-01-07 08:20
    关注

    Solution 1

    If you are running your PHP on a Linux system, you can try using the 'mysql' command itself. However please note that your PHP installation has the permission to run "system" commands, like system(), exec() etc.

    So here is what I mean to say:

    system("mysql -U{db_user_name} -h{db_host} -P{db_password} < {full_path_to_your_sql_file}");
    

    Please replace,

    {db_user_name} with the DB username,

    {db_host} with the DB host,

    {db_password} with the DB password,

    {full_path_to_your_sql_file} with the path to your SQL file.

    And this of course requires the SQL file to be uploaded.

    Solution 2:

    Read the SQL file line by line and while reading execute each statement using PHP's standard MySQL library. Something like:

    $arrFile = file("full_path_to_sql_file.sql", FILE_IGNORE_NEW_LINES);
    foreach ($arrFile as $q) {
        mysql_query($q);
    }
    

    However, this might not be as simple as it seems. If your SQL file has comments and other .sql specific statements, you might need to put checks to ignore them. Or better if the SQL file contains nothing but SQL statements.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示