PHP查询类不返回值

PHP查询类不返回值,php,mysql,json,Php,Mysql,Json,我正在为我的php项目编写一个查询类,但是我有一个问题,查询没有从我的数据库返回任何值 PHP代码: <?php class DatabaseConnect{ protected $host = 'localhost'; protected $user = 'root'; protected $pass = 'root'; protected $db = 'test'; public function __construct(){

我正在为我的php项目编写一个查询类,但是我有一个问题,查询没有从我的数据库返回任何值

PHP代码:

<?php

class DatabaseConnect{

    protected $host = 'localhost';
    protected $user = 'root';
    protected $pass = 'root';
    protected $db = 'test';

    public function __construct(){

        $con = mysqli_connect($this->host, $this->user, $this->pass, $this->db) or die('Cannot Connect to DB');

        return $con;

    }
}

class ExecuteQuery{

    public $connection;
    public $result;

    public function __construct(){

        $this->connection = new DatabaseConnect();

    }

    public function getQueryAction($sql){

        $this->result = mysqli_query($this->connection, $sql);

    }

    public function setStringAction($string){

        $file = file_get_contents('queryFile.json');
        $json = json_decode($file, true);

        foreach($json['Queries'] as $this->result){

            return $this->result[$string];

        }
    }
}

$execute = new ExecuteQuery();
索引文件:

<?php
require_once('query.php');
?>

<html>
<head>
    <title>
    </title>
</head>
<body>
<h1>Welcome</h1>
<h3><?php

    $execute->getQueryAction($execute->setStringAction('query1'));

    foreach($execute->result as $item){
        echo $item['id']. ' ' . $item['user_name'] .'<br />';
    }

?></h3>
</body>
</html>
经过一些调试后,我意识到getQueryAction方法中的代码失败了,我认为mysqli\u query不喜欢$this->connection

我的问题是:

为什么
如何修复它

mysqli->query
不会返回结果。它只返回一个可以请求结果的句柄。查找
mysqli->fetch\u assoc

if($result=mysqli\u query($this->connection,$sql)){
/*获取关联数组*/
while($item=mysqli\u fetch\u数组($result)){
echo$item['id'].'.$item['user_name'.]。
; } /*自由结果集*/ mysqli_免费_结果($result); }
谢谢你们试图帮助我,但我只是做了一些小改动来解决这个问题,它们是:

将DatabaseConnection类下的方法namd从_construct更改为DBcon, 然后我在getQueryAction中执行了以下操作:

$this->result=mysqli\u查询($this->connection->DBcon(),$sql)

全班同学:

class DatabaseConnect{

    protected $host = 'localhost';
    protected $user = 'root';
    protected $pass = 'root';
    protected $db = 'test';

    public function DBcon(){

        $con = mysqli_connect($this->host, $this->user, $this->pass, $this->db) or die('Cannot Connect to DB');

        return $con;

    }
}

class ExecuteQuery{

    public $connection;
    public $result;

    public function __construct(){

        $this->connection = new DatabaseConnect();

    }

    public function getQueryAction($sql){

        $this->result = mysqli_query($this->connection->DBcon(), $sql);

    }

    public function setStringAction($string){

        $file = file_get_contents('queryFile.json');
        $json = json_decode($file, true);

        foreach($json['Queries'] as $this->result){

            return $this->result[$string];

        }
    }
}

$execute = new ExecuteQuery();

现在一切都很完美了,仍然不确定为什么前面的方法(出现在初始问题中)有效xD

构造函数不能返回任何东西,构造函数的唯一目的是创建类的实例

private $con;
public function __construct(){

    $this->con = mysqli_connect($this->host, $this->user, $this->pass, $this->db) 
                 or die('Cannot Connect to DB');
}

public function get_connection(){
   return $this->con;
}
因此,在
ExecuteQuery
类中,您可以执行以下操作:

public function __construct(){
    $db = new DatabaseConnect();
    $this->connection = $db->get_connection();
}

另外:如果在foreach循环中执行
返回
,则将从循环中获得第一个结果,而不是所有结果!好的,我刚刚更改了
return$this->result[$string]
to
echo$this->result[$string]
但是现在它只返回查询字符串而不是结果请再次阅读答案。您需要先获取结果!不,我不必我知道我可以通过以下方法来实现这一点:class ExecuteQuery扩展DatabaseConnect,而不是让我的ExecuteQuery类\uuuu构造方法删除它,并在qetQueryAction中,而不是$this->connection中使用$this->\uu构造……但这看起来不太好,我想用问题中的代码的方式来实现哇,好的,这很有意义谢谢你,现在我不会再犯同样的错误了。。。。!!!很高兴你找到了解决办法。如果将来想使用类,您可能需要学习更多关于OOP的知识,您应该这样做。;)
class DatabaseConnect{

    protected $host = 'localhost';
    protected $user = 'root';
    protected $pass = 'root';
    protected $db = 'test';

    public function DBcon(){

        $con = mysqli_connect($this->host, $this->user, $this->pass, $this->db) or die('Cannot Connect to DB');

        return $con;

    }
}

class ExecuteQuery{

    public $connection;
    public $result;

    public function __construct(){

        $this->connection = new DatabaseConnect();

    }

    public function getQueryAction($sql){

        $this->result = mysqli_query($this->connection->DBcon(), $sql);

    }

    public function setStringAction($string){

        $file = file_get_contents('queryFile.json');
        $json = json_decode($file, true);

        foreach($json['Queries'] as $this->result){

            return $this->result[$string];

        }
    }
}

$execute = new ExecuteQuery();
private $con;
public function __construct(){

    $this->con = mysqli_connect($this->host, $this->user, $this->pass, $this->db) 
                 or die('Cannot Connect to DB');
}

public function get_connection(){
   return $this->con;
}
public function __construct(){
    $db = new DatabaseConnect();
    $this->connection = $db->get_connection();
}