php+mysql 语句中被查询的字段可以设置变量么
黄舟
黄舟 2017-04-10 16:05:22
[PHP讨论组]

比如:

$condition = "a_size";
$single_size = 30;
$sql_count = "select count(*) as count from foot_info WHERE '$condition' = '$single_size' ";

我这样运行,貌似什么也查询不到
但很奇怪,如果我用字符串连接成sql语句就可以正常查询,如下

$sql_count = "select count(*) as count from foot_info WHERE".$condition." = '$single_size' ";

/////////////////////////////////////
我又输出了上面两个sql语句的输出,分别是

select count(*) as count from foot_info WHERE Foot_Size = '30'//可运行
select count(*) as count from foot_info WHERE 'Foot_Size' = '30'//不可运行
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(4)
PHP中文网

把$condition的引号去掉就好。。

天蓬老师

mysql的查询字段不需要加引号或者select count(*) as count from foot_info WHERE `Foot_Size` = '30'//可运行

怪我咯
select * from foot_info WHERE 1=1;
select * from foot_info WHERE 1=0;
ringa_lee
在postgresql中表名和字段名是单引号;在Mysql中表名和字段名必须``或不写
一句话概括就是Mysql的单引号和双引号会被Mysql当成字符串处理,`符号内是表名或者字段处理
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号