简体   繁体   中英

SQLite3 cant fetch columns

I wrote my code as simple as possible to make a simple login, because its my first time working with SQLite3 databases.

Here is my code:

<?php
$db  = new SQLite3("FirstDatabase.db");

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

    $query = $db->prepare("SELECT COUNT(`id`) FROM data WHERE `username` = '$username' AND `password` = '$password'");
    $query->execute();

    $count = $query->fetchColumn();

    if($count = 1){
        echo "Welcome $username!";
    } else {
        echo "Wrong credentials";
    }
}

?>

I'm really confused why this is not working.. I updated all the extensions etc. I know this is vulnerable to pretty much every type of attack, but I'm just trying to figure out the SQLite3 database connection.

Thanks! :)

This is the Error I get btw:

Fatal error: Uncaught Error: Call to undefined method SQLite3Stmt::fetchColumn() in C:\\XAMPP\\htdocs\\PHP-Login\\index.php:41 Stack trace: #0 {main} thrown in C:\\XAMPP\\htdocs\\PHP-Login\\index.php on line 41

The SQL is vulnerable to SQL injection which could be devastating to your database and your system and your health. First and foremost you should change the select to a parametrized query.

There are very good examples in the doc for using a parametrized query and binding the values, not to mention a ton of resource on this site.

The error is telling you that that there is not method fetchColumn() for a SQLite3Stmt object. The PHP: SQLite3Stmt doc confirms that. It is a method on the PDO database extension.

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