PDO SQL占位符问题

时间:2015-06-05 11:08:30

标签: php mysql pdo

任何人都可以弄清楚为什么这段代码不会将值打印到页面上?

$query_params = array(
            ':test1' => 'test1',
            ':test2' => 'test2'
);

// Count the number of records matching the first quota

$quota1Count = "SELECT COUNT(*)
               FROM :test1
               WHERE :test2 = 1";

try { 

        $stmt = $db->prepare($quota1Count); 
        $stmt->execute($query_params); 

    } 

catch(PDOException $ex) { 

        die("Failed to run query"); 
    } 

$quota1 = $stmt->fetchColumn();

打印到页面

Quota 1: <?= $quota1; ?>

当我不使用PDO占位符时,代码运行正常并返回正确的值:

$quota1Count = "SELECT COUNT(*)
               FROM test1
               WHERE test2 = 1";

0 个答案:

没有答案