[英]insert and select strings array from database.?
I want all strings array insert in a row the database, and after that, elsewhere select only the first part of they(strings array) And displaying they(first part strings array) by loop foreach. 我希望所有的字符串数组都插入到数据库的行中,然后,在其他地方仅选择它们的第一部分 (字符串数组)并通过foreach循环显示它们(第一部分字符串数组)。
What function do I do [ serialized
or implode
or json_encode
or ?
我要执行什么功能[ serialized
或implode
或json_encode
或?
]? ]? Did you can give me an example? 你能给我一个例子吗?
EXAMPLE: 例:
data(strings array): 数据(字符串数组):
- "input name='units[]'" = value => hello, how, where “输入名称='units []'” =值=>你好,如何,在哪里
- "input name='units[]'" = value => hi, what, other “输入名称='units []'” =值=>嗨,什么,其他
I want this output: 我想要这个输出:
- hello 你好
- hi 你好
The right way to do this is normally using 'implode' and loop using while
and one of the mysql_fetch functions: 正确的方法通常是使用“ implode”并使用while
和mysql_fetch函数之一进行循环:
$units = $_REQUEST['units'];
$units = array_map( 'mysql_real_escape_string', $units );
mysql_query( "INSERT INTO <table name> VALUES('" .
implode("'),('", $units)."')" );
Then: 然后:
$q = mysql_query( 'select * from <table name>' );
while( $row = mysql_fetch_row( $q ) )
{
echo $row[0];
}
Sometimes, however, serialize
is the better choice: 但是,有时serialize
是更好的选择:
$units = $_REQUEST['units'];
$units = array_map( 'mysql_real_escape_string', $units );
mysql_query( "INSERT INTO <table name> VALUES('" .
serialize($units)."')" );
Then: 然后:
$q = mysql_query( 'select * from <table name>' );
while( $row = mysql_fetch_row( $q ) )
{
$units = unserialize( $row[0] );
}
var_dump($units); // use foreach here.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.