简体   繁体   English

使用变量作为表名的pdo update语句

[英]pdo update statement using variable as table name

the pdo update statement below doesn't work due the table name ties to a variable. 由于表名称与变量相关,因此下面的pdo update语句不起作用。 Does anyone know how to make it work? 有谁知道如何使它工作?

    $stmt1 = $DB_CON_C->prepare('UPDATE `".$account_list."`
    SET property_type=:property_type; property_address=:property_address, property_city=:property_city, property_state=:property_state, property_zip=:property_zip WHERE contract_number=:order_list');
    $stmt1->bindParam(':account_list', $account_list, PDO::PARAM_STR);
    $stmt1->bindParam(':order_list', $order_list, PDO::PARAM_STR);
    $stmt1->bindParam(':property_class', $property_class, PDO::PARAM_STR);
    $stmt1->bindParam(':property_type', $property_type, PDO::PARAM_STR);
    $stmt1->bindParam(':property_address', $property_address, PDO::PARAM_STR);
    $stmt1->bindParam(':property_city', $property_city, PDO::PARAM_STR);
    $stmt1->bindParam(':property_state', $property_state, PDO::PARAM_STR);
    $stmt1->bindParam(':property_zip', $property_zip, PDO::PARAM_STR);
    $stmt1->execute();

You will have to user single quotes in stead of double: 您将必须使用单引号而不是双引号:

$stmt1 = $DB_CON_C->prepare('UPDATE `' .$account_list. '`
    SET property_type=:property_type; property_address=:property_address, property_city=:property_city, property_state=:property_state, property_zip=:property_zip WHERE contract_number=:order_list');

Or, just simplify, and do: 或者,只需简化并做:

->prepare("UPDATE {$account_list} SET...

Ie, use double quotes. 即,使用双引号。 The {} isn't needed, but I prefer using them because I personally use this as a prefix to the actual table name (so ("SELECT * FROM {$dbprefix}tablename") ) {}不是必需的,但我更喜欢使用它们,因为我个人将其用作实际表名的前缀(因此("SELECT * FROM {$dbprefix}tablename")

This way you do not need to concoct strings inside the query, which you shouldn't need to do. 这样,您就无需在查询中编写字符串,而无需这样做。 Just wrap the query in double quotes instead. 只需将查询用双引号引起来即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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