与MySQL建立连接的问题

时间:2017-05-16 14:04:10

标签: php mysql

所以我在register.php页面上不断收到以下错误消息。我的托管服务提供商说它是我的PHP版本从7.1一直到5.6但仍然是相同的东西。以下是不断出现的错误。

  

第74行的/home/scylla97/public_html/register.php中无法将类PDOStatement的对象转换为int

请参阅下面的register.php页面上的代码。

            if ($securimage->check($_POST['captcha_code']) == false) {
                echo '<font color="white">Wrong captcha :\ </font>';
        }
        else {
        $username = $_POST['username'];
        $password = $_POST['password'];
        $rpassword = $_POST['rpassword'];
        $email = $_POST['email'];
        $errors = array();
        $checkUsername = $odb -> prepare("SELECT COUNT(*) FROM `users` WHERE `username` = :username");
        $checkUsername -> execute(array(':username' => $username));
        $countUsername = $checkUsername -> fetchColumn(0);
        if ($checkUsername > 0)
        {
            $errors['<font color="white">Username is already taken</font>'];
        }
        if (!ctype_alnum($username) || strlen($username) < 4 || strlen($username) > 15)
        {
            $errors[] = '<font color="white">Username Must Be  Alphanumberic And 4-15 characters in length</font>';
        }
        if (!filter_var($email, FILTER_VALIDATE_EMAIL))
        {
            $errors[] = '<font color="white">Email is invalid</font>';
        }
        if (empty($username) || empty($password) || empty($rpassword) || empty($email))
        {
            $errors[] = '<font color="white">Please fill in all fields</font>';
        }
        if ($password != $rpassword)
        {
            $errors[] = '<font color="white">Passwords do not match</font>';
        }
        if (empty($errors))
        {
             try{ 
            $insertUser = $odb -> prepare("INSERT INTO `users` VALUES(NULL, :username, :password, :email, 0, 0, 0, 0)");
            $insertUser -> execute(array(':username' => $username, ':password' => SHA1($password), ':email' => $email));
            } 
            catch(PDOException $exception){ 
               return $exception->getMessage(); 
            } 
            echo '<div class="nNote nSuccess hideit"><p><strong><font color="white">SUCCESS: </font></strong><font color="white">User has been registered.  Redirecting....</font></p></div><meta http-equiv="refresh" content="3;url=login.php">';
        }
        else
        {
            echo '<div class="nNote nFailure hideit"><p><strong>ERROR:</strong><br />';
            foreach($errors as $error)
            {
                echo '-'.$error.'<br />';
            }
            echo '</div>';
        }
    }
    }
    ?>

真的很感谢你的帮助:)

2 个答案:

答案 0 :(得分:0)

$checkUsername = $odb -> prepare("SELECT COUNT(*) FROM `users` WHERE `username` = :username");
        $checkUsername -> execute(array(':username' => $username));//OOP 101 suggests that $checkUsername gets all functions/methods contained via the `$odb` variable  

 $countUsername = $checkUsername -> fetchColumn(0);// use this variable in your if statements.

$checkUsername仅仅是声明。它不能单独用于检查count

将您的if条件更改为

if ($countUsername > 0)
        {
            $errors['<font color="white">Username is already taken</font>'];
        }

答案 1 :(得分:0)

您正在将PDOStatement $checkUsername与整数进行比较(在这种情况下,如果它大于0,则为chek),而实际上,行数存储在$countUsername中,因此您需要将代码更改为:

    if ($countUsername> 0)
    {
        $errors['<font color="white">Username is already taken</font>'];
    }

Obs:不允许发表评论