2015-05-04 47 views
-1

這是我寫的類。當我執行findbyAdNetID方法時沒有錯誤。但是,該方法不提取任何數據。如果我把它放在一個類的外面並且除去實例變量$ this,那麼相同的代碼就可以工作。任何幫助,將不勝感激。謝謝。mysqli類使用準備好的語句不返回任何數據

<?php 

class adscraper_mysqli 
{ 
private $id; 
private $result; 
private $rows; 
private $_mysqli; 
private $statement; 
private $query ="SELECT * FROM scrapelist_master WHERE ad_network_id=?"; 

public function __construct($host = NULL, $username = NULL, $password = NULL, $db = NULL) 
{ 
    $this->host = $host; 
    $this->username = $username; 
    $this->password = $password; 
    $this->db = $db; 
    $this->connect(); 
} 

/** 
* A method to connect to the database 
* 
*/ 
public function connect() 
{ 

    $this->_mysqli = new mysqli ($this->host, $this->username, $this->password, $this->db) 
    or die('There was a problem connecting to the database'); 
} 

/** 
* A method to create prepared statements 
* 
*/ 
public function prepare() 
{ 
    $this->statement = $this->_mysqli->prepare($this->query); 
} 
/* Method to query using prepared statement */ 
public function findbyAdNetID($id){ 
    if (!$this->statement) { 
     $this->prepare(); 
    } 
    $this->statement->bind_param("i", $this->id); 
    $this->statement->execute(); 

    $this->result = $this->statement->get_result(); 

    $this->rows = $this->result->fetch_all(MYSQLI_ASSOC); 
    return $this->rows; 

} 

} // END class 
?> 

`

+0

設置你用調試器來看看會發生什麼,並且該值由每個返回了什麼電話?我會從這裏開始。 – mins

回答

2

所有的代碼寫在正確的道路。唯一的問題,爲什麼你有沒有什麼,在findbyAdNetID($ id)的方法是行:

$this->statement->bind_param("i", $this->id); 

但是你沒有設置$這個 - > ID。所以,你必須只使用$ ID而不是像

$this->statement->bind_param("i", $id); 

,或者在開始

$this->id = id; 
+0

你是老闆!非常感謝 – hvs