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 |                |
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里