We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • I ran into an issue with a deployment this week where the target server would not let me have direct access to the server through SSH nor direct access to the database. In fact I don't even have permission to change file or folder permissions (REALLY basic FTP access and a remote SQL log in is all I would be given). I had to figure out an efficient way of populating my MODX database with the Dev site tables via a php script.

    First, I installed Databasebackup add on to get a full backup of the target database, in case any table came in corrupt. This tool is pretty cool as it does a full dump as well as dumps each table into a .sql file.

    Now, it was time to get my 9Mb .sql file from my dev site into the new database. I found this script on StackOverflow:
    http://stackoverflow.com/questions/19751354/how-to-import-sql-file-in-mysql-database-using-php
    <?php
    
    // Name of the file
    $filename = '[path_to_your_sql_file_to_import].sql';
    // MySQL host
    $mysql_host = 'localhost';//<--change to the remote host address if needed
    // MySQL username
    $mysql_username = '[sql_username]';
    // MySQL password
    $mysql_password = '[sql_password]';
    // Database name
    $mysql_database = '[sql_database_name]';
    
    // Connect to MySQL server
    mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
    // Select database
    mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());
    
    // Temporary variable, used to store current query
    $templine = '';
    // Read in entire file
    $lines = file($filename);
    // Loop through each line
    foreach ($lines as $line)
    {
    // Skip it if it's a comment
    if (substr($line, 0, 2) == '--' || $line == '')
        continue;
    
    // Add this line to the current segment
    $templine .= $line;
    // If it has a semicolon at the end, it's the end of the query
    if (substr(trim($line), -1, 1) == ';')
    {
        // Perform the query
        mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
        // Reset temp variable to empty
        $templine = '';
    }
    }
     echo "Tables imported successfully";

    I created this as a file in the root of my modx install and ran it in a browser.

    So far, it looks like everything came in clean and saved me a lot of time (and errors) to get the db transferred.

    Posting it here as reference to anyone else who, on the rare event, finds themselves in this difficult situation.