PHP:从Thread返回Resultset

时间:2015-12-07 15:31:24

标签: php multithreading mysqli

我有以下课程:

class SQLExec extends Thread {

        private $mCnx;
        private $mError;
        private $mResult;
        private $mHost;
        private $mUser;
        private $mPassword;
        private $mDB;
        private $mSQL;

        public function __construct($host, $user, $password, $DB) {
            $this->mHost = $host;
            $this->mUser = $user;
            $this->mPassword = $password;
            $this->mDB = $DB;
        }

        public function run() {
            $this->runSQL();
        }

        public function setSQL($sql) {
            $this->mSQL = $sql;
        }

        public function runSQL() {
            $this->mResult = true;
            $this->mError = DEFAULT_MESSAGE;
            $this->mCnx = mysqli_connect($this->mHost, $this->mUser, $this->mPassword, $this->mDB);
            if (!$this->mCnx) {
                $this->mError = mysqli_connect_error();
                $this->mResult = false;
            } else {
                $stmt = mysqli_stmt_init($this->mCnx);
                if (!stmt) {
                    $this->mError = "Statement not initialized";
                    $this->mResult = FALSE;
                    return;
                }
                if (!mysqli_stmt_prepare($stmt, $this->mSQL)) {
                    $this->mError = mysqli_stmt_error($stmt);
                    $this->mResult = FALSE;
                    return;
                }
                if (mysqli_stmt_execute($stmt) == false) {
                    $this->mError = mysqli_stmt_error($stmt);
                    $this->mResult = FALSE;
                    return;
                }
                $this->mResult = mysqli_stmt_get_result($stmt);
                mysqli_close($this->mCnx);
            }
        }

        public function &getResult() {
            return $this->mResult;
        }

        public function getError() {
            return $this->mError;
        }

    }

现在我想使用它:

$exec = new SQLExec($host, $user, $pwd, $db);
$exec->setSQL('SHOW databases');
if ($exec->start() && $exec->join()) {
   $rset = $exec->getResult();
   if ($rset) {
   // processing $rset
   // ...
   }
}

但条件if ($rset)永远不会TRUE。我有什么不对? 构造函数的参数是有效的。我在这个类之外的代码中成功使用它们。不在Thread内的相同代码完美地运行

0 个答案:

没有答案