如何在SQL查询中修复单引号错误?

时间:2017-10-20 17:42:57

标签: mysql phpmyadmin

我有一个{ "key": "ctrl+shift+9", "command": "editor.emmet.action.wrapWithAbbreviation", "when": "editorHasSelection", "args": { "abbreviation": "span" } }, 查询,我将英寸转换为英尺。

SQL

根据我的理解,我应该将单引号翻倍,我已经完成了

<?php  
$query ="
SELECT *
,replace (replace('<feet>'' <inches>"',
                   '<feet>', height / 12),
           '<inches>', height % 12) AS playerHeight
,abbrName
FROM leagueRosters
INNER JOIN leagueTeams AS teamInfo
ON leagueRosters.teamId=teamInfo.teamId

WHERE abbrName LIKE '".$_GET['team']."'
ORDER BY rosterId DESC
";  
$resultRoster = mysqli_query($connect, $query);  
?>

我也试过这个

,replace (replace("<feet>"" <inches>"",
                   "<feet>", height / 12),
           "<inches>", height % 12) AS playerHeight

两者都不起作用。我尝试过其他一些组合,但总是至少在一行上出现错误。

我在这个问题中按照了答案 - How do I escape a single quote in SQL Server?但我仍然不确定我做错了什么。

1 个答案:

答案 0 :(得分:1)

您需要转义双引号,以便它不会结束PHP字符串。

$query ="
SELECT *
,replace (replace('<feet>'' <inches>\"',
                   '<feet>', height / 12),
           '<inches>', height % 12) AS playerHeight
,abbrName
FROM leagueRosters
INNER JOIN leagueTeams AS teamInfo
ON leagueRosters.teamId=teamInfo.teamId

WHERE abbrName LIKE '".$_GET['team']."'
ORDER BY rosterId DESC
";

但是首先不需要使用replace,只需使用字符串连接。

$query ="
SELECT *
,CONCAT(FLOOR(height/12), ''' ', height % 12, '\"') AS playerheight
,abbrName
FROM leagueRosters
INNER JOIN leagueTeams AS teamInfo
ON leagueRosters.teamId=teamInfo.teamId

WHERE abbrName LIKE '".$_GET['team']."'
ORDER BY rosterId DESC
";