douhoujun9304
2019-05-04 17:34
浏览 115
已采纳

将数组插入MYSQL数据库中的单个字段

I am creating a SELECT drown down list, I would like to have it loaded in 1 of 2 ways.

First way is pulling data from database with ID, name, and Value - This way will be loaded from other pages.

Second way, I would like to load from a Single line in the database with Name and Value. But I want to have that value be loaded with an array.

How do I load that array into the database?

$name = 'name';
$value = array( 'red' => 'Red', 'blue' => 'Blue' );
$SQL = 'INSERT INTO table_name (name, value) VALUES ($name,$value);

I expect when I run the SELECT * from table_name WHERE name = "name" to use that array in the value field right away.


With what "RakeshJakhar" said, to use the implode and explode onto $value

$value = array( 'red' => 'Red', 'blue' => 'Blue' );

print_r( $value );
echo "<br />";
echo implode( ",", $value );
echo "<br />";
$implode = implode( ",", $value );
$explode = explode( ',', $implode );
print_r( $explode );

Results:

Array ( [red] => Red [blue] => Blue ) 

Red,Blue

Array ( [0] => Red [1] => Blue )

图片转代码服务由CSDN问答提供 功能建议

我正在创建一个 SELECT 淹没列表,我想将它加载到 2种方式中的1种。

第一种方法是从ID,名称和值的数据库中提取数据 - 这种方式将从其他页面加载。

第二种方式,我 想要从名称和值的数据库中的单行加载。 但是我希望用数组加载该值。

如何将该数组加载到数据库中?

  $ name  ='name'; 
 $ value = array('red'=&gt;'Red','blue'=&gt;'Blue'); 
 $ SQL ='INSERT INTO table_name(name,value)VALUES($  name,$ value); 
   
 
 

我希望从table_name运行 SELECT * WHERE name =“name”来使用该数组 立即在值字段中。


使用“RakeshJakhar”所说的内容,使用内爆并爆炸到 $ value

  $ value = array('red'=&gt;'Red','blue'=&gt;'Blue'); 
 
print_r($ value); 
echo“  &lt; br /&gt;“; 
echo implode(”,“,$ value); 
echo”&lt; br /&gt;“; 
 $ implode = implode(”,“,$ value); 
 $ explode  = explode(',',$ implode); 
print_r($ explode); 
   
 
 

结果:

 数组([红色] =&gt;红色[蓝色] =&gt;蓝色)
 
红色,蓝色
 
Array([0] =&gt;红色[1] =&gt;蓝色)
    
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongye9453 2019-05-04 18:10
    已采纳

    You can use json_encode and json_decode

    $value  = array( 'red' => 'Red', 'blue' => 'Blue' );
    $column = json_encode($value);
    

    So the inserted value {"red":"Red","blue":"Blue"}

    When you are fetching value use json_decode

    $values = json_decode($column,true);
    
    打赏 评论

相关推荐 更多相似问题