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

将数组插入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 )
  • 写回答

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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器