简体   繁体   English

PHP数据库单例不返回第二个结果

[英]php database singleton does not return second result

I'm new to all of this so please excuse me if I've done something daft. 我对这一切都是陌生的,所以如果我做些愚蠢的事情,请原谅。 I realise I probably have. 我知道我可能有。 But I've been trying to get this to work for 2 days now and I'm really stuck. 但是我一直在努力使它工作2天,我真的很困。

I have a simple database singleton as below: 我有一个简单的数据库单例,如下所示:

define('SERVER',$config["serverName"]);
define('USERNAME',$config["userName"]);
define('PASSWORD',$config["password"]);
define('DATABASE',$config["databaseName"]);

class DbClass{

    private static $instance;
    private $conn;

    private function __construct(){
        $this->conn = new mysqli(SERVER, USERNAME, PASSWORD, DATABASE);
    }

    public static function init(){
        if(is_null(self::$instance)){
            self::$instance = new DbClass();
        }
        return self::$instance;
    }

    public function callQuery($query){
        $result = null;
        try{
            $result = $this->conn->query($query);
        }
        catch(Exception $e){
            return $e->getMessage();
        }
        return $result;
    }
}

I have a simple test file where I am trying to call a stored procedure twice and access the result. 我有一个简单的测试文件,试图在其中调用两次存储过程并访问结果。 It all works the first time, but the second time I call it, I get a null result. 第一次都可以,但是第二次调用时,结果为空。 Here's the simple test file: 这是简单的测试文件:

require_once '../utility/DbClass.php';

$conn = DBClass::init();
$query4a = "CALL selectMaxBaseline(218)";
$result = $conn->callQuery($query4a);
var_dump($conn);
var_dump($result);

$queryA = "CALL selectMaxBaseline(218)";
try{
    $result2 = $conn->callQuery($queryA);
}
catch(Exception $e) {
    echo $e->getMessage();
}
var_dump($conn);
var_dump($result2);

The $conn object looks good and I get the same object back both times I call var_dump. $ conn对象看起来不错,两次调用var_dump时我都得到了相同的对象。 Which is what I would expect. 这是我所期望的。 I can iterate through the $result the first time round and see it's returning the right data. 我可以在第一轮中遍历$ result,并查看它返回了正确的数据。 So I know the database connection is good, the stored procedure works and it returns a valid result (I've removed this code for brevity). 因此,我知道数据库连接良好,存储过程正常工作,并且返回有效结果(为简便起见,我删除了此代码)。 But the $result object is false for the second call. 但是$ result对象在第二次调用中为false。 No errors are returned. 没有错误返回。 Here's the output from running the file: 这是运行文件的输出:

object(DbClass)[1]
  private 'conn' => 
    object(mysqli)[2]
      public 'affected_rows' => null
      public 'client_info' => null
      public 'client_version' => null
      public 'connect_errno' => null
      public 'connect_error' => null
      public 'errno' => null
      public 'error' => null
      public 'error_list' => null
      public 'field_count' => null
      public 'host_info' => null
      public 'info' => null
      public 'insert_id' => null
      public 'server_info' => null
      public 'server_version' => null
      public 'stat' => null
      public 'sqlstate' => null
      public 'protocol_version' => null
      public 'thread_id' => null
      public 'warning_count' => null
object(mysqli_result)[3]
  public 'current_field' => null
  public 'field_count' => null
  public 'lengths' => null
  public 'num_rows' => null
  public 'type' => null
object(DbClass)[1]
  private 'conn' => 
    object(mysqli)[2]
      public 'affected_rows' => null
      public 'client_info' => null
      public 'client_version' => null
      public 'connect_errno' => null
      public 'connect_error' => null
      public 'errno' => null
      public 'error' => null
      public 'error_list' => null
      public 'field_count' => null
      public 'host_info' => null
      public 'info' => null
      public 'insert_id' => null
      public 'server_info' => null
      public 'server_version' => null
      public 'stat' => null
      public 'sqlstate' => null
      public 'protocol_version' => null
      public 'thread_id' => null
      public 'warning_count' => null
boolean false

I've tried various things without any success. 我尝试过各种方法,但都没有成功。 Any ideas gratefully received. 任何想法表示感谢。

It turns out that the first query has not been 'used' by the time the second query fires and mysqli does not allow concurrent queries. 事实证明,在第二个查询触发时,第一个查询尚未“使用”,并且mysqli不允许并发查询。 So the trick is to 'clear' the first query, then fire the second query. 因此,诀窍是“清除”第一个查询,然后触发第二个查询。

I changed the function to: 我将功能更改为:

public function callQuery($query){
    try{
        while($this->conn->more_results()) {
            $this->conn->next_result();
            $this->conn->use_result();
        }
        $result = $this->conn->query($query);
        return $result;
    }
    catch(Exception $e){
        return $e->getMessage();
    }
}

and now everything works as expected. 现在一切都按预期进行。 Both calls to callQuery return the correct result as expected. 对callQuery的两个调用均按预期返回正确的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM