dongquelu1239 2015-01-05 22:25
浏览 6

注册时使用mysql_query()时返回不正确

I am very beginner in PHP.

When I am registering a new user, I received an unproper 'return' (-1 instead of 1) when I am using mysql_query. Without line with "mysql_query" return is proper.

What am I doing wrong?

public function register ($username, $password, $activationcode) {
    $username = $this->parse($username);
    $password = $this->parse($password);

    $query_search = "SELECT * from tbl_user WHERE username = '".$username."' ";
    $query_exec = mysql_query($query_search) or die(mysql_error());
    $no_of_rows = mysql_num_rows($query_exec);

if ($no_of_rows == 0)
{
        $newUser="INSERT INTO tbl_user(username, password,activationcode) VALUES ('".$username."', '".$password. "','".$activationcode."')"; 
        if(mysql_query($newUser))
        {
        return 1;
        }
}else {
    return -1;
    }
}
  • 写回答

1条回答 默认 最新

  • duanchuang1935 2015-01-05 23:41
    关注

    Let me say I would not code it that way, but here is what I would do to correct your code while keeping your structure:

    include($_SERVER['DOCUMENT_ROOT']."/database.php"); // your database connection values that are into a $database variable that we will use now. For example, mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD);
    
    public function register ($username, $password, $activationcode){
        $username = $this->parse($username);
        $password = $this->parse($password);
    
        mysqli_select_db(DB_NAME); // the name of the database you will use.
    
        $sql = mysqli_query($database, "SELECT * from tbl_user WHERE username = '$username'") or die(mysqli_error());
    
        if(mysqli_num_rows($sql) == 0){
            $newUser = "INSERT INTO tbl_user (username, password, activationcode) VALUES ('$username', '$password', '$activationcode')";
            if(mysqli_query($database, $newUser)){
                return 1;
            } else {
                return -1; // you might get a problem in here too.
            }
        } else {
            return -1;
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题