Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php SQL检查行存在的位置_Php_Mysql - Fatal编程技术网

Php SQL检查行存在的位置

Php SQL检查行存在的位置,php,mysql,Php,Mysql,伙计们,我有上面的代码来检查用户名是否存在,但是当我输入用户表中存在的用户名时,它仍然会回显用户未注册的消息,我的代码有什么问题吗?这部分 function SQL_QUERY_LOGIN_BY_SCHOOLCODE($con, $username, $opt=""){ if(is_resource($con)){ $q = "SELECT * FROM users WHERE username = '$username"; $result = mysql

伙计们,我有上面的代码来检查用户名是否存在,但是当我输入用户表中存在的用户名时,它仍然会回显用户未注册的消息,我的代码有什么问题吗?

这部分

function SQL_QUERY_LOGIN_BY_SCHOOLCODE($con, $username, $opt=""){
    if(is_resource($con)){
        $q = "SELECT * FROM users WHERE username = '$username";
        $result = mysql_query($q);
        $row = mysql_fetch_array($result);
        $count = mysql_num_rows($result);
        if($count == 0) return  $opt="0";
        if($count > 1) return $opt="1";
    }
}

$mysql_login_result = SQL_QUERY_LOGIN_BY_USERNAME(MYSQL_LINK, $form_username, 1);

if($mysql_login_result == 0){
    $message = "User not registered, please register!";
}else{
    $message = "Success!";
}
您应该检查计数是否大于0而不是1,因为只有1行具有用户名,而不是大于1行

if($count == 0) return  $opt="0";
if($count > 1) return $opt="1";
也不需要使用变量$opt,也不需要使用字符串作为返回,只需返回一个整数即可

if($count == 0) return  $opt="0";
if($count > 0) return $opt="1";
或者简单的一行:

if($count == 0) return 0;
if($count > 1) return 1;

请务必查看有关SQL注入等的评论。

您非常容易受到SQL注入攻击,如果您还没有受到攻击,您将被黑客攻击。请使用准备好的/参数化的查询来防止这种情况发生。另请参见:$username->'$username->$q=SELECT*用户的$username->'$username;-还要清理数据以防止注入您使用的函数在哪里?这不是你发布的代码中的那个。。。posted函数是由您通过\u用户名调用的\u学校代码
return ($count > 1) 1 : 0;