2017-07-19 69 views
2

這裏是my_sqli數據庫連接使用PHP數據庫連接的兩個SELECT查詢

class FbChatMock { 
    // Holds the database connection 
    private $dbConnection; 
    private $_dbHost = 'localhost'; 
    private $_dbUsername = 'root'; 
    private $_dbPassword = ''; 
    public $_databaseName = 'erp5_temp2'; 
    public function __construct() { 
     $this->dbConnection = new mysqli($this->_dbHost, $this->_dbUsername, 
      $this->_dbPassword, $this->_databaseName); 
     if ($this->dbConnection->connect_error) { 
      die('Connection error.'); 
     } 
    } 

我得到的getchat函數的兩個參數:

public function getchat($userId, $id){ 
    $meesage = array(); 

    $query = "SELECT u.user_id FROM `users` u where u.id='$id'"; 
    $resultObj = $this->dbConnection->query($query); 
    $user = $resultObj->fetch_assoc()) 
    $userid = $user; 
} 

我從查詢得到$userid變量,在此查詢中使用它:

$query = "SELECT u.id,c.message,c.sent_on FROM `chat` c JOIN 
    `users` u ON c.user_id=u.user_id where u.id='$id' AND c.user_id='$userid'"; 
    // Execute the query 
    $resultObj = $this->dbConnection->query($query); 
    // Fetch all the rows at once. 
    while ($rows = $resultObj->fetch_assoc()){ 
     $meesage[] = $rows; 
    } 
    return $meesage; 

問題是我的第一個sql查詢無法正常工作。我通過顯示來自echo的$userid值來測試它。

回答

3

恰克此行

$userid = $user; 

對此

$userid = $user['user_id']; 

因爲$用戶是陣列選擇了所有列,你需要從它那裏得到唯一ID。

+0

我改變了這一行$ userid = $ user ['user_id']; echo($ userid); exit();但它沒有顯示回顯結果 –

0

你的問題就在這裏:

$user = $resultObj->fetch_assoc()) 
$userid = $user; 

因爲mysqli::fetch_assoc()與檢索關聯數組。因此,你必須像這樣訪問它(給出你選擇的列名):

$userid = $user["user_id"]; 
+0

我改變了這一行$ userid = $ user ['user_id']; echo($ userid); exit();但它沒有顯示回顯結果 –

+0

試着'var_dump()'看看它告訴你什麼 –