My SQL Error in session and query

1

I have a class where I use to get the username, and inside it I have the method:

public function getfName() {
        $Session = $_SESSION[$this->Prefix . 'username'];
        $sql = "SELECT * FROM " . DB_DBPREFIX . "$this->Table WHERE name = $Session";

        try {
            $stmt = Conn::dbPrepare($sql);
            $stmt->execute();
            while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
                $data = $row[1];
                print $data;
            }
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

But when I put the $ Session variable in my query I get this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'TheNight' in 'where clause'

I searched here on Stack and other sites and found a topic but it did not help me, so I decided to open this one!

    
asked by anonymous 01.06.2015 / 02:41

1 answer

2

Change the following line:

while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {

To:

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

See if it will work.

Edited:

Dude, as I do not know how your connection is, I'll post a working example complete:

$name = 'nome';

try {
    $conn = new PDO('mysql:host=localhost;dbname=database', "user", "pass");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $data = $conn->query('SELECT * FROM tabela WHERE name = ' . $conn->quote($name));

    $row = $data->fetch(PDO::FETCH_ASSOC);

    echo $row['name'];

} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

When it returns false it is because an error has occurred. Make the adjustment for your code and test.

    
01.06.2015 / 03:48