PHP查询类不返回值

时间:2014-10-17 13:14:37

标签: php mysql json

我正在为我的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();

Jason文件('它将包含所有查询'):

{
   "Queries": [

        {"query1":"SELECT * FROM tbl_user"},
        {"query2":"SELECT * FROM tbl_users WHERE status=1"}

    ]
}

索引文件:

<?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>

所以我要做的是创建一个类来处理jason文件提取查询然后类来运行查询。我提到的Jason文件包含所有查询,在索引和任何包含query.php的文件中,我可以运行所有这样的查询:

 $execute->getQueryAction($execute->setStringAction('query name'));
经过一些调试后,我意识到代码在getQueryAction方法中失败了,我觉得mysqli_query不喜欢$ this-&gt;连接。

我的问题是:

为什么 以及如何解决它

3 个答案:

答案 0 :(得分:0)

mysqli->query不会返回结果。它只返回一个句柄,您可以使用该句柄请求结果。查看mysqli->fetch_assoc

http://php.net/manual/en/mysqli-result.fetch-assoc.php

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

    /* fetch associative array */
    while ($item = mysqli_fetch_array($result)) {
        echo $item['id']. ' ' . $item['user_name'] .'<br />';
    }

    /* free result set */
    mysqli_free_result($result);
}

答案 1 :(得分:0)

Thx试图帮助我们,但我只是设法修改了它,但是它们是:

将方法namd从__construct更改为DatabaseConnection类下的DBcon, 然后我在getQueryAction中做了这个:

  

$ this-&gt; result = mysqli_query($ this-&gt; connection-&gt; 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

答案 2 :(得分:0)

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

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();
}