简体   繁体   English

从php插入mysql数组

[英]Insert into mysql from php an array

I have the following array: 我有以下数组:

$array[0] = "internet";
$array[1] = "renweb";

and so on. 等等。

I want to insert that array into a table. 我想将该数组插入表中。 I do not know how many positions the array will have as this is a post coming from a form that a user fills. 我不知道数组有多少个位置,因为这是一个来自用户填写的表格的帖子。 How can I insert them into a table? 如何将它们插入表格中?

I was thinking on a foreach 我在想一想

something like: 就像是:

foreach($array as $tags){
    $query = sprintf("insert into solution_tags values('%s')", $tags);
    $DBconnect->query($query);
}

Is it the correct approach? 这是正确的方法吗? Or is there a more easy, efficient, painless way to do it? 还是有更简单,有效,无痛的方法来做到这一点?

Any help will be much appreciated! 任何帮助都感激不尽!

You could save the array to a textfile and then do a bulk insert: 您可以将数组保存到文本文件,然后进行批量插入:

$file=.... /* your temp filename */
file_put_contents($file,implode("\n",$array));
$DBconnect->query("LOAD DATA INFILE '$file' INTO TABLE solution_tags");

...provided, of course, that the web server and the DB server share the same filesystem! ...当然,前提是网络服务器和数据库服务器共享相同的文件系统!

To answer the question you asked: 要回答您提出的问题:

Do look for the painless approach only if you feel a pain. 仅在感到疼痛时才寻找无痛方法。 Do you? 你呢? Express it then, to make others understand what certainly makes you uneasy, to let them offer certain solution for this very issue. 然后表达出来,以使其他人了解确实会使您感到不安的问题,并让他们针对此问题提供某些解决方案。 If not - just leave it as is. 如果不是,请保持原样。 Exactly the same goes for the efficiency. 效率完全相同。 And easiness too. 而且容易。 Altering your code without a reason, just out of a whim, out of nowhere, may cause much more troubles than you imagine with your current code. 一时兴起,无所事事地无故更改代码可能会导致比您当前代码想象的麻烦更多的事情。

To answer the question you didn't ask: 要回答您没有提出的问题:

Your approach, as well as in all the answers, suffers from one essential flaw: you don't format your query properly. 您的方法以及所有答案都存在一个基本缺陷:您的查询格式不正确。 Which may lead to unpredictable consequences. 这可能导致不可预测的后果。 To format your query properly you have to use prepared statements 要正确格式化查询,您必须使用准备好的语句

$stm = $DBconnect->prepare("insert into solution_tags values(?)");
$stm->bind_param('s', $tag);

foreach($array as $tag){
    $stm->execute();
}

again: this is not a matter of "efficiency" or "ease" (as a matter of fact, on a more or less complex query this approach will be a pain compared to your current one), but matter of essential approach you ought to follow with every your query. 再说一遍:这不是“效率”或“轻松”的问题(事实上,与您当前的方法相比,在某种程度上或多或少的复杂查询中,这种方法将是一件痛苦的事情),而是您应该采取的基本方法跟上您的每个查询。

On a side note, you may wish to add some field(s) to this table to link these tags to some other entities. 另外,您可能希望在此表中添加一些字段以将这些标签链接到其他实体。

The best way is using foreach like this : 最好的方法是像这样使用foreach:

foreach( $myarray as $tags) {
mysql_query('insert into solution_tags values('$tags')');
}

You can do many insert's with one query: 您可以通过一个查询执行许多插入操作:

$aValues = array();
foreach($array as $tags) {
    $aValues[] = sprintf('("%s")');
}

$query = 'INSERT INTO solution_tags VALUES '.implode(',', $aValues);
$DBconnect->query($query);

I used implode because it will handle commas for you. 我使用内implode是因为它可以为您处理逗号。

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

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