java - Android:用户使用 php 登录时出错

标签 java php android mysql

我正在创建一个允许用户注册和登录的应用程序,该应用程序使用 php 连接到数据库并使用 mysql 存储用户信息。 虽然我有一个我似乎无法弄清楚的问题。

这是 PHP 脚本 DB_Functions.php

<?php 
class DB_Functions 
{

private $db;

//put your code here
// constructor
function __construct() 
{
    require_once 'DB_Connect.php';
    // connecting to database
    $this->db = new DB_Connect();
    $this->db->connect();
}

// destructor
function __destruct() 
{

}

/**
 * Storing new user
 * returns user details
 */
public function storeUser($name, $email, $password) 
{
    $uuid = uniqid('', true);
    $hash = $this->hashSSHA($password);
    $encrypted_password = $hash["encrypted"]; // encrypted password
    $salt = $hash["salt"]; // salt
    $result = "INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())";
    // check for successful store
    if ($result) 
    {
        // get user details 
        $uid = mysqli_insert_id($result); // last inserted id
        $result = ("SELECT * FROM users WHERE uid = $uid");
        // return user details
        return mysqli_fetch_array($result);
    }
}

/**
 * THE PROBLEM IS HERE!
 * Get user by email and password
 */
public function getUserByEmailAndPassword($email, $password) 
{
    $result = ("SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
    // check for result 
    $no_of_rows = mysql_num_rows($result);
    if ($no_of_rows > 0) 
    {
        //user not found
        return false;
    }
    else 
    {
        $result = mysql_fetch_array($result);
        $salt = $result['salt'];
        $encrypted_password = $result['encrypted_password'];
        $hash = $this->checkhashSSHA($salt, $password);
        // check for password equality
        if ($encrypted_password == $hash) 
        {
            // user authentication details are correct
            return $result;
        }
    }
}

/**
 * Check user is existed or not
 */
public function isUserExisted($email) 
{
    $result = ("SELECT email from users WHERE email = '$email'");
    $no_of_rows = mysql_num_rows($result);
    if ($no_of_rows > 0) 
    {
        // user existed 
        return true;
    } 
    else 
    {   
        // user not existed
        return false;
    }
}

/**
 * Encrypting password
 * @param password
 * returns salt and encrypted password
 */
public function hashSSHA($password) 
{
    $salt = sha1(rand());
    $salt = substr($salt, 0, 10);
    $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
    $hash = array("salt" => $salt, "encrypted" => $encrypted);
    return $hash;
}

/**
 * Decrypting password
 * @param salt, password
 * returns hash string
 */
public function checkhashSSHA($salt, $password) 
{
    $hash = base64_encode(sha1($password . $salt, true) . $salt);
    return $hash;
}
}
?>

这是我遇到的错误,我似乎不知道要添加什么。

警告:mysql_num_rows() 期望参数 1 为资源,在线 /home/bf13/13421254/public_html/android_login_api/include/DB_Functions.php 中给出的字符串53

警告:mysql_fetch_array() 期望参数 1 为资源,/home/bf13/13421254/public_html/android_login_api/include/DB_Functions.php 行中给出的字符串61
{"tag":"login","error":true,"error_msg":"不正确的电子邮件或密码!"

最佳答案

$result = "INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())";
    // check for successful store
    if ($result)

你实际上并不是在查询,也许:

$result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())");
    // check for successful store
    if ($result) 

关于java - Android:用户使用 php 登录时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29989641/

相关文章:

java - Liquibase 是否需要写权限才能获取锁?

java - 在仔细锁定但不受信任的代码上使用 Thread.stop()

java - 具有多个 catch 语句的无法访问的代码

java - 如何在应用程序启动时自动加载RecyclerView?

PHP 在非静态方法上调用 self

android - 在 XML 中为 EditText 指定虚拟键盘类型

java - 从数组填充 GridView

PHP EDI X12 解析

php - 在php中自动执行链接

java - 在其他类中使用抽象变量