2013-05-10 215 views
0

我有一個DB類,我創建了幾個函數來返回不同的值。其中一個函數返回(或應該是)代表應用程序登錄用戶的「用戶」類對象。php函數不返回類對象

class user { 
    public $guid = ''; 
    public $fname = ''; 
    public $lname = ''; 

    public function __construct() {} 

    public function print_option() { 
     return "<option value='$this->guid'>$this->name</option>"; 
    } 
} 

在DB類,我有以下兩個功能:

public function get_user($uid) { 
    $sql = ''; 
    $usr = new user(); 
    $sql = "SELECT guid, fname, lname FROM ms.users WHERE guid=?"; 

    if($sth = $this->conn->prepare($sql)) { 
     $sth->bind_param('s', $uid); 
     if($sth->execute()) { 
      $sth->bind_result($usr->guid, $usr->fname, $usr->lname); 
      $sth->fetch(); 
      print_r($usr); // PRINTS OUT CORRECTLY 
      return $usr; 
     } 
     else {return null;} 
    } 
    else {return null;} 
} 

public function get_practice_user_list($pguid) { 
    $ret = ''; 
    $sql = "SELECT user_guid FROM ms.perm WHERE p_guid=?"; 
    if($sth = $this->conn->prepare($sql)) { 
     $sth->bind_param('s', $pguid); 
     if($sth->execute()) { 
      $usr = new user(); 
      $guid = ''; 

      $sth->bind_result($guid); 
      while($sth->fetch()) { 
       print_r($guid); // PRINTS GUID correctly 
       $usr = $this->get_user($guid); 
       print_r($usr); // PRINTS NOTHING object is null so prints "error" two lines later. 
       if($usr != null) $ret .= $usr->print_option(); 
       else print "error"; 
      } 

      return $ret; 
     } 
     else {return null;} 
    } 
    else {return null;} 
} 

我只是不理解爲什麼「用戶」的對象是不是在這種情況下返回。其他調用get_user函數的工作很好,並返回與該用戶有關的用戶類對象。

TIA

+1

你在調用哪個函數,返回的是什麼? – ashes999 2013-05-10 01:24:41

+0

我以get_practice_user_list函數開始。現在它什麼都不返回 – Slinky33 2013-05-10 01:27:17

+0

它必須返回一些東西,即使那是'null'。 – 2013-05-10 01:37:44

回答

0

的問題是與查詢。由於代碼只是循環查詢一個查詢(get_practice_user_list),因此調用get_user函數並嘗試第二個查詢MySQL時返回了錯誤消息out of sync。當我查看時,我可以通過在第一個查詢上執行fetch_all然後遍歷該數組以獲取用戶來修復它。

0

我猜你的GUID可能是一個整數,所以

$sth->bind_param('s', $uid); 

bind_param的第一個參數應該是 '我' 而不是 'S';

http://www.php.net/manual/en/mysqli-stmt.bind-param.php

+0

不,它是一個字符串,用'uniqid()' – Slinky33 2013-05-10 03:48:48

+0

就像我說的那樣,它填充在'get_user'函數,但是返回並不顯示在'get_practice_user_list'中 – Slinky33 2013-05-10 03:49:34