简体   繁体   中英

Fatal error: Uncaught Error: Call to a member function RowCount() on bool

ı have a problem my login page. I got this error: Fatal error: Uncaught Error: Call to a member function RowCount() on bool in C:\\xampp\\htdocs\\root\\login.php:43 Stack trace: #0 {main} thrown in C:\\xampp\\htdocs\\root\\login.php on line 43

  <?php

                    if(isset($_POST['loginRequest'])){

                        $username = $_POST['email'];
                        $password = $_POST['password'];
                        $Sifreli = md5($password);

                        $query = $db->query("SELECT * FROM yonetim WHERE username = '{$username}'")->fetch(PDO::FETCH_ASSOC);
                        if($say = $query->RowCount()){
                        if ($say > 0){
                            $user = $query;
                            if($user['pass'] == $Sifreli){
                                $_SESSION['LOGGED'] = $user['username'];
                                if(isset($_SESSION['LOGGED'])){
                                    header('location:index.php');
                                }
                            } else {
                                echo "Kullanıcı adı veya şifre hatalı!";
                            }
                        } else {
                            echo 'Böyle bir kullanıcı bulunmamaktadır.';
                        }
                    }
                    }
                    ?>

rowCount() is the most useless method in all database APIs. And your code is a perfect example of that.

You already have your database results in $query, it means you don't need anything else . Just get rid of the rowCount line and use $query right in the condition:

$query = $db->query("SELECT * FROM yonetim WHERE ...")->fetch(PDO::FETCH_ASSOC);
if ($query){

Two obligatory notes:

  1. Under no circumstances you should put a data variable directly in the query. Use a placeholder and a prepared query instead.
  2. Using MD5() is no better than storing as a plain text. You must use password_verify() instead.

Here is a canonical example I wrote for the code to check the login and password using PDO :

$stmt = $pdo->prepare("SELECT * FROM yonetim WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch();

if ($user && password_verify($password, $user['password']))
{
    $_SESSION['LOGGED'] = $user['username'];
    header('location:index.php');
    exit;
} else {
    echo "Kullanıcı adı veya şifre hatalı!";
}

see - it's much simpler actually than your current code!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM