samedi 19 décembre 2015

Why doesn't this work? sqlit3 php

I want to get the user by his email addresse or username ... but it doesn't work. Everytime i use a string and not a number, i just get false.

It's a school project so I can't use any frameworks :(

This doesn't work $objAcc->getUser('email', 'example@gmail.com')

This works

$objAcc->getUser('user_id', '1')

Methode:

    public function getUser($by = NULL, $value = NULL, $quantity = 0) {
        global $database;

        try {
            $row = array();
            if (isset($by) && isset($value)) {
                $query = 'SELECT * FROM "user" WHERE :by=:value';
                $stmt = $database->prepare($query);

                $type = ($by == 'user_id' || $by == 'phone' || $by == 'fax' || $by == 'zip' || $by == 'admin') ? SQLITE3_INTEGER : SQLITE3_TEXT;
                $stmt->bindValue(':by', $by, SQLITE3_TEXT);
                $stmt->bindValue(':value', $value, $type);

                $result = $stmt->execute();
            } else {
                $query = 'SELECT * FROM "user"';
                $stmt = $database->prepare($query);

                $result = $stmt->execute();
            }

            if ($quantity) {
                for ($i = 0; $i < $quantity; $i++) {
                    $row[ $i ] = $result->fetchArray(SQLITE3_ASSOC);
                }
            } else {
                $i = 0;
                while ($entry = $result->fetchArray(SQLITE3_ASSOC)) {
                    $row[ $i ] = $entry;
                    $i++;
                }
            }

            if (empty($row[0])) {
                return FALSE;
            }

            return $row;

        } catch (Exception $e) {
            var_dump($e);
            die();
        }
    }

I hope you can help me guys.

Aucun commentaire:

Enregistrer un commentaire