2014-02-06 36 views
-1

請記住,我完全是OOP概念的新手,我一直在嘗試從mysql數據庫檢索數據並回顯結果。是那樣簡單,但我有這麼及彼不同的錯誤是應該返回結果無法回顯數組結果「未定義的變量:行」

function fetchUserByEmail($email=NULL) 
{ 
    if($email!=NULL) { 
     $column = "email"; 
     $data = $email; 
    } 
    global $mysqli,$db_table_prefix; 
    $stmt = $mysqli->prepare("SELECT 
     id, 
     user_name, 
     display_name, 
     password, 
     email, 
     activation_token, 
     last_activation_request, 
     lost_password_request, 
     active, 
     title, 
     sign_up_stamp, 
     last_sign_in_stamp 
     FROM ".$db_table_prefix."users 
     WHERE 
     $column = ? 
     LIMIT 1"); 
     $stmt->bind_param("s", $data); 

    $stmt->execute(); 
    $stmt->bind_result($id, $user, $display, $password, $email, $token, $activationRequest, $passwordRequest, $active, $title, $signUp, $signIn); 
    while ($stmt->fetch()){ 
     $row = array('id' => $id, 'user_name' => $user, 'display_name' => $display, 'password' => $password, 'email' => $email, 'activation_token' => $token, 'last_activation_request' => $activationRequest, 'lost_password_request' => $passwordRequest, 'active' => $active, 'title' => $title, 'sign_up_stamp' => $signUp, 'last_sign_in_stamp' => $signIn); 
    } 
    $stmt->close(); 
    return ($row); 
} 

什麼,我需要知道的是完全相同的功能,我要去如何調用結果在html標籤

這裏是我的嘗試:

$email = mysql_real_escape_string($_POST['email']); 
fetchUserByEmail($email); 
    ?><ul> 
     <div style="background-color:#EEEEEE" class="content"> 
      <li> 
      <h2><?php echo $row['id'];?></h2> 

我得到通知:未定義的變量:ERROR

+2

'$行= fetchUserByEmail($電子郵件);' – vee

回答

0

你看,裏面的局部變量$row函數在結束執行時會丟失。由於此線路<h2><?php echo $row['id'];?></h2>fetchUserByEmail()之外,因此$row將在該時刻未定義。

您可以將功能的結果賦值給變量$row$row = fetchUserByEmail($email)