php+mysql 语句中被查询的字段可以设置变量么
比如:$condition="a_size";$single_size=30;$sql_count="selectcount(*)ascountfromfoot_info...
比如:
$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'//不可运行
求大神解释 展开
$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'//不可运行
求大神解释 展开
展开全部
查询语句中可以直接放变量;
$abc = '内容';
$sql = "select * from table where a = '{$abc}'";
// 这个语句等同于: $sql = "select * from table where a = '内容'";
// 其次,字段名 和 字段值都可以用变量来代替,但是字段名不能加引号,否则会报 mysql 语法错误;
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询