简体   繁体   中英

Getting the result of an SQLite query for PHP

I am trying to obtain the user id from my database, using the query bellow, to insert it somewhere else. I am new to php and sql so I can't really spot what's wrong. The result I get on var_dump() is object (SQLite3Result)#4 (0) { } - I only used this for testing. I tried using fetchArray() but it still got me nothing. The database works alright, I used it for other things.

    require 'database.php';     
    $db = new Database();
    $email = $_POST['member'];
    $list = $db->prepare('SELECT userid FROM users WHERE (email = :email)');
    $list->bindValue(':email', $email, SQLITE3_TEXT);
    $q = $list->execute();

    var_dump($q);

Thanks for any help!

Here is what you can do:

<?php

class UserDB extends SQLite3
{
    function __construct()
    {
        $this->open('test.db');
    }
}

$db = new UserDB();
$stmt = $db->prepare('SELECT * FROM users where email = :email');
$stmt->bindValue(':email', 'user1@example.com', SQLITE3_TEXT);
$result = $stmt->execute();
var_dump($result->fetchArray());

Then you can update the bindValue method call with the email variable.

For the sqlite3 db I created it using the following commands:

$ sqlite3 test.db
CREATE TABLE USERS (
ID INT PRIMARY KEY NOT NULL,
EMAIL TEXT NOT NULL
);
INSERT INTO USERS VALUES(1, 'user1@example.com');
.quit

Then you can use that with my 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