Catchable fatal error: Object of class PDOStatement could not be converted to string in /home/refined/public_html/refer.php on line 30

前端 未结 1 599
青春惊慌失措
青春惊慌失措 2021-01-24 18:52

I currently am building a small and simple referral system. However I am having trouble trying to return how many referrals one single user has. When a new user registers it sto

相关标签:
1条回答
  • 2021-01-24 19:41

    Let's analyse your code:

    $checknumber = $odb -> prepare("SELECT COUNT('referid') FROM `users` WHERE `ID` = :ID");
    

    ... where PDO::prepare() is defined as:

    PDOStatement PDO::prepare ( string $statement [, array $driver_options = array() ] )

    So it returns an object. But two lines later you try to print the object:

    echo($checknumber);
    

    If you check any of the usage examples in the manual you'll see that you need to call one of the PDOStatement methods to extract the query results, e.g.:

    <?php
    /* Execute a prepared statement by passing an array of values */
    $sql = 'SELECT name, colour, calories
        FROM fruit
        WHERE calories < :calories AND colour = :colour';
    $sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
    $sth->execute(array(':calories' => 150, ':colour' => 'red'));
    $red = $sth->fetchAll();
    

    Update: Two more links:

    • Available PDOStatement methods
    • Arrays in PHP
    0 讨论(0)
提交回复
热议问题