使用 mysqli & 将数百条记录插入 mysql 数据库PHP
我正在制作一个 Flex 4 应用程序并使用 ZendAMF 与 MySQL 数据库交互。我让 Flex 为我生成了大部分服务代码(它使用了 mysqli),并粗略地编辑了一些代码(对于 PHP,我还是个新手)。此时一切正常。
我的问题是 - 目前,应用程序在创建用户时将大约 400 条记录插入数据库(它保存自己的数据供他们稍后加载),但它是通过单独调用服务器来实现这一点的 - 即每条记录发送到服务器,然后保存到数据库,然后发送下一条记录。
这在我的本地环境中运行良好,但由于使用实时网络服务器,它只在某些时候添加这些记录。其他时候它会完全忽略它。 我假设它这样做是因为实时数据库不喜欢几乎同时收到数百个请求的垃圾邮件
我认为更有效的方法是将所有这些记录打包到一个数组,将其发送到服务器一次,然后让 PHP 服务对数组中的每个项目执行多次插入。问题是,我不知道如何使用 mysqli 语句在 PHP 中进行编码。
任何帮助或建议将不胜感激 - 谢谢!
I'm making a Flex 4 application and using ZendAMF to interact with a MySQL database. I got Flex to generate most of the services code for me (which utilizes mysqli) and roughly edited some of the code (I'm very much a novice when it comes to PHP). All works fine at this point.
My problem is - currently the application inserts ~400 records into the database when a user is created (it's saving their own data for them to load at a later date) but it does this with separate calls to the server - i.e. each record is sent to the server, then saved to the database and then the next one is sent.
This worked fine in my local environment, but since going on a live webserver it only adds these records some of the times. Other times it will totally ignore it. I'm assuming it's doing this because the live database doesn't like getting spammed with hundred of requests at practically the same time
I'm thinking a more efficient way would be to package all of these records into an array, send that to the server just the once and then get the PHP service to do multiple inserts on each item in the array. The problem is, I'm not sure how to go about coding this in PHP using mysqli statements.
Any help or advice would be greatly appreciated - thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
阅读加载数据本地INFILE< /a>. 似乎这正是插入多条记录所需要的:它通过一次操作将文件中的许多记录(不幸的是,不是数组)插入到表中。
使用 LOAD DATA LOCAL INFILE 进行多记录更新比使用每行更新要快得多。
Read up on LOAD DATA LOCAL INFILE. It seems it's just what you need for inserting multiple records: it inserts many records from a file (though not an array, unfortunately) into your table in one operation.
It's also much, much faster to do multiple-record UPDATEs with LOAD DATA LOCAL INFILE than with one-per-row UPDATEs.
ee 你应该单独处理用户默认值,只存储更改
,如果没有保存,你必须检查 mysql 的警告或 php 的错误,数据不会消失,
尝试
请在插入之前
以及
此后
ee you should handle the user defaults separate and only store the changes
and if they are not saved you must check warnings form mysql or errors form php, data don't disappear
try
just before insering
and
aferwards