duanjiaoxi4928 2013-04-27 18:57
浏览 67
已采纳

php变量作为mysql列名

I tried to find solution for this about everywhere, but nothing Google gave seems to work for my specific case.

My table has 4 columns that I want updated that I have named 0, 1, 2, 3, conveniently as I have to loop through it to update. Here is one instance of the column structure :

 column name 0 ENUM('Y','N') null=no default='N'

Now I want to set the value to 'Y' using this loop, but it will not accept the way I am referring to the $i variable within the query.

See below my last attempt. I also tried to cast $i as int, but that did not work either.

I appreciate your guidance on this.

                     /* update versions columns to Y where applicable */
        for ($i=0;$i <= $ctvid-1;$i++){
        $sql = mysql_query (
        "SELECT trim_id
        FROM versiontrim
        WHERE (
        version_id =$arr_vid[$i]
        )");
        $escapedi=mysql_real_escape_string($i);
        while ($row1 = mysql_fetch_assoc( $sql ))
        {
        $r=$row1['trim_id'];
        mysql_query ("UPDATE ttcomp SET '".$escapedi."' ='Y' where         trim_id=$r ");
        }
                    }
  • 写回答

2条回答 默认 最新

  • doulianglou0898 2013-04-27 19:41
    关注

    As documented under Schema Object Names:

    Permitted characters in unquoted identifiers:

    [ deletia ]

    • Identifiers may begin with a digit but unless quoted may not consist solely of digits.

    [ deletia ]

    The identifier quote character is the backtick (“`”):

    Therefore, if you must name your columns in this way (and, really, you should find some other solution), you will need to quote them with backticks:

    UPDATE ttcomp SET `0` = 'Y' WHERE ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题