这段代码为什么能查询出_id = 1001的用户信息?
Jun 20, 2016 pm 12:31 PM
<?phptry { $db = new PDO("mysql:host=localhost;dbname=mtest;charset=utf8", 'root', 'root'); $db -> setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $stmt = $db -> prepare("select * from zhuru where _id=:usid "); $usid = '1001 and 1=0'; $stmt->bindParam(':usid',$usid,PDO::PARAM_INT); $exer = $stmt -> execute(); while ($rows = $stmt -> fetch(PDO::FETCH_ASSOC)) { echo $rows['username'].'------' . $rows['nickname'].'<br><br>'; } $db = null;} catch(PDOException $e) { echo $e -> getMessage();}
这段代码为什么能查询出_id = 1001的用户信息?
回复讨论(解决方案)
select * from zhuru where _id=:usid
实际执行的是
select * from zhuru where _id='1001 and 1=0'
不可能满足条件
退一步说,即便不给你加上单引号,执行的也是
select * from zhuru where _id=1001 and 1=0
1=0 恒为假,表达式不会成立,自然也就找不到
问题是查询到了_id=1001的用户的信息。
select * from zhuru where _id=:usid
实际执行的是
select * from zhuru where _id='1001 and 1=0'
不可能满足条件
退一步说,即便不给你加上单引号,执行的也是
select * from zhuru where _id=1001 and 1=0
1=0 恒为假,表达式不会成立,自然也就找不到
执行的时候查询到了_id=1001的用户的信息。
实际执行的是
select * from zhuru where _id='1001 and 1=0'
因为 _id 是 int 类型,'1001 and 1=0' 转换为数值是 1001,可以满足条件
实际执行的是
select * from zhuru where _id='1001 and 1=0'
因为 _id 是 int 类型,'1001 and 1=0' 转换为数值是 1001,可以满足条件
怎么解决这个问题?
$db -> prepare("select * from zhuru where _id=:usid ");
:usid加单引号会什么都查不到,即使$usid=1001;

인기 기사

인기 기사

뜨거운 기사 태그

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











Laravel Back End : Part 2, React가있는 React 앱 구축

PHP의 컬 : REST API에서 PHP Curl Extension 사용 방법
