首页 > 解决方案 > PHP MYSQL SELECT 查询参数包含一个 & 符号

问题描述

我正在使用 PHP PDO 准备好的语句。我传入一个字符串并从 MYSQL 返回记录。我将三个变量传递给该方法。

查询不返回任何内容。如果我在 phpmyadmin 中执行相同的查询,它会返回所有正确的数据。我相信它是 $team 变量中的 & 符号,但是,不知道我要解决它。我没有使用链接,它不是表单元素。这是对该方法的直接调用。三个参数的值是

    $season = '2018-19';
    $league = 20;
    $team = "Texas A&M University-Kingsville"; 

这是我的方法:

    public static function getTeamGames($season, $league, $team){

        $conn = parent::connect();            
        $sql = "SELECT * FROM rfw_games WHERE season = :season && 
            league = :league && home = :team";
        try {
            $st = $conn->prepare( $sql );
            $st->bindValue( ":season", $season, PDO::PARAM_STR );
            $st->bindValue( ":team", $team, PDO::PARAM_STR );
            $st->bindValue( ":league", $league, PDO::PARAM_INT );
            $st->execute();

            $games = array();
            foreach ( $st->fetchAll() as $row ) {
                $games[] = new Game( $row );
            }
            parent::disconnect( $conn);
            return $games;
        } catch (PDOException $e ) {
            parent::disconnect( $conn );
            die( "Query failed: " . $e->getMessage() );
        }
    }

    $weeklyGames = Game::getTeamGames( $season, $league, $tName );

我真的很感谢大家的帮助。

先感谢您。

标签: phpmysqlpdo

解决方案


我能够解决这个问题。

我不得不在 $team 参数上使用 html_entity_decode。

我将方法更改为以下内容:

        public static function getTeamGames($season, $league, $team){
          $dTeam = html_entity_decode($team);

          $conn = parent::connect();              
          $sql = "SELECT * FROM rfw_games WHERE season = :season && league = :league && home = :team";

          try {
            $st = $conn->prepare( $sql );
            $st->bindValue( ":season", $season, PDO::PARAM_STR );
            $st->bindValue( ":team", $dTeam, PDO::PARAM_STR );
            $st->bindValue( ":league", $league, PDO::PARAM_INT );
            $st->execute();
            $games = array();
            foreach ( $st->fetchAll() as $row ) {
              $games[] = new Game( $row );
            }
            parent::disconnect( $conn);
            return $games;
          } catch (PDOException $e ) {
            parent::disconnect( $conn );
            die( "Query failed: " . $e->getMessage() );
          }
       }

推荐阅读