duanji5116 2018-02-05 12:08
浏览 38
已采纳

too long

Sorry if the wording in the title is not correct (new to PHP). I'm trying to return results from a mysql db using below php scripts.

php script

<?php

require "conn.php";

$adopt_id = $_GET["adopt_id"];

  $query = "
select *
from temp_table
where adopt_id = $adopt_id
";

....
?>

Now if I run the above in my browser as url below, it returns as expected http://localhost/searchfeed.php?adopt_id=1

Dump of above query:

select *
from temp_table
where adopt_id = 1

Same php script but filtering on a diff field which is of varchar data type.

php script

    <?php

    require "conn.php";

    $GENDER = $_GET["gender"];

      $query = "
    select *
    from temp_table
    where gender = $GENDER
    ";

    ....
    ?>

Now if I run the above in my browser as url below, it returns null because its not getting any results = http://localhost/searchfeed.php?gender=M

I dumped the above query to a log file, seems like it doesn't do anything with the $GENDER. This is what the query looks like

select *
from temp_table
where gender = 
  • 写回答

3条回答 默认 最新

  • dongzang5815 2018-02-05 12:20
    关注

    I don't see any problem with your PHP code in your second statement, but as suggested by others, your MySQL query consists of incorrect statement.

    Please, update your MySQL statement to look either like:

    select *
    from temp_table
    where gender = '$GENDER'
    

    or

    select *
    from temp_table
    where gender like '$GENDER'
    

    Also consider using any MySQL library, as such usually consists of various security patches (as noted in the previous answer - the SQL injections)

    or at least use mysqli_real_escape_string() function

    Edit: Problem solved most probably by incorrect variable name spelling. GET variables are all stored in $_GET array.

    your value of gender can be accessed by this statement: $gender = $_GET['gender'];

    note: for others - be careful to spell variable like $_GET not $GET

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

报告相同问题?

悬赏问题

  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题