Reputation: 441
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):
- "input name='units[]'" = value => hello, how, where
- "input name='units[]'" = value => hi, what, other
I want this output:
- hello
- hi
Upvotes: 0
Views: 118
Reputation: 82008
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.
Upvotes: 0