简体   繁体   中英

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.

What function do I do [ serialized or implode or json_encode or ? ]? Did you can give me an example?

EXAMPLE:
data(strings array):

  1. "input name='units[]'" = value => hello, how, where
  2. "input name='units[]'" = value => hi, what, other

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:

$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:

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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