-1

I am trying to do something new in PHP, counting rows in a MYSQL database. But it keeps spitting out errors as soon as I put echo $rows in it.

Can someone please help me?

Here is my login code.

    <?php
    'include('classes/DB.php');

if (isset($_POST['login'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];

        if (DB::query('SELECT username FROM users WHERE username=:username', array(':username'=>$username))) {
          // if (DB::query('SELECT token FROM login_tokens WHERE user_id=1')<=5) {
                if (password_verify($password, DB::query('SELECT password FROM users WHERE username=:username', array(':username'=> $username))[0]['password'])) {
                        echo 'Logged in!';
                        $query = DB::query('SELECT * FROM login_tokens WHERE user_id=:user_id', array(':user_id'=>$user_id));
                        $rows = mysql_num_rows($query);
                        echo $rows;
                        $cstrong = True;
                        $token = bin2hex(openssl_random_pseudo_bytes(64, $cstrong));
                        $user_id = DB::query('SELECT id FROM users WHERE username=:username', array(':username'=>$username))[0]['id'];
                        DB::query('INSERT INTO login_tokens VALUES (0, :token, :user_id)', array(':token'=>sha1($token), ':user_id'=>$user_id));

                        setcookie("SNID", $token, time() + 60 * 60 * 24 * 7, '/', NULL, NULL, TRUE);
                        setcookie("SNID_", '1', time() + 60 * 60 * 24 * 3, '/', NULL, NULL, TRUE);

                } else {
                        echo 'Incorrect Password!';
                }
              // } else {
                      // echo 'You already logged in on 5 different devices!'
              // }

        } else {
                echo "User not registered! Create account <a href='http://follome.ddns.net/create-account.php'>here</a>!";
        }

}

?>
<h1>Login to your account</h1>
<form action="login.php" method="post">
<input type="text" name="username" value="" placeholder="Username ..."><p />
<input type="password" name="password" value="" placeholder="Password ..."><p />
<input type="submit" name="login" value="Login">
</form>

And my DB class:

    <?php
class DB {

        private static function connect() {
          $pdo = new PDO('mysql:host=127.0.0.1;dbname=socialnetwork;charset=utf8', 'root', 'Daan0109');
          $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          return $pdo;
        }

        public static function query($query, $params = array()) {
                $statement = self::connect()->prepare($query);
                $statement-> execute($params);

                if (explode(' ', $query)[0] == 'SELECT') {
                  $data = $statement->fetchAll();
                  return $data;
                }

        }

}
Daan Hermans
  • 31
  • 10

2 Answers2

0

You can't mix the db extensions (PDO, mysqli, mysql). Furthermore, mysql was deprecated and is removed in php7.

You should use the PDO variant of mysql_num_rows()

http://php.net/manual/de/pdostatement.rowcount.php

But you need the PDOStatement object to call this. Otherwise, if you always return fetchAll(), you can just use a simple count()

clemens321
  • 2,103
  • 12
  • 18
0

It's because you use PDO, so you should check the row number with its rowcount. http://php.net/manual/en/pdostatement.rowcount.php

Also, I would suggest you to use a framework or at least an ORM to support your goals, because otherwise you will waste a lot of time implementing features, which are aleady done. For ORM I think Doctrine is a good choice, and Laravel is an easy-to-start framework. I hope I could help you.