My SQL Error in Session and query

Asked

Viewed 52 times

1

I have a class where I use to get the username, and inside 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();
        }
    }

Only 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 did not help me, so I decided to open this!

  • Try to put it like this: name='$Session' in single quotes

  • @Diegomachado you almost got rsrs, the error disappeared more did not appear what I wanted, I want to take my name there on DB and print on the screen...

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 works.

Edited:

Dude, since I don’t know how your connection is, I’m going to post a full working example:

$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 he returns false is because an error has occurred.
Make the adaptation to your code and test.

  • No, it hasn’t! http://i.imgur.com/fuCT0a3.png was supposed to show Guilherme there where it’s marked in red

  • Just to confirm, I had initially put it like this: PDO:: FETCH_ASSOC with a space after two points, then corrected. Check if your ta with this space, if you have, take it out and test, otherwise, tell me.

  • Oops, ta no space, an observation when I give a var_dump($data) get back to me boolean false.

  • I edited the code, see if it works.

  • same thing, my God that boring thing this business rsrs, ta tense :/

  • Very strange this, I just tested it before posting and it worked. Have you used PDO before? Is it enabled? If you’ve never used it maybe you need to enable it.

  • Yes it is enabled, because I have used before... is it that I am not doing wrong? 'Cause I’ve followed every step you’ve taken, DB :/

  • helped me notice the query: $sql = "SELECT * FROM " . DB_DBPREFIX . "$this->Table WHERE username = '$Session'"; now compare with the other :D ready Thanks Diego!

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.