我会保持简短快捷。指出我正确的方向或告诉我如何做。
我有一个数组(如下所示) - 我希望能够替换值?与第二个数组中的元素。那么第一个?被替换为数组2中的第一个值,依此类推。
我没有推出自己的PDO,代码库很老,目前还没有使用PDO。只需要知道我如何在下面的数组中进行准备和绑定,使其成为一个快速的sql语句:
Dim Current_Day As String = Format(Now, "dddd")
Dim Day_Name_Val As String = "0"
Select Case Current_Day
Case "Saturday"
Day_Name_Val = "1"
Case "Monday"
Day_Name_Val = "2"
Case "Tuesday"
Day_Name_Val = "3"
Case "Wednesday"
Day_Name_Val = "4"
Case "Thursday"
Day_Name_Val = "5"
Case "Friday"
Day_Name_Val = "6"
Case "Sunday"
Day_Name_Val = "7"
End Select
Var Dump Output
Update tblA set name = 'SRTest12', agency = 'AUS', feature_tag = 'SRTest1', sart_date = '2000-01-01 00:00:00', end_date = '2050-12-31 00:00:00', ongoing = '0', hide = '1' where id = 165
Print_r输出
> array(2) { > [0]=> > string(126) "UPDATE tblA SET name = ?,agency = ?,feature_Tag = ?,start_Date = ?,end_Date = ?,hide_text = ?,ongoing = > ? WHERE id = ?" > [1]=> > array(8) { > [0]=> > string(8) "SRtest12" > [1]=> > string(5) "AUS" > [2]=> > string(7) "SRTest1" > [3]=> > string(19) "2000-01-01 00:00:00" > [4]=> > string(19) "2050-12-31 00:00:00" > [5]=> > string(1) "0" > [6]=> > string(1) "1" > [7]=> > string(3) "165" > } > }
答案 0 :(得分:1)
最直接的方法可能是preg_replace_callback
,带有一个计数器:
$query = 'UPDATE...';
$data = ['foo', 'bar', ...];
$counter = 0;
$result = preg_replace_callback('/\?/', function () use (&$counter, $data) {
return $data[$counter++];
}, $query);
您还希望添加代码以在回调中转义/引用SQL语法的值。为了完整起见,我可能还会检查$data[$counter]
是否存在,如果不存在则抛出异常。