在SQL命令中输入每个数据的变量

时间:2015-02-19 05:55:23

标签: php regex

关于如何将这些数据放入变量的任何建议。

> INSERT INTO process (process, temp) VALUES ('PST_BUMP', '1'), ('IMPORT', '5'), ('IM', '5'), ('PORT', '9')

我试过了:

<?php
    $statement = "INSERT INTO process (process, temp) VALUES ('PST_BUMP', '1'), ('IMPORT', '5'), ('IM', '5'), ('PORT', '9') ";
    echo $statement . "<br><br>";

    echo $count = substr_count($statement, "), (") . "<br>";

    $remove = str_replace("INSERT INTO process (process, temp) VALUES ", "", $statement);
    echo $remove . "<br><br>";

    $str = (explode("), (",$remove));

    $string= "";

    for($i=0; $i<=$count; $i++){
       echo $str[$i] . "<br>"; 
    }

    ?>

结果如下:

`INSERT INTO process (process, temp) VALUES ('PST_BUMP', '1'), ('IMPORT', '5'), ('IM', '5'), ('PORT', '9') 

3
('PST_BUMP', '1'), ('IMPORT', '5'), ('IM', '5'), ('PORT', '9') 

('PST_BUMP', '1'
'IMPORT', '5'
'IM', '5'
'PORT', '9') `

我的目标是:

$ proc1 =&#34; PST_BUMP&#34 ;; $ proc2 =&#34; IMPORT&#34 ;; ..所以... ..

$ temp1 =&#34; 2&#34 ;; $ temp2 =&#34; 3&#34 ;; ..等...... ..

感谢:)

1 个答案:

答案 0 :(得分:0)

我认为你想在&#34; 插入&#34;中使用变量查询而不是确切的值。

要在插入查询中使用变量,可以将查询编写为:

$statement="INSERT INTO process (process, temp) VALUES ('".$proc1."', '".$temp1."'), ('".$proc2."', '".$temp2."'), ('".$proc3"', '".$temp3."')";