我正在使用 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 );
非常感谢大家的帮助。
提前谢谢您。
最佳答案
我能够解决这个问题。
我必须在 $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() );
}
}
关于PHP MYSQL SELECT 查询参数包含与号(&),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51991153/