简体   繁体   中英

SQLSTATE[42000]: Syntax error or access violation: 1064 PDO

I get this every time I use my Mysql PDO class:

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 '? WHERE ?' at line 1

Heres the code:

function mysql_execute_safe_search($table, $where, $query){
    global $db;

    try{
        $preparedQuery = $db->prepare("SELECT * FROM ? WHERE ?");

        $preparedQuery->execute([$table, $where, $query]);
        return $preparedQuery->fetchAll(PDO::FETCH_ASSOC);
    } 

    catch(PDOException $Exception){

        echo $Exception->getMessage();

        return;
    }
}

Heres how I execute:

$search_query = mysql_execute_safe_search("people", "name", "Nathan");

您不能绑定表名称:

$preparedQuery = $db->prepare("SELECT * FROM yourtable WHERE ?");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM