为什么我不能使用mysql_num_rows?

时间:2014-11-03 00:15:00

标签: php mysql phpmyadmin

在我的代码的第25行,我写了这个:

$check = mysql_num_rows($u_check); 

它给了我错误:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in D:\Download\htdocs\hoping.php on line 25

这是我的整个文件代码:

<?php include ("./header.inc.php"); ?>
<?php
$reg = @$_POST['reg'];
$fn = "";
$ln = "";
$un = "";
$em = "";
$em2 = "";
$pswd = "";
$pswd2 = "";
$d = "";
$u_check = "";
$fn = strip_tags(@$_POST['fname']);
$ln = strip_tags(@$_POST['lname']);
$un = strip_tags(@$_POST['username']);
$em = strip_tags(@$_POST['email']);
$em2 = strip_tags(@$_POST['email2']);
$pswd = strip_tags(@$_POST['password']);
$pswd2 = strip_tags(@$_POST['password2']);
$d = date("Y-m-d");

if($reg){
if($em==$em2){
$u_check = mysql_query("SELECT username FROM test WHERE username='$un'");
$check = mysql_num_rows($u_check);
if($check == 0){
if($fn&&$ln&&$un&&$em&&$em2&&$pswd&&$pswd2){
if($pswd==$pswd2){
if(strlen($un)>25||strlen($fn)>25||strlen($ln)>25){
echo "The maximum limit for username/first name/last name is 25 characters!";
}
else
{
if(strlen($pswd)>30||strlen($pswd)<5){
echo "Your password must be between 5 and 30 characters long!";
}
else
{
$pswd =md5($pswd);
$pswd2 = md5($pswd2);
$query = mysql_query("INSERT INTO test VALUES ('', '$un', '$fn', '$ln','$em', '$pswd', '$d','0')");
die("<h2>Welcome to communicate</h2>Login to your account to get started ...");
}
}
}
else {
echo "Your passwords don't match!";
}
}
else {
echo "Please fill in all of the fields";
}
}
else {
echo "Username already taken ...";
}
}
else {
echo "Your E-mails don't match!";
}
}
if(isset($_POST["user_login"]) && isset($_POST["password_login"])) {
$user_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["user_login"]);
$password_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password _login"]);
}
?>



            <td width="40%">
            <h2>Sign Up Below!</h2>
            <form action="#" method="POST"> 
<input type="text" name="fname" size="25" placeholder="First Name" /><p />
                <input type="text" name="lname" size="25" placeholder="Last Name"/><br /><br />
                <input type="text" name="username" size="25" placeholder="Username"/><br /><br />
                <input type="text" name="email" size="25" placeholder="Email Address"/><br /><br />
                <input type="text" name="email2" size="25" placeholder="Email Address (again)"/><br /><br />
                <input type="text" name="password" size="25" placeholder="Password"/><br /><br />
                <input type="text" name="password2" size="25" placeholder="Password (again)"/><br /><br />
                <input type="submit" name="reg" value="Sign Up!">
</td>
        </tr>

    </table>
    <?php include ("./footer.inc.php"); ?>

为什么它会给我这个错误?

2 个答案:

答案 0 :(得分:1)

mysql_query在出错时返回false(布尔值),所以也许这就是发生的事情。如果是这样,请尝试打印错误?例如:

$u_check = mysql_query("SELECT username FROM test WHERE username='$un'");
if(!$u_check) {
    die('Invalid query: ' . mysql_error());
}

这可能会指向正确的方向。另请注意,mysql_query和相关函数早已弃用。您应该学习如何使用mysqli *函数。

答案 1 :(得分:0)

在执行查询之前我没有看到您的mysql_connect,是否在header.inc.php内?

确保您还在查询和连接中包含错误检查,以便了解事情未按预期运行的原因。

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

您提到的错误似乎正在发生,因为查询失败,错误显示没有资源,因此请在查询中添加错误检查,如果失败则退出。

$u_check = mysql_query("SELECT `username` FROM `test` WHERE `username` = '$un'");
if (!$u_check) {
    die('Invalid query: ' . mysql_error());
}
$check = mysql_num_rows($u_check);