dongmozhui3805 2014-02-24 05:10
浏览 50

PHP查询给出了错误的结果

This is a follow up question on a previous question I asked SQL query returning wrong results

Quick Background of Question

On my site a user can select players out of a squad of 32 players. There are 15 positions in the starting lineup so out of the 32 player squad 15 players need to be selected. 1 player can play more than one position

When I run the following PHP query

$sql1 = mysql_query("SELECT * FROM `allsquads` WHERE `Team` = '$t1select' 
                    AND `Position` = '$position[$i]' 
                    OR `Secondary` = '$position[$i]'") or die(mysql_error());

The Problem

The query works but it does not return players from only $t1select which is the team the user selected, it displays players from ALL teams in my database.

Any idea how I can fix this or why this is happening.

Thank you in advance

  • 写回答

2条回答 默认 最新

  • doutangtan6386 2014-02-24 05:11
    关注

    May be you should write your query like

    $sql1 = mysql_query("SELECT * FROM `allsquads` WHERE `Team` = '".$t1select."' 
                    AND ( `Position` = '$position[$i]' 
                    OR `Secondary` = '$position[$i]' ) ") or die(mysql_error());
    

    Because your OR condition will be between Position and Secondary.As you have written it will take

    `Team` = '".$t1select."' AND `Position` = '$position[$i]'
    

    And then apply Or condition to that result.

    Note :mysql functions are deprecated as of PHP 5.5 so you need to use either of mysqli or PDO statements.

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行