在PHP中动态创建的数组中转义撇号

时间:2017-05-18 19:02:09

标签: php arrays escaping

我正在开发一个项目,在PostgreSQL数据库中混合许多不同的数据集。我仍然认为自己是PHP开发和脚本的初学者。我在逃避数组中的撇号时遇到了一些麻烦。我在这些论坛中尝试了一些不同的解决方案:An escaped apostrophe in associative array valueReplace apostrophe in a dynamically created insert statementhttp://www.codingforums.com/php/296075-array_walk_recursive-w-function-takes-2-parameters-mysqli_real_escape_string.html,最后是Escaping quotation marks in PHP。我目前正在尝试使用PDO版本重新创建我的脚本,因此我不必清理文本。至少我所理解的是我所做的所有研究中更好的方法。我目前正在寻找的是一种逃避角色的方法,同时我找到了更有说服力的解决方案。这是我用于导入过程的主要代码:

<?php


include('connect_local.php'); //Includes DB Connection Script
ini_set('max_execution_time', 3000); //3000 seconds = 50 minutes

$emp_get = "SELECT * FROM table1 WHERE person_type LIKE 'Employee'";
$emp_data = pg_query($conn, $emp_get);
while ($emp_row=pg_fetch_array($emp_data)) {


$oraint_get = "SELECT * FROM table2 WHERE source_enrollment_status_name LIKE  'Attended' AND employee_number LIKE '$emp_row[0]' ";
$oraint_data = pg_query($conn, $oraint_get);
$oraint_lms = "Oracle Learning Management Platform";
$oranull = "";


//foreach ($oraint_row as $oraint)
while ($oraint_row = pg_fetch_array($oraint_data)){




    $data_deposit = "INSERT INTO EDU_DATA (person_number, person_name, preferred_name, person_type, start_date, original_date_of_hire
                                 ,hire_date, email_address, region, location, gender, job_name, cbs_level, supervisor_employee_number
                                 ,supervisor_name, supervisor_person_type, business_unit, organization_2, organization_3, effective_date
                                 ,completion_date, training_item_code, days_on_to_do_list, days_overdue, initial_due_in, initial_due_in_unit 
                                 ,retraining_due_in, retraining_due_in_unit, retraining_period, retraining_period_unit 
                                 ,curriculum_code, curriculum_title, learning_course_name, learning_activity, class_duration, college, delivery_method_name
                                 ,class_location_name, class_location_country_name, learning_category, source_enrollment_status_name, lms_platform
                                 ,supervisor_1 ,supervisor_2, supervisor_3, supervisor_4, supervisor_5, supervisor_6, supervisor_7, supervisor_8) 
                          VALUES ('$emp_row[0]','$emp_row[1]','$emp_row[2]','$emp_row[4]','$emp_row[5]','$emp_row[6]','$emp_row[8]'
                                  ,'$emp_row[9]','$emp_row[16]','$emp_row[17]','$emp_row[19]','$emp_row[21]','$emp_row[22]','$emp_row[28]'
                                  ,'$emp_row[29]','$emp_row[30]','$emp_row[33]','$emp_row[44]','$emp_row[45]','$oraint_row[2]','$oraint_row[3]'
                                  ,'$oranull','$oranull','$oranull','$oranull','$oranull','$oranull','$oranull','$oranull','$oranull','$oranull'
                                  ,'$oranull','$oraint_row[4]','$oraint_row[5]','$oraint_row[6]','$oraint_row[7]','$oraint_row[8]','$oraint_row[9]'
                                  ,'$oraint_row[10]','$oraint_row[11]','$oraint_row[12]','$oraint_lms','$emp_row[46]','$emp_row[47]','$emp_row[48]'
                                  ,'$emp_row[49]','$emp_row[50]','$emp_row[51]','$emp_row[52]','$emp_row[53]')";
    pg_query($conn, $data_deposit);

在我尝试清理文本时,我尝试将数组输出转换为字符串,然后使用addslashes而没有任何成功:

$clnname = $emp_row[1];
addslashes($clnname);

我也尝试使用我在这里找到的例子Escape single quotes in every string in php来递归地创建一个函数来处理这个问题。代码段如下:

 function escapeApos(array $emp_row)
{

    $return_array = [];
    array_walk_recursive($emp_row, function($x) use (&$return_array)
    {

        $return_array[] = str_replace("'","\\'",$x);

    }   

    return $return_array;
}

我还尝试了其他一些方法但没有取得任何成功。任何援助或协助将不胜感激。同样使用上面的函数,我不确定是否需要在数组中声明我想要清理的实际列。欢迎再次提供帮助!提前谢谢!

1 个答案:

答案 0 :(得分:0)

好的,非常感谢大家的帮助!我开始使用PDO重新创建脚本,而不是我采用的第一种方法。这是一个脚本示例,我有一些工作在我前面。但是,现在我正在使用PDO,清理文本的问题不是问题。从现在开始我将使用这种方法!

<?php

include('connect_local_pdo.php'); //Includes DB Connection Script
ini_set('max_execution_time', 3000); //3000 seconds = 50 minutes

try {
    $stmt = $conn->query('SELECT * FROM table1');
    $rows = $stmt->setFetchMode(PDO::FETCH_ASSOC);

    while ($rows = $stmt->fetch()) {

        $emp_id = $rows['person_number'];

        $stmt2 = $conn->query("SELECT * FROM table2 WHERE employee_number LIKE '$emp_id'");
        $oracleint = $stmt2->setFetchMode(PDO::FETCH_ASSOC);

        while ($oracleint = $stmt2->fetch()) {



            $GO = $conn->prepare("INSERT INTO table3 (person_number, person_name, learning_course_name) VALUES (:emp_number, :emp_name, :learning_course_name)");

            $GO->bindParam(':emp_number', $rows['person_number']);
            $GO->bindParam(':emp_name', $rows['person_name']);
            $GO->bindParam(':learning_course_name', $oracleint['learning_course_name']);

            $GO->execute();

        }


        }

    } catch (PDOException $b) {
        echo 'Data Extraction Failed: ' . $b->getMessage();

    }

再次感谢协助新手!我非常喜欢StackExchange !!你们摇滚!

相关问题