mysql pdo查询的问题

时间:2016-12-15 09:24:24

标签: php mysql pdo

我的查询有问题吗?

function needExport($table, $key, $id, $stamp) {
    try {
        $dbW = dbConCC();
        $stmt = $dbW->prepare("SELECT * FROM :table LIMIT 10");
        $params = array(':table' => "tmlaender");
        $stmt->execute($params);
        while ($row = $stmt->fetch()) {
            echo $row;
        }
    }
    catch(PDOException $e) {
        echo json_encode($e->getMessage());
    }
}

我得到了下面的语法错误:

"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''tmlaender' LIMIT 1' at line 1"

在这个"简单"中找不到任何错误。查询。

1 个答案:

答案 0 :(得分:0)

试试这个:

function needExport($table, $key, $id, $stamp) {
    try {
        $dbW = dbConCC();
        $stmt = $dbW->prepare("SELECT * FROM `table`");
        $stmt->execute();

        $fetch = $stmt->fetch(PDO::FETCH_OBJ);

        return $fetch;
    }
    catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
}

然后在回复表中的字段时,您只需:

$var1 = $var->needExport();

echo $var1->idColumn;

注意:

绑定参数仅在使用WHERE时使用,例如:

public function getId($userid)
{
    $dbW = dbConCC();
    $stmt = $dbW->prepare("SELECT * FROM `users` WHERE `userid` = :uid");
    $stmt->bindParam(':uid', $userid);
    $stmt->execute();
}