title: Running MySQL *.sql files in PHP
body: <p>I have two <code>*.sql</code> files that I use when creating a new web site database.  The first file creates all the tables.  The second file populates some default records.  I would like to execute these files from PHP.  I also use the Zend_Framework, if that will help accomplish this.</p>
  
  <p><strong>Additional Info</strong></p>
  
  <ol>
  <li>I don't have console access</li>
  <li>I'm trying to automate site generation from within our application.</li>
  </ol>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>Using <code>shell_exec()</code>...</p>
  
  <pre><code>$command = 'mysql'
          . ' --host=' . $vals['db_host']
          . ' --user=' . $vals['db_user']
          . ' --password=' . $vals['db_pass']
          . ' --database=' . $vals['db_name']
          . ' --execute="SOURCE ' . $script_path
  ;
  $output1 = shell_exec($command . '/site_db.sql"');
  $output2 = shell_exec($command . '/site_structure.sql"');
  </code></pre>
  
  <p>...I never did get useful output, but followed <a href="https://stackoverflow.com/questions/814586/calling-mysql-exe-using-php-exec-doesnt-work/814722#814722">some suggestions</a> on <a href="https://stackoverflow.com/questions/814586/calling-mysql-exe-using-php-exec-doesnt-work">another thread</a> and finally got it all working.  I switch to the <code>--option=value</code> format for the commands and used <code>--execute="SOURCE ..."</code> instead of <code>&lt;</code> to execute the file.</p>
  
  <p>Also, I never got a good explanation of the difference between <code>shell_exec()</code> and <code>exec()</code>.</p>