PDO - Fatal error: Call to a member function fetch() on a non-object

user2693017

if I try to run the following PHP code, I get a

Call to a member function fetch() on a non-object.

Do you know why? I use the same code on another site, where it works just fine.

<?php
$username = ($_GET ['user']);
try {
    $dbh = new PDO("mysql:host=localhost;dbname=***", '***', '***');    
} catch (PDOException $e) {
    echo $e->getMessage();
}
$sth = $dbh->query( "SELECT user, captcha 
    FROM xf_captcha WHERE user='$username'" );
print_r($sth->fetch());
?>

Edit:

$sth = $dbh->query( "SELECT username, user_state, last_activity, alerts_unread, conversations_unread, message_count 
    FROM xf_user WHERE username='$user'" );
$row = $sth->fetch();

Edit2:

Does this look safe, should I do more ?

<?php
$username = ($_GET ['user']);
try {
    $dbh = new PDO("mysql:host=localhost;dbname=***", '***', '***');
} catch (PDOException $e) {
    echo $e->getMessage();
}
$sth = $dbh->prepare("SELECT username, captcha, timestamp 
    FROM xf_captcha 
    WHERE username = :username", array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute(array(':username' => $username));
print_r($sth->fetch());
?>
Fluffeh

Your code has the variable $username in the top part of your question, but you then have $user in the bottom section.

Are you perhaps meaning to use the same variable?

$username = ($_GET ['user']);
$sth = $dbh->query( "SELECT username, user_state, last_activity, alerts_unread, conversations_unread, message_count 
  FROM xf_user WHERE username='$user'" );
  //                           ^^ Should this ALSO be $username ?   
$row = $sth->fetch();

Edit: Okay, now you are just being cute with your PDO::ATTR_EMULATE_PREPARES. Observe this:

Database and table structure:

Database changed
mysql> show tables
    -> ;
+----------------+
| Tables_in_prep |
+----------------+
| users          |
+----------------+
1 row in set (0.00 sec)

mysql> select * from users;
+----+---------+--------+
| id | userid  | pass   |
+----+---------+--------+
|  1 | Fluffeh | mypass |
+----+---------+--------+
1 row in set (0.00 sec)

And some PHP code that is copied from yours, with the added PDO attribute:

<?php
    //$username = ($_GET ['user']);
    $username="Fluffeh";

    $dbh = new PDO('mysql:host=localhost;dbname=prep', 'prepared', 'example');
    $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

    $sth = $dbh->query( "SELECT userid, pass FROM users WHERE userid='$username'" );
    echo "Trying to use $username.\n";
    print_r($sth->fetch());
    echo "----------------------------------------\n\n";
?>

<?php
    //$username = ($_GET ['user']);
    $username="user2693017";

    $dbh = new PDO('mysql:host=localhost;dbname=prep', 'prepared', 'example');
    $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

    $sth = $dbh->query( "SELECT userid, pass FROM users WHERE userid='$username'" );
    echo "Trying to use $username.\n";
    print_r($sth->fetch());
    echo "----------------------------------------\n\n";
?>

<?php
    //$username = ($_GET ['user']);
    $username="Oh my' or 1=1 or 'm=m";

    $dbh = new PDO('mysql:host=localhost;dbname=prep', 'prepared', 'example');
    $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

    $sth = $dbh->query( "SELECT userid, pass FROM users WHERE userid='$username'" );
    echo "Trying to use $username.\n";
    print_r($sth->fetch());
    echo "----------------------------------------\n\n";
?>

<?php
    //$username = ($_GET ['user']);
    $username="(select id from users limit 1)";

    $dbh = new PDO('mysql:host=localhost;dbname=prep', 'prepared', 'example');
    $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

    $sth = $dbh->query( "SELECT userid, pass FROM users WHERE id='$username'" );
    echo "Trying to use $username.\n";
    print_r($sth->fetch());
    echo "----------------------------------------\n\n";
?>

<?php
    //$username = ($_GET ['user']);
    // Changed this one to be a non-string, you might be checking an ID instead.
    $username="(select id from users limit 1)";

    $dbh = new PDO('mysql:host=localhost;dbname=prep', 'prepared', 'example');
    $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

    $sth = $dbh->query( "SELECT userid, pass FROM users WHERE id=$username" );
    echo "Trying to use $username.\n";
    print_r($sth->fetch());
    echo "----------------------------------------\n\n";
?>

<?php
    //$username = ($_GET ['user']);
    $username="bob'; drop table users; \  
    ";
    // This one is tricker to do in PHP code. I could easily enter this into a text field however.

    $dbh = new PDO('mysql:host=localhost;dbname=prep', 'prepared', 'example');
    $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

    //$sth = $dbh->query( "SELECT userid, pass FROM users WHERE id='$username'" );
    echo "Trying to use $username.\n";
    print_r($sth->fetch());
    echo "----------------------------------------\n\n";
?>

And the output:

    Trying to use Fluffeh.
stdClass Object
(
    [userid] => Fluffeh
    [pass] => mypass
)
----------------------------------------


    Trying to use user2693017.
----------------------------------------


    Trying to use Oh my' or 1=1 or 'm=m.
stdClass Object
(
    [userid] => Fluffeh
    [pass] => mypass
)
----------------------------------------


    Trying to use (select id from users limit 1).
----------------------------------------


    Trying to use (select id from users limit 1).
stdClass Object
(
    [userid] => Fluffeh
    [pass] => mypass
)
----------------------------------------


    Trying to use bob'; drop table users; \  
        .
----------------------------------------

Oh, the reason I left the last one till LAST is this output now in my database:

mysql> show tables;
Empty set (0.00 sec)

Yes, that's right, I just dropped a table. Let me say that again, I had a select statement, and with a little trickery I entered in a value that ANYONE with half a brain and some malicious intent could do into a text field, and DROPPED YOUR TABLE.

Now, granted, if you are setting things up properly, you might well set up a different user for the select statements, and only grant them select rights from your database, to stop this sort of thing happening - but lets be honest... you aren't are you?

Clearly setting that emulation is not enough. Seriously, now PLEASE do go read that answer, use prepared statements and use params if you want to be secure in your code.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

PHP Fatal error: Call to a member function find() on a non-object however my function work

来自分类Dev

Fatal error: Call to a member function check_capabilities() on a non-object

来自分类Dev

Invalid use of this in a non static member function

来自分类Dev

PHP PDO FetchAll与Fetch

来自分类Dev

accessing member function from an object inside a friend function

来自分类Dev

While和foreach与获取PDO :: FETCH_OBJECT的不同行为

来自分类Dev

与 PDO::FETCH_CLASS 相反

来自分类Dev

Is a function call parameter always a new object?

来自分类Dev

PHP PDO:fetchAll(PDO :: FETCH_COLUMN | PDO :: FETCH_GROUP)不起作用

来自分类Dev

PDO提取(PDO :: FETCH_ASSOC)不返回值

来自分类Dev

Catchable Fatal Error: Object of class Proxies\__CG__\AppBundle\Entity\Ticket could not be converted to string

来自分类Dev

在 Windows 上使用 Codeception 2.2.10 运行接受时出现错误“Call to a member function get() on null”

来自分类Dev

Laravel5.6 "Call to a member function delete() on null ", 删除表中的一行

来自分类Dev

如何正确使用PDO :: FETCH_SERIALIZE?

来自分类Dev

PHP PDO :: FETCH_ASSOC返回false

来自分类Dev

PDO :: FETCH_ASSOC返回false

来自分类Dev

php pdo fetch无法使用功能

来自分类Dev

PDO Fetch Assoc返回单词“ Array”

来自分类Dev

FETCH_ASSOC PDO 连续循环

来自分类Dev

使用 PDO Fetch 时的意外输出

来自分类Dev

PDO::FETCH_ASSOC 意外结果

来自分类Dev

为什么 fetch(PDO::FETCH_ASSOC) 返回 false

来自分类Dev

PDO binding error

来自分类Dev

Call function directly after constructor: new Object()->callFunction()

来自分类Dev

PL/SQL How to call a function without getting returned object

来自分类Dev

使PDO :: fetch(PDO :: FETCH_CLASS)返回对象的空值实例,而不是bool(false)

来自分类Dev

FETCH API返回[object Object]

来自分类Dev

PDO如果fetch()则print_r($ smt-> fetch(PDO :: FETCH_OBJ))将不会显示任何结果

来自分类Dev

Why does printing a variable and function call together in this code give an error?

Related 相关文章

  1. 1

    PHP Fatal error: Call to a member function find() on a non-object however my function work

  2. 2

    Fatal error: Call to a member function check_capabilities() on a non-object

  3. 3

    Invalid use of this in a non static member function

  4. 4

    PHP PDO FetchAll与Fetch

  5. 5

    accessing member function from an object inside a friend function

  6. 6

    While和foreach与获取PDO :: FETCH_OBJECT的不同行为

  7. 7

    与 PDO::FETCH_CLASS 相反

  8. 8

    Is a function call parameter always a new object?

  9. 9

    PHP PDO:fetchAll(PDO :: FETCH_COLUMN | PDO :: FETCH_GROUP)不起作用

  10. 10

    PDO提取(PDO :: FETCH_ASSOC)不返回值

  11. 11

    Catchable Fatal Error: Object of class Proxies\__CG__\AppBundle\Entity\Ticket could not be converted to string

  12. 12

    在 Windows 上使用 Codeception 2.2.10 运行接受时出现错误“Call to a member function get() on null”

  13. 13

    Laravel5.6 "Call to a member function delete() on null ", 删除表中的一行

  14. 14

    如何正确使用PDO :: FETCH_SERIALIZE?

  15. 15

    PHP PDO :: FETCH_ASSOC返回false

  16. 16

    PDO :: FETCH_ASSOC返回false

  17. 17

    php pdo fetch无法使用功能

  18. 18

    PDO Fetch Assoc返回单词“ Array”

  19. 19

    FETCH_ASSOC PDO 连续循环

  20. 20

    使用 PDO Fetch 时的意外输出

  21. 21

    PDO::FETCH_ASSOC 意外结果

  22. 22

    为什么 fetch(PDO::FETCH_ASSOC) 返回 false

  23. 23

    PDO binding error

  24. 24

    Call function directly after constructor: new Object()->callFunction()

  25. 25

    PL/SQL How to call a function without getting returned object

  26. 26

    使PDO :: fetch(PDO :: FETCH_CLASS)返回对象的空值实例,而不是bool(false)

  27. 27

    FETCH API返回[object Object]

  28. 28

    PDO如果fetch()则print_r($ smt-> fetch(PDO :: FETCH_OBJ))将不会显示任何结果

  29. 29

    Why does printing a variable and function call together in this code give an error?

热门标签

归档