2017-10-20 75 views
0

我有一個SQL查詢,其中我將英寸轉換爲英尺。如何解決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?但我「米仍不能確定什麼,我做錯了

+0

我不明白爲什麼你使用'取代()'在第一p花邊,當原始字符串是文字。只需使用字符串連接。 – Barmar

+0

試試這個:$查詢=」 SELECT *更換(更換( ''', '',高度/ 12), '',高度%12)playerHeight,abbrName FROM leagueRosters INNER JOIN leagueTeams AS teamInfo ON leagueRosters.teamId = teamInfo.teamId WHERE abbrName LIKE'$ _GET [team]' ORDER BY rosterId DESC 「; – Nic3500

+0

注意:mysqli的面向對象的接口明顯不那麼冗長,使得代碼更易於閱讀和審計,並且不容易與陳舊的mysql_query接口混淆。在你過於投入程序風格之前,它是值得轉換的。例如:'$ db = new mysqli(...)'和'$ db-> prepare(「...」)過程接口是PHP4時代的一個神器,當引入mysqli API時,不應該在新的碼。 – tadman

回答

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 
"; 
+0

Ooooh ok,非常感謝@barmar的幫助和信息。我只有4周的時間自己學習SQL,因爲我在這裏找到的信息,我使用了'replace' - [link] https://stackoverflow.com/questions/30078773/converting-a-number-到英尺英寸%5D – ssx95351