title: PHP Pthreads - using mysqli
body: <p>I'm trying to utilize pthreads for the first time in my web application.  I have pthreads working for simple test cases, but am having trouble getting mysql queries within pthreads to work.  Here's my php file:</p>
  
  <pre><code>class SqlThread extends Thread
  {
      private $dbc;
      public $log;
      public $return;
  
      public function __construct(){
          $this-&gt;dbc = mysqli_connect("localhost", "root", "rootpassword", "my_database");
  
          $this-&gt;log = "&lt;br&gt;(".__LINE__.") construct finished.";
      }
  
      //gets called when thread is started
      public function run(){
          $val = $this-&gt;testSqlCall();
  
          $this-&gt;log .= "&lt;br&gt;(".__LINE__.") testSqlCall() has finished";
  
          $this-&gt;return = $val;
      }
  
      /**
       * testing sql queries
       */
      function testSqlCall(){
  
          $sql = "SELECT *
                  FROM client_detail";
  
          $this-&gt;log .= "&lt;br&gt;(".__LINE__.") testSqlCall() - sql: ".$sql;
  
          $r= @mysqli_query($this-&gt;dbc,$sql);
          $num = mysqli_affected_rows($this-&gt;dbc);
  
          $this-&gt;log .= "&lt;br&gt;(".__LINE__.") testSqlCall() - total number of returned rows: ".$num;
  
          return $num;
  
      }
  }
  
  /**
   * threaded test
   */
  $thread = new SqlThread();
  $thread-&gt;start();
  $thread-&gt;join();
  
  echo "&lt;br&gt;&lt;br&gt;&lt;br&gt;thread return value:";
  var_dump($thread-&gt;return);
  echo $thread-&gt;log;
  
  /**
   * same test, but not threaded
   */
  
  $dbc = mysqli_connect("localhost", "root", "rootpassword", "my_database");
  $sql = "SELECT *
          FROM client_detail";
  $r = @mysqli_query($dbc,$sql);
  $num = mysqli_affected_rows($dbc);
  echo "&lt;br&gt;&lt;br&gt; --- non-threaded return value: $num";
  </code></pre>
  
  <p>and here's what it returns:</p>
  
  <pre><code>thread return value:
  
  null
  
  
  (12) construct finished.
  (32) testSqlCall() - sql: SELECT * FROM client_detail
  (37) testSqlCall() - total number of returned rows:
  (19) testSqlCall() has finished
  
  --- non-threaded return value: 39276
  </code></pre>
  
  <p>As you can see, the mysqli query within the SqlThread returns nothing, while the exact same query outside the SqlThread class returns what I would expect.  </p>
  
  <p>Any Ideas? Has anyone gotten sql queries within php threads to work?</p>