dqknycyt92288 2010-09-11 00:24
浏览 70
已采纳

在PHP中将querystring转换为int

Working on my first PHP site (I'm mainly an ASP programmer), and I need to convert a querystring variable into a number I can then use to query a database. What's the best way to convert to a usable int (I tried intval() already but I keep getting 0 as a result) and also validate it (AKA no single quotes, blah blah) in PHP?

  • 写回答

2条回答 默认 最新

  • dongshi8038 2010-09-11 00:35
    关注

    PHP allows for far more flexibility with types than ASP, and will convert between different types automatically.

    The best way to ensure a number in your SQL is to use sprintf(), for example:

    $sql = "SELECT name FROM users WHERE id = ".sprintf("%d", $_POST['userid']) ;
    

    When inserting strings delivered by GET or POST into your SQL, you should use mysql_real_escape_string() (assuming your SQL is going to MySQL) to escape anything that needs escaping, so:

    $sql = "SELECT id FROM users where name = ".sprintf("'%s'", mysql_real_escape_string($_GET['username'])) ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题