dongqianzhan8325 2016-10-07 14:44
浏览 6
已采纳

如何从函数中获取字符串

So, this is my code:

class Functions{
    public static function login($email,$password){
        $email = $_GET['email'];
        $password = $_GET['password'];
        if(isset($_GET['submit']) AND isset($email) AND isset($password)){
            $password = md5($password);
            $sql = "SELECT * FROM users WHERE email='$email' AND password='$password'";
            $result = $conn->query($sql);
            if ($result->num_rows > 0) {
                $row = $result->fetch_assoc();
                $_SESSION['nume'] = $row["name"];
                $_SESSION['uid'] = $row["id"];
                $_SESSION['admin'] = $row["admin"];
                $_SESSION['email'] = $row["email"];
                $_SESSION['points'] = $row["points"];
            }else{
                $errortxt = "Invalid Login Credentials";
                $error = true;
            }
        }
        return $error;
    }
}

In my HTML file I'm calling for the function like this:

Function::login($email,$password);

But I'm wondering how can I get the $errortxt string to echo in the HTML file.

Thanks!

  • 写回答

2条回答 默认 最新

  • dongshi1188 2016-10-07 15:04
    关注

    You just need a simple fix:

    class Functions{
    public static function login($email,$password){
        $response['error'] = false;
        $response['errortxt'] = "";
        $email = $_GET['email'];
        $password = $_GET['password'];
        if(isset($_GET['submit']) AND isset($email) AND isset($password)){
            $password = md5($password);
            $sql = "SELECT * FROM users WHERE email='$email' AND password='$password'";
            $result = $conn->query($sql);
            if ($result->num_rows > 0) {
                $row = $result->fetch_assoc();
                $_SESSION['nume'] = $row["name"];
                $_SESSION['uid'] = $row["id"];
                $_SESSION['admin'] = $row["admin"];
                $_SESSION['email'] = $row["email"];
                $_SESSION['points'] = $row["points"];
            }else{
                $response['error'] = true;
                $response['errortxt'] = "Invalid Login Credentials";
            }
        }
        return $response;
    }
    }
    

    And you need to call it this way:

    $fnCallStatus = Function::login($email,$password); //Now you have the response;
    if($fnCallStatus['error']) //we have an error
    {
        echo $fnCallStatus['errortxt']; //we print the message
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥15 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)