2015-11-28 99 views
1

我正在編寫PHP後端程序,但我的程序不工作,因爲PHP不支持get_result()fetch_assoc()函數。PHP get_result()和fetch_assoc()替代

我該如何重寫代碼,以便它們能夠以與此函數相同的方式工作?

public function storeUser($userLoginID, $password) {       

    $uuid = uniqid('', true); 
    $hash = $this->hashSSHA($password); 
    $encrypted_password = $hash["encrypted"]; 
    $salt = $hash["salt"]; 
    $stmt = $this->conn->prepare("INSERT INTO hkcheung.users(unique_id,userLoginID,encrypted_password,salt,created_at) VALUES (?,?,?,?,NOW())"); 
    $stmt->bind_param("ssss", $uuid, $userLoginID, $encrypted_password, $salt); 
    $result = $stmt->execute(); 
    $stmt->close(); 
    if ($result) { 
     $stmt = $this->conn->prepare("SELECT * FROM hkcheung.users WHERE userLoginID=?"); 
     $stmt->bind_param("s", $userLoginID); 
     $stmt->execute(); 
     $result = $stmt->get_result(); 
     $users = $result->fetch_assoc(); 
     $stmt->close(); 
     return $users; 
    } else { 
     return false; 
    } 
} 
+0

爲什麼不工作?你收到的錯誤是什麼? –

+0

它應該可以工作,但主機不支持get_result()函數,PHP版本不夠新。主持人來自我的大學,我無法自己升級,我不得不調整我的代碼來應對它。但我是新來的PHP。 –

+0

嘗試使用mysqli_fetch_assoc而不是$ result-> fetch_assoc();和mysqli_stmt_get_result($ result)而不是$ result = $ stmt-> get_result(); –

回答

0

下面添加功能,以您的PHP文件

function get_result($Statement) { 
    $RESULT = array(); 
    $Statement->store_result(); 
    for ($i = 0; $i < $Statement->num_rows; $i++) { 
     $Metadata = $Statement->result_metadata(); 
     $PARAMS = array(); 
     while ($Field = $Metadata->fetch_field()) { 
      $PARAMS[] = &$RESULT[ $i ][ $Field->name ]; 
     } 
     call_user_func_array(array($Statement, 'bind_result'), $PARAMS); 
     $Statement->fetch(); 
    } 
    return $RESULT; 
} 

,並在get_result線,將其更改爲

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