gpt4 book ai didi

mysql - 如何修复 SQL 查询中的单引号错误?

转载 作者:行者123 更新时间:2023-11-29 10:33:57 25 4
gpt4 key购买 nike

我有一个 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

我也尝试过这个

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

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

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

最佳答案

您需要转义双引号,这样它就不会结束 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
";

关于mysql - 如何修复 SQL 查询中的单引号错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46854559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com