Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在多个查询中进行多个字符串替换?_Php_Mysql_Loops_Str Replace - Fatal编程技术网

Php 如何在多个查询中进行多个字符串替换?

Php 如何在多个查询中进行多个字符串替换?,php,mysql,loops,str-replace,Php,Mysql,Loops,Str Replace,我有一个名为$querys的数组,如下所示: Array ( [0] => SELECT * FROM StudentRecord WHERE Year = '%YEAR%' AND Session = '%SESSION%' AND StudentID = '%SID%'; [1] => SELECT * FROM Student_ApplicationRecord WHERE ApplicationYear = '%YEAR%' AND Session%SES

我有一个名为
$querys
的数组,如下所示:

Array ( 
    [0] => SELECT * FROM StudentRecord WHERE Year = '%YEAR%' AND Session = '%SESSION%' AND StudentID = '%SID%'; 
    [1] => SELECT * FROM Student_ApplicationRecord WHERE ApplicationYear = '%YEAR%' AND Session%SESSION% = 1; 
)
$variables = array('YEAR' => 2016, 'SESSION' => 1, 'SID' => $_SESSION['sid']);
Array ( 
    [0] => SELECT * FROM StudentRecord WHERE Year = '2016' AND Session = '1' AND StudentID = '1234'; 
    [1] => SELECT * FROM Student_ApplicationRecord WHERE ApplicationYear = '2016' AND Session1 = 1; 
)
我还有第二个数组,名为
$variables
,如下所示:

Array ( 
    [0] => SELECT * FROM StudentRecord WHERE Year = '%YEAR%' AND Session = '%SESSION%' AND StudentID = '%SID%'; 
    [1] => SELECT * FROM Student_ApplicationRecord WHERE ApplicationYear = '%YEAR%' AND Session%SESSION% = 1; 
)
$variables = array('YEAR' => 2016, 'SESSION' => 1, 'SID' => $_SESSION['sid']);
Array ( 
    [0] => SELECT * FROM StudentRecord WHERE Year = '2016' AND Session = '1' AND StudentID = '1234'; 
    [1] => SELECT * FROM Student_ApplicationRecord WHERE ApplicationYear = '2016' AND Session1 = 1; 
)
我想用
$variables
中的正确值替换
$querys
中的变量

我已经尝试了许多解决方案,目前为止我想到的最好的解决方案是:

foreach ($queries as $query)
    foreach($variables as $key=>$value)
        $newqueries[] = str_replace("%".$key."%", $value, $query);
但是,这在每个查询中一次只替换一个变量

i、 e.当我这样做时:

foreach ($newqueries as $query)
    print ($query);
每个查询的结果是:

SELECT * FROM StudentRecord 
WHERE Year = '%YEAR%' AND Session = '%SESSION%' AND SID = '%SID%'; 

SELECT * FROM StudentRecord 
WHERE Year = '%YEAR%' AND Session = '1' AND SID = '%SID%';

SELECT * FROM StudentRecord 
WHERE Year = '%YEAR%' AND Session = '%SESSION%' AND SID = '1234';
我想要的是最后一个数组
$newquerys
,如下所示:

Array ( 
    [0] => SELECT * FROM StudentRecord WHERE Year = '%YEAR%' AND Session = '%SESSION%' AND StudentID = '%SID%'; 
    [1] => SELECT * FROM Student_ApplicationRecord WHERE ApplicationYear = '%YEAR%' AND Session%SESSION% = 1; 
)
$variables = array('YEAR' => 2016, 'SESSION' => 1, 'SID' => $_SESSION['sid']);
Array ( 
    [0] => SELECT * FROM StudentRecord WHERE Year = '2016' AND Session = '1' AND StudentID = '1234'; 
    [1] => SELECT * FROM Student_ApplicationRecord WHERE ApplicationYear = '2016' AND Session1 = 1; 
)
有人能帮我吗?如果以前有人问过这个问题,我很抱歉,但是我找不到任何类似的问题。

因为接受了
$search
$replace
参数的数组,所以您可以使用它

$search = ['%YEAR%', '%SESSION%', '%SID%'];
$replace = ['2016', '1', $_SESSION['id']];
$newQueries = [];
foreach ($queries as $query) {
    $newQueries[] = str_replace($search, $replace, $query);
}

为什么不使用带参数绑定的prepared语句呢?