简体   繁体   English

MySQL表无法从PHP数组中获取所有值

[英]MySQL table doesn't get all values from PHP array

I am reading the lines from a CSV file and storing in a PHP array. 我正在从CSV文件中读取行并将其存储在PHP数组中。 I created a function to save the array content into a MySQL table. 我创建了一个函数,用于将数组内容保存到MySQL表中。 However, it only saves around 210 rows while the array has more than 100K positions... I checked the array size and it shows much more than what is being saved into the database. 但是,当数组的位置超过100K时,它仅保存约210行...我检查了数组的大小,它显示的内容远远超过了保存到数据库中的内容。 Is there any limit per operation that MySQL accepts? MySQL接受的每个操作有任何限制吗?

Also, MySQL doesn't show any error. 另外,MySQL不显示任何错误。 It only loads those few records and that's it. 它仅加载那几条记录,仅此而已。

My function: 我的功能:

    function save_uar($data)
{       
    if (isset($data))
    {               
        foreach($data as $row)
        {
            $id = $row['_id'];
            $period = $row['period'];
            $reviewStatus = $row['reviewStatus'];
            $dataSource = $row['dataSource'];
            $dataId = $row['dataId'];
            $offset = $row['offset'];
            $employeeId = $row['employeeId'];
            $firstName = $row['firstName'];
            $lastName = $row['lastName'];
            $resourceName = $row['resourceName'];
            $resourceType = $row['resourceType'];
            $resourceUserId = $row['resourceUserId'];
            $role = $row['role'];
            $reference = $row['reference'];
            $resourceGroup = $row['resourceGroup'];
            $businessUnit = $row['businessUnit'];
            $lifeCycle = $row['lifeCycle'];
            $userComment = $row['userComment'];
            $extractDate = $row['extractDate'];
            $reason = $row['reason'];
            $reviewDate = $row['reviewDate'];
            $reviewerId = $row['reviewerId']
            $sql = "INSERT INTO uar VALUES (
            '".$id."', '
            ".$period."', '
            ".$reviewStatus."', '
            ".$dataSource."', '
            ".$dataId."',
            ".$offset.", 
            ".$employeeId.", '
            ".$firstName."', '
            ".$lastName."', '
            ".$resourceName."', '
            ".$resourceType."', '
            ".$resourceUserId."', '
            ".$role."', '
            ".$reference."', '
            ".$resourceGroup."', '
            ".$businessUnit."', '
            ".$lifeCycle."', '
            ".$userComment."', '
            ".$extractDate."', '
            ".$reason."', '
            ".$reviewDate."', '
            ".$reviewerId."'
            ); ";
            $result = $this->db->query($sql);           
    }
}
}

As it was suggest by @Num6, after escaping the variables the code worked. 正如@ Num6所建议的那样,转义变量后代码可以工作。 I used a CodeIgniter function: 我使用了CodeIgniter函数:

$sql = "INSERT INTO uar VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 

            $result = $this->db->query($sql, array($id, $period ,$reviewStatus, $dataSource, $dataId, $offset, $employeeId,
            $firstName, $lastName, $resourceName, $resourceType, $resourceUserId, $role,
            $reference, $resourceGroup, $businessUnit, $lifeCycle, $userComment, $extractDate,
            $reason, $reviewDate, $reviewerId));

I confirmed the issue when I tried to run the function directly from the browser (I was following the jquery result before). 当我尝试直接从浏览器运行该函数时,我确认了该问题(之前我遵循的是jquery结果)。 Now, I am having issues with the time spent to insert the execute the queries, but this is another issue not direct related tho this topic. 现在,我在插入执行查询所花费的时间上遇到了问题,但这是与该主题没有直接关系的另一个问题。

Thank you everyone! 谢谢大家!

Perhaps one of the queries makes it stop because it supplies invalid data for its column? 也许其中一个查询使其停止,因为它为其列提供了无效数据?

Are error and warning messages turned on? 错误和警告消息是否打开? You can do this at the top of your PHP: 您可以在PHP的顶部执行此操作:

error_reporting(E_ALL);
ini_set('display_errors', 1);

And make it stop on a bad query to catch it: 并使其停止在错误的查询上以进行捕获:

if ($result === false) exit(0);

You should consider reading one csv row at a time, and passing that to a prepared statement instead of filling an array with 100k arrays. 您应该考虑一次读取一个csv行,并将其传递给准备好的语句,而不是用100k数组填充数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM