我正在爲我的PHP項目編寫一個查詢類,但是我遇到了一個問題,查詢不會從我的數據庫返回任何值。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();
傑森文件( '它將包含所有查詢'):
{
"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文件提取一個查詢,然後類來運行查詢。正如我前面提到賈森文件包含所有的查詢,在指數和任何文件,其中包括我query.php我可以運行所有這樣的疑問:
$execute->getQueryAction($execute->setStringAction('query name'));
一些調試我意識到代碼getQueryAction方法失敗後,我認爲mysqli_query不喜歡$ this->連接。
我的問題是:
爲什麼 以及如何解決它
哇確定,使很多道理感謝你,現在我不會再犯同樣的錯誤.... !!! – Tomazi 2014-10-17 14:33:37