PDO fetch()不会返回应有的结果

时间:2018-08-13 18:33:29

标签: php mysql pdo

我对fetch()有点问题。它返回的东西,但不是返回的意思。代码:

public static function databaseConnect()
{
    $dsn = "mysql:host=".self::$host.";dbname=".self::$dbname;
    $opt = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
    ];
    $con = new PDO($dsn, self::$user, self::$password, $opt);
    return $con;
}


$db = new Database();
$state = $db->databaseConnect()->prepare("SELECT ? FROM users");
$state -> bindValue(1,'balance');
$state -> execute();
var_dump($state -> fetch());

在var_dump中:

array(5) { [0]=> array(1) { ["?"]=> string(7) "balance" } [1]=> array(1) { ["?"]=> string(7) "balance" } [2]=> array(1) { ["?"]=> string(7) "balance" } [3]=> array(1) { ["?"]=> string(7) "balance" } [4]=> array(1) { ["?"]=> string(7) "balance" } }

但是,我的表中恰好有5行。我的代码有什么问题?

0 个答案:

没有答案