从数组中的数据创建新的db行?

时间:2017-03-03 20:30:13

标签: php

嘿伙计们试图将一些数据从(oracle)fetch_array传递给变量数组然后使用该数组数据来检查数据是否存在于mysql数据库中并创建任何当前不存在的行...这就是我到目前为止。

问题是它只检查/创建数组的1个条目,并且不检查/创建整个数组数据。我想我需要使用for循环来同时处理所有数组数据

<?php




$conn = oci_connect('asdsdfsf');
$req_number = array();

if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$stid = oci_parse($conn, "  SELECT WR.REQST_NO                          
                            FROM DEE_PRD.WORK_REQST WR                      
                            WHERE WR.WORK_REQST_STATUS_CD = 'PLAN' AND WR.DEPT_CD ='ISNG'       

                            ");
oci_execute($stid);



while (($row = oci_fetch_array($stid, OCI_BOTH+OCI_RETURN_NULLS)) != false) {
    // Use the uppercase column names for the associative array indices

        $req_number[]= $row['REQST_NO'];


}


oci_free_statement($stid);
oci_close($conn);


//MYSQL
//Connection Variables
//connect to MYSQL  

$con = mysqli_connect($servername,$username,$password,$dbname);
if (!$con)
  {
  die('Could not connect: ' . mysqli_error());
  }
        // lets check if this site already exists in DB 
        $result = mysqli_query($con,"

                                SELECT EXISTS(SELECT 1 FROM wr_info WHERE REQST_NO = '$req_number') AS mycheck; 

                                ");

        while($row = mysqli_fetch_array($result))
            {
                    if ($row['mycheck'] == "0")  // IF site doesnt exists lets add it to the MYSQL DB
                    {
                        $sql = "INSERT INTO wr_info (REQST_NO)VALUES ('$req_number[0]')";

                        if (mysqli_query($con, $sql)) {
                            $created = $req_number." Site Created Successfully";
                        } else {
                            echo "Error: " . $sql . "<br>" . mysqli_error($con);
                        }
                    }else{ // if site is there lets get some variables if they are present... 

                            $result = mysqli_query($con,"
                                                            SELECT *
                                                            FROM wr_info
                                                            WHERE REQST_NO = '$req_number[0]'
                                                        ");

                            while($row = mysqli_fetch_array($result))
                            {   

                        $do some stuff

                            }   

                        }
            }
mysqli_close($con);

?>

1 个答案:

答案 0 :(得分:0)

您创建一个数组:

$req_number = array();

循环记录以将值赋给数组:

while (($row = oci_fetch_array($stid, OCI_BOTH+OCI_RETURN_NULLS)) != false) {
    $req_number[]= $row['REQST_NO'];
}

但是从来没有循环过那个数组。相反,您只是引用数组中的第一条记录:

$sql = "INSERT INTO wr_info (REQST_NO)VALUES ('$req_number[0]')";
// etc.

注意:有几个地方直接引用数组本身($req_number)而不是数组中的元素($req_number[0]),这是可能是一个错误。你想要纠正这些错误。另外:你应该使用查询参数和预处理语句。习惯从连接值构建SQL代码就像SQL注入漏洞一样等待发生。)

不是仅引用数组中的第一个值,而是遍历数组。像这样:

for($i = 0; $i < count($req_number); $i++) {

    // The code which uses $req_number, but
    // referencing each value: $req_number[$i]

}