php中sql语句常见错误

时间:2023-03-08 19:45:41
php中sql语句常见错误

.php文件中sql语句的写法导致的错误如下:

1、$logSql="select * from jd_login where uname=".$u."and upwd=".$p;

报错如下:

查询用户失败: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'upwd=456789' at line 1

2、$logSql='SELECT * FROM jd_login WHERE uname='.$u;

报错如下:

查询用户失败: Unknown column 'naicha' in 'where clause'

当然我的数据库表字段都是没有问题的,这里报错的原因是因为""号可以自动解析变量,所以没必要做拼接,

正确查询语句应如下:

$logSql="select * from jd_login where uname='$u' and upwd='$p'";
ID 用户名 密码
20 naicha 456789