Php mysqli_fetch_array无效

时间:2017-02-02 02:14:16

标签: php mysql

我尝试在Android应用中创建注册登录功能。我尝试使用此Php文件将数据从我的应用程序发送到数据库。但是,当我点击我的应用程序上的注册按钮时,我收到了错误/错误。看起来像mysqli_fetch_array不起作用。有谁知道如何解决这个问题?谢谢!

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
    $name = $_POST['name'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $email = $_POST['email'];

    if($name == '' || $username == '' || $password == '' || $email == ''){
        echo 'please fill all values';
    }else{
        require_once('dbConnect.php');
        $sql = "SELECT * FROM users WHERE username='$username' OR email='$email'";          
        $result=mysqli_query($con,$sql);
        $check = mysqli_fetch_array($result,MYSQLI_BOTH);   
        if(isset($check)){      
            echo 'username or email already exist';
        }else{          
            $sql = "INSERT INTO users (name,username,password,email) VALUES('$name','$username','$password','$email')";
            if(mysqli_query($con,$sql)){
                echo 'successfully registered';
            }else{
                echo 'oops! Please try again!';
            }
        }
        mysqli_close($con);
    }
 } else{
echo 'error';
}

dbConnect.php

<?php
 define('HOST','localhost');
 define('USER','username');
 define('PASS','password');
 define('DB','database');

 $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

1 个答案:

答案 0 :(得分:0)

我认为你应该删除isset check。无论如何,它总会回归真实。请改为计数检查。

if(count($check)){      
    echo 'username or email already exist';
}else{          
    $sql = "INSERT INTO users (name,username,password,email) VALUES('$name','$username','$password','$email')";
    if(mysqli_query($con,$sql)){
        echo 'successfully registered';
    }else{
        echo 'oops! Please try again!';
    }
}