你好,並提前感謝您的任何幫助。試圖逃脫單引號和雙引號的PHP MySQL查詢
當我將查詢全部粘貼到phpMyAdmin中時,我得到結果集。當我嘗試在PHP腳本中運行查詢時,出現PHP錯誤。我認爲問題在於我如何逃避代碼的MySQL語句部分中的單引號(撇號)。任何人都可以推薦一個轉義方法來用於這個精心設計的MySQL查詢。
下面是MySQL的代碼的一部分作爲在phpMyAdmin(它工作正常)進入....
SET SESSION group_concat_max_len = 2000000;
SET @radius = .014;
select
cast(concat('{"type":"Feature","id":"',t2.TerrID,'","properties": {"name":"',t2.TerrName,'","density":',t2.TotalOpp2,',"color":"',t2.TerrClr,'"},','"geometry" :{"type":"MultiPolygon","coordinates":[', t2.tett2,']}},')as char) as tett
from(
select TerrName,
TerrID,
sum(TotalOpp) as TotalOpp2,
AgentsAssigned,
(sum(TotalOpp) - AgentsAssigned * 60) as density,
if((sum(TotalOpp) - AgentsAssigned * 60)<0,"red", if((sum(TotalOpp) - AgentsAssigned * 60)<60,"yellow","green")) as TerrClr,
group_concat(tett) as tett2
from(
SELECT
territories.territory_name as TerrName,
territories.territoryID as TerrID,
territories_meta.tm_color,
territories.territory_description,
territories.territory_state,
GROUP_CONCAT(distinct(territories_zips.tz_zip)SEPARATOR ', ') AS ZipCodes,
GROUP_CONCAT(distinct(concat(users.user_Fname,' ',users.user_Lname))SEPARATOR ', ') AS AgentName,
users.user_role,
round(sum(boundaries_meta.bm_opportunity)/Count(distinct(territories_assign.ta_repID))) AS TotalOpp,
Count(distinct(territories_assign.ta_repID)) AS AgentsAssigned,
group_concat(boundaries.boundary_geometry)as tett
FROM
territories
INNER JOIN territories_zips ON territories.territoryID = territories_zips.tz_terrID
INNER JOIN territories_assign ON territories.territoryID = territories_assign.ta_territoryID...
... ...
這裏就是我試圖添加代碼來運行數據庫查詢PHP腳本的一部分...
$places_zipopps_terr3 = $db->query('SET SESSION group_concat_max_len = 2000000;
SET @radius = .014;
select
cast(concat(\'{"type":"Feature","id":"\',t2.TerrID,\'","properties": {"name":"\',t2.TerrName,\'","density":\',t2.TotalOpp2,\',"color":"\',t2.TerrClr,\'"},\',\'"g eometry":{"type":"MultiPolygon","coordinates":[\', t2.tett2,\']}},\')as char) as tett
from(
select TerrName,
TerrID,
sum(TotalOpp) as TotalOpp2,
AgentsAssigned,
(sum(TotalOpp) - AgentsAssigned * 60) as density,
if((sum(TotalOpp) - AgentsAssigned * 60)<0,"red", if((sum(TotalOpp) - AgentsAssigned * 60)<60,"yellow","green")) as TerrClr,
group_concat(tett) as tett2
from(
SELECT
territories.territory_name as TerrName,
territories.territoryID as TerrID,
territories_meta.tm_color,
territories.territory_description,
territories.territory_state,
GROUP_CONCAT(distinct(territories_zips.tz_zip)SEPARATOR \', \') AS ZipCodes,
GROUP_CONCAT(distinct(concat(users.user_Fname,\' \',users.user_Lname))SEPARATOR \', \') AS AgentName,
users.user_role,
round(sum(boundaries_meta.bm_opportunity)/Count(distinct(territories_assign.ta_repID))) AS TotalOpp,
Count(distinct(territories_assign.ta_repID)) AS AgentsAssigned,
group_concat(boundaries.boundary_geometry)as tett
FROM
territories
INNER JOIN territories_zips ON territories.territoryID = territories_zips.tz_terrID
INNER JOIN territories_assign ON territories.territoryID = territories_assign.ta_territoryID...
.....
爲什麼你在'.sql'文本文件中沒有這個巨大的東西,讀入,然後發送? – tadman
謝謝你tadman。 .sql文件的想法讓我想到了,爲什麼不使用存儲過程。我們對.sql文件做了一個快速實驗,但遇到了一些格式問題,在進一步深入研究之前,我想嘗試一下SP。無論如何,快速提問存儲過程對文本文件有什麼優缺點?再次感謝您 – alex
存儲過程是一種更正式的方式。如果你運行這個不重要的次數,這可能是最好的方法,因爲它會明確查詢的內容。另一方面,更改存儲過程以對代碼進行微小調整是令人討厭的,因此對於幾乎與運行一樣頻繁更改的內容,請不要煩惱。 – tadman