doushe7934 2018-06-08 05:47
浏览 68
已采纳

从sql查询中的url获取id [duplicate]

This question already has an answer here:

I have an sql query to fetch data from database in php. I am using the limit to limit data. I need to limit data according to the id in the url.

How can I call the id in the sql query. I have tried the following but it's showing an error:

$response=mysqli_query($con,"select * from prometric ORDER BY RAND() LIMIT echo $_GET['id']; ");

and the error is

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs urse\prometricajax.php on line 14

</div>
  • 写回答

2条回答 默认 最新

  • douzi6060 2018-06-08 12:40
    关注

    It could be, worse, if you got it 'right' there would be an SQL injection vulnerability in your application.

    die('the following appears to work but is unsafe');
    $response=mysqli_query($con,"select * from prometric ORDER BY RAND() LIMIT $_GET[id]; ");
    

    I've got die above because noone should use such code ever.

    eg: called as

    https://you.example.com/thing.php?id=1%3btruncate+prometric%3bcommit
    

    could delete all the content from the prometric table.

    At a bare minimum do this.

    $response=mysqli_query($con,"select * from prometric ORDER BY RAND() LIMIT ".(0+$_GET['id']));
    

    the above forces $_GET['id'] to a number by adding 0, this should block all injection attacks, the worst they can do is use a big number and produce an error for number too big, etc.

    but probably a better sanitization is called for. Eg: copy $_GET['id'] into a variable, and check that is in an acceptable range before using it in SQL.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line