title: Connection refused when using shell_exec
body: <p>I'm trying to connect to a database using a script by <code>shell_exec</code>, but I'm getting:</p>
  
  <blockquote>
    <p>mysqli::mysqli(): (HY000/2002): No such file or directory. </p>
  </blockquote>
  
  <p>When using the script without <code>shell_exec</code> (example: by submit button) it works perfectly.</p>
  
  <hr>
  
  <p>On the caller php file:</p>
  
  <pre><code>shell_exec('php /Users/Desktop/save.php param')
  </code></pre>
  
  <p>Called php file:</p>
  
  <pre><code>$a=$argv[1];
  saveonDB($a);
  </code></pre>
  
  <p>I really don't know why isn't working when calling the function by <code>shell_exec</code>. The parameters are being passed correctly.</p>
  
  <p>Thanks</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I made the example simpler:</p>
  
  <p>connection.php:</p>
  
  <pre><code>function connect()
  {
      $servername = "localhost";
      $username = "root";
      $password = "root";
      $dbname= "db";
  
      $conn = new mysqli($servername, $username, $password, $dbname);
      if ($conn-&gt;connect_error) {
          die("Connection failed: " . $conn-&gt;connect_error);
      }
      else{
          echo "connected";
      }
  
  }
  
  connect();
  </code></pre>
  
  <p>caller.php:</p>
  
  <pre><code>    echo exec ("php /Users/Desktop/connection.php");
  </code></pre>
  
  <p>When executing the connection.php directly, it says:</p>
  
  <blockquote>
    <p>"connected"</p>
  </blockquote>
  
  <p>When executing from caller.php it stays: </p>
  
  <blockquote>
    <p>"Connection failed: No such file or directory"</p>
  </blockquote>