這段代碼為什麼能查詢出_id = 1001的使用者資訊?

來源:互聯網
上載者:User
 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'].'

';}$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;
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.