无法检查用户名是否存在 [英] unable to check username exists or not
问题描述
我创建了一个注册程序,将用户名密码插入到 mysql 数据库中.我现在正在尝试检查用户名是否存在.我编写了以下程序.它不工作.它显示mysql_num_rows() 期望参数 1 是资源."我真的需要你的专家建议
I have created a registration program which insert username password into mysql database. I am now trying to check the username exixts or not. I wrote the following program. its not working. it showing "mysql_num_rows() expects parameter 1 to be resource." I really need your expert suggestion
registration.php
<?php
require 'jcon.php';
if(isset($_POST["username"], $_POST["firstname"],$_POST["password"])){
$username=$_POST["username"];
$firstname=$_POST["firstname"];
$password=$_POST["password"];
}
$query = mysql_query("SELECT * FROM member WHERE username='$username'");
if(mysql_num_rows($query) != 0)
{
echo "Username already exists";
}
else{
$sql="INSERT INTO member (username, firstname, password)
VALUES ('$username', '$firstname','$password')";}
if(!mysqli_query($con,$sql)){
die('Error: ' . mysqli_error($con));
}
echo "Dear {$firstname} ! you have been successfully registered. "
?>
推荐答案
它显示mysql_num_rows() 期望参数 1 是资源
it showing "mysql_num_rows() expects parameter 1 to be resource
这是mysql_query
在失败时返回false
从而触发臭名昭著的典型情况:
This is the typical case where mysql_query
returns false
upon failure therefore triggering the infamous:
mysql_num_rows() 期望参数 1 为资源
mysql_num_rows() expects parameter 1 to be resource
这可能是由多种因素造成的.尝试在 phpMyAdmin 或直接到数据库中运行查询并查看错误或通过 mysql_error
.
This can be caused by multiple factors. Try running the query in phpMyAdmin or directly to the database and see the error or fetch the last mysql error via mysql_error
.
最好始终检查 mysql_query
的返回值是否为 false
或 mysql_error
字符串是否为空:
It's good practice to always check if the returned value of mysql_query
is false
or if the mysql_error
string is not empty:
if ($result and empty(mysql_error()))
// everything ok
注意:永远不要混合使用 mysql_
和 mysqli_
函数.如果您必须选择我会选择 mysqli
,因为 mysql_*
函数被认为已弃用.
Note: Never ever mix mysql_
and mysqli_
functions. If you have to choose I'd go with mysqli
since mysql_*
functions are considered deprecated.
这篇关于无法检查用户名是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!