php mysql select查询参数包含一个符号(&)

9lowa7mx  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(319)

我用的是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 );

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

pbossiut

pbossiut1#

我能解决这个问题。
我不得不对$team参数使用html\u entity\u 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() );
          }
       }

相关问题