执行 pdo insert 排除某些参数
Posted
技术标签:
【中文标题】执行 pdo insert 排除某些参数【英文标题】:execute pdo insert excluding certain parameters 【发布时间】:2018-10-04 16:48:59 【问题描述】:我的 SELECT 查询正在正确获取所有内容,但我的插入失败,因为我的参数值包含太多的插入(frame2、cover2、color2 等)
我的第二次选择需要额外的参数,但插入不需要它们。
有没有办法执行我的插入并说“在没有 :frame2、:cover2、:color2、:frame3、:cover3、:color3 的情况下在 $salesValues 上执行”
$selectPlacements = "
SELECT DISTINCT
sku_id, s.frame as frame, sg.group_code as group_code, sg.name as group_name, s.cover1 as cover, s.color1 as color, dealer_id, sales_rep as repNum, count(*) as placements
FROM place p
inner join skus s
on p.sku_id = s.id
inner join sku_groups sg
on sg.id = s.sku_group_id
where p.expire_date > curdate()
group by sku_id, s.frame, dealer_id
;
";
try
$placementCheck = $mysqlConn->prepare($selectPlacements);
$detailRslt = $placementCheck->execute();
while ($placementRow = $placementCheck->fetch(PDO::FETCH_ASSOC))
print_r($placementRow); //This prints the array I need
$salesValues = [
":dealer_id" => $placementRow["dealer_id"],
":frame" => $placementRow["frame"],
":cover" => $placementRow["cover"],
":color" => $placementRow["color"],
":frame2" => $placementRow["frame"],
":cover2" => $placementRow["cover"],
":color2" => $placementRow["color"],
":frame3" => $placementRow["frame"],
":cover3" => $placementRow["cover"],
":color3" => $placementRow["color"],
":group_code" => $placementRow["group_code"],
":group_name" => $placementRow["group_name"],
":placements" => $placementRow["placements"],
":repNum" => $placementRow["repNum"],
];
$insertSales = "
MERGE INTO Tmetrics AS T
USING (VALUES (
CAST(:dealer_id as CHAR(10)),
CAST(:frame as CHAR(10)),
CAST(:cover as CHAR(10)),
CAST(:color as CHAR(10)),
CAST(:group_code as CHAR(10)),
CAST(:group_name as CHAR(25)),
CAST(:sales180Cust as CHAR(10)),
CAST(:velocityCust as CHAR(10)),
CAST(:repnum as CHAR(10)),
CAST(:sales180Rep as CHAR(10)),
CAST(:velocityRep as CHAR(10)),
CAST(:sales180Comp as CHAR(10)),
CAST(:velocityComp as CHAR(10)),
CAST(:placements as CHAR(10))
)
)
AS S(dealer, frame, cover, color, groupnum, groupname, last180days_sales,velocity,repnumber,last180_rep,velocityrep,last180company,velocitycompany,placements)
ON S.dealer = T.dealer and S.frame = T.frame and S.cover = T.cover and S.color = T.color
WHEN MATCHED
THEN UPDATE SET last180days_sales = S.last180days_sales, velocity = S.velocity, repnumber = S.repnumber, last180_rep = S.last180_rep, velocityrep = S.velocityrep, last180company = S.last180company, velocitycompany = S.velocitycompany
WHEN NOT MATCHED
THEN INSERT VALUES(S.dealer, s.frame, s.cover, s.color, s.groupnum, s.groupname, s.last180days_sales,s.velocity,s.repnumber,s.last180_rep,s.velocityrep,s.last180company,s.velocitycompany,s.placements)
";
try
$velocityExecution = $velocityInsertPrep->execute($salesValues);
catch(PDOException $ex)
echo "INSERT_FAILED: " .$ex->getMessage();
【问题讨论】:
【参考方案1】:没有办法将值传递给 PDOStatement::execute()
并告诉 PDO 不要使用它们,但您可以很容易地不将这些值传递给 execute()
开始,使用纯 php array_diff_key()
:
// Note, the actual values can be anything. The keys just need to be set.
$ignores = [
'frame2' => false,
'cover2' => false,
'color2' => false,
];
$velocityExecution = $velocityInsertPrep->execute(array_diff_key($salesValues, $ignores));
【讨论】:
以上是关于执行 pdo insert 排除某些参数的主要内容,如果未能解决你的问题,请参考以下文章