doulu5717 2018-04-30 13:05
浏览 90
已采纳

与预准备语句和LIKE一起使用时,SHOW COLUMNS返回错误

I’m having troubles with the MySQL/MariaDB query below and I can’t figure out why.

$field = 'some_db_field';
$field = '%' . $field . '%';

$query = $db->prepare(
    " SHOW COLUMNS "
  . "         FROM table"
  . "         LIKE :fieldName"
);

$query->bindParam(':fieldName', $field, PDO::PARAM_STR);
$query->execute();

I get the following error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' at line 1' in script.php:

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dongyuan8312 2018-04-30 15:06
    关注

    I have experienced this issue as well.

    You simply can't use PDO bindParam function on a SHOW COLUMN query.

    I don't know why. I have searched the doc and the net for hours and I can't find any explanation.

    An alternative is to use PDO::getColumnMeta

    Or to query the information_schema

    $field = 'some_db_field';
    $field = '%' . $field . '%';
    
    $query = $db->prepare( 
        " SELECT * "
      . " FROM INFORMATION_SCHEMA.COLUMNS "
      . " WHERE table_name = 'your_table' AND column_name LIKE :fieldName "
    );
    
    $query->bindParam(':fieldName', $field, PDO::PARAM_STR);
    $query->execute();
        return $output; 
    }
    

    There's an advantage with this one over SHOW COLUMN: you have much more column information returned. Example :

    | TABLE_CATALOG | TABLE_SCHEMA |  TABLE_NAME | COLUMN_NAME | ORDINAL_POSITION | COLUMN_DEFAULT | IS_NULLABLE | DATA_TYPE | CHARACTER_MAXIMUM_LENGTH | CHARACTER_OCTET_LENGTH | NUMERIC_PRECISION | NUMERIC_SCALE | DATETIME_PRECISION | CHARACTER_SET_NAME |  COLLATION_NAME | COLUMN_TYPE | COLUMN_KEY | EXTRA |                      PRIVILEGES | COLUMN_COMMENT |
    |---------------|--------------|-------------|-------------|------------------|----------------|-------------|-----------|--------------------------|------------------------|-------------------|---------------|--------------------|--------------------|-----------------|-------------|------------|-------|---------------------------------|----------------|
    |           def |  db_9_1e4841 | input_table | Agency_CODE |                1 |         (null) |         YES |   varchar |                        3 |                      9 |            (null) |        (null) |             (null) |               utf8 | utf8_general_ci |  varchar(3) |            |       | select,insert,update,references |                |
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 C++使用TWAIN协议如何实现A3幅面扫描仪扫描A4横向
  • ¥15 如何在sql server里完成筛选
  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题