Home > Backend Development > PHP Tutorial > php+mysql 语句中被查询的字段可以设置变量么

php+mysql 语句中被查询的字段可以设置变量么

WBOY
Release: 2016-06-06 20:27:38
Original
1165 people have browsed it

比如:

<code>$condition = "a_size";
$single_size = 30;
$sql_count = "select count(*) as count from foot_info WHERE '$condition' = '$single_size' ";
</code>
Copy after login
Copy after login

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

<code>$sql_count = "select count(*) as count from foot_info WHERE".$condition." = '$single_size' ";
</code>
Copy after login
Copy after login

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

<code>select count(*) as count from foot_info WHERE Foot_Size = '30'//可运行
select count(*) as count from foot_info WHERE 'Foot_Size' = '30'//不可运行</code>
Copy after login
Copy after login

回复内容:

比如:

<code>$condition = "a_size";
$single_size = 30;
$sql_count = "select count(*) as count from foot_info WHERE '$condition' = '$single_size' ";
</code>
Copy after login
Copy after login

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

<code>$sql_count = "select count(*) as count from foot_info WHERE".$condition." = '$single_size' ";
</code>
Copy after login
Copy after login

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

<code>select count(*) as count from foot_info WHERE Foot_Size = '30'//可运行
select count(*) as count from foot_info WHERE 'Foot_Size' = '30'//不可运行</code>
Copy after login
Copy after login

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

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

<code>select * from foot_info WHERE 1=1;
select * from foot_info WHERE 1=0;</code>
Copy after login

<code>在postgresql中表名和字段名是单引号;在Mysql中表名和字段名必须``或不写
一句话概括就是Mysql的单引号和双引号会被Mysql当成字符串处理,`符号内是表名或者字段处理</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template