无回声数组结果“未定义变量:行”

时间:2014-02-06 19:31:47

标签: php mysql oop

请记住,我对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

1 个答案:

答案 0 :(得分:0)

你知道,函数内部的$row变量在结束执行时会丢失。由于此行<h2><?php echo $row['id'];?></h2>位于fetchUserByEmail()之外,因此此时$row将不确定。

您可以将功能结果分配到$row变量:$row = fetchUserByEmail($email)