php操作mysql语法格式出错

WBOY
Release: 2016-06-06 20:21:58
Original
1188 people have browsed it

<code>1、错误查询语句:$sql='select * from sh_admin where username=$username and password=$password limit 1';
2、正确查询语句:$sql = "select * from sh_admin where username = '{$username}' and password = '{$password}' limit 1";
</code>
Copy after login
Copy after login

当PHP操作mysql数据库时为什么非得用 '{$username}'的方式才能查询,PHP语法中双引号就能解析变量啊 ,为什么我用第一种会出错呢?

回复内容:

<code>1、错误查询语句:$sql='select * from sh_admin where username=$username and password=$password limit 1';
2、正确查询语句:$sql = "select * from sh_admin where username = '{$username}' and password = '{$password}' limit 1";
</code>
Copy after login
Copy after login

当PHP操作mysql数据库时为什么非得用 '{$username}'的方式才能查询,PHP语法中双引号就能解析变量啊 ,为什么我用第一种会出错呢?

1、变量$username和$password在单引号里面是不解析的
2、字符型字段username和password后面的值是需要加引号的。

<code>$sql='select * from sh_admin where username="'.$username.'" and password="'.$password.'" limit 1';</code>
Copy after login

你第一条sql用的是单引号啊 所以出错
是用

<code>$sql="select * from sh_admin where username=$username and password=$password limit 1";
</code>
Copy after login

就没问题了

同意楼上,字符串是要加引号的。

调试一下 将$sql输出一下,一眼就能看出了!
第一句 单引号里面的变量不解析,且sql里面 字符串是要引起来的

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!