title: Error: Unterminated quoted string during PHP exec
body: <p>I'm using the <code>exec</code> function in PHP to make mysql dump a query's contents into a CSV file.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>$outfile = '/home/dotslash/file.csv';
  $emails_table = 'all_emails';
  
  $host = 'localhost';
  $user = 'root';
  $pass = 'root';
  $dbname = 'test';
  
  $query = "SELECT DISTINCT(emailid), COUNT(emailid), GROUP_CONCAT(DISTINCT offers) FROM '" . $emails_table . "' GROUP BY emailid INTO OUTFILE '" . $outfile . "' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n';";
  
  exec("mysql -h$host -u$user -p$pass $dbname -e '$query'");
  </code></pre>
  
  <p>And I'm getting this error: <code>sh: 2: Syntax error: Unterminated quoted string</code></p>
  
  <p>I know the problem is escaping when the string is being passed from PHP to bash, but I can't figure out what I'm doing wrong. Please help!</p>