duanjiao6731 2016-12-12 05:58
浏览 52
已采纳

如何将PHP对象转储到SQL表中?

I'm wondering if there is a quick way to do the following:

I have a PHP object with multiple attributes. For example

$person;
$person->height = 165;
$person->name = 'john';

I would like to dump this into an SQLite MySQL DB while creating each column automatically from the attribute names of the PHP object. So for example, if I were to dump the above object into mySQL, it would look something like this:

Table : people
Name(Column, VARCHAR)  -  "John"
Height(Column, INT)  -  165 

The reason I am asking this is because the number of attributes is growing constantly, and having to create and manage the table columns manually is a lot of work. I am wondering if there is an automated way of doing this.

  • 写回答

1条回答 默认 最新

  • duanqiongchong0354 2016-12-12 08:15
    关注

    First you need to convert object into array and then you can iterate through it and can create table and insert values in it.

    Something like below:

    Step 1: Convert object to array

    Step 2: Get keys(fields) and values out of array

    Step 3: Generate sql queries

        <?php
        //Step 1: convert object to array
        //$persion =  (array) $yourObject;
    
        //Step 2: get keys(fields) and values out of array
        $person = array(
            "height" => "165",
            "name" => "john",
            "age" => "23"
        );
    
        function data_type($val) {
            if(is_numeric($val)) { 
                return "int"; 
            } 
            else {
                return "varchar(15)";
            }   
        }
    
        //Step 3: sql query, only including sql query
        function create_table($person) {
            $create = "CREATE TABLE IF NOT EXISTS people";
            $ctr = 0;
            foreach($person as $key => $value) {
                if($ctr == 0) {
                    $field_query = $key." ".data_type($value);
                } else {
                    $field_query .= ", ".$key." ".data_type($value);
                }
                $ctr++;
            }
            echo $create .= " (".$field_query.")";
            echo "<br/>";
        }
        create_table($person);
    
        function insert_table($person) {
            $ctr = 0;
            foreach($person as $key => $value) {
                if($ctr == 0) {
                    $field_query = $key;
                    $value_query = $value;
                } else {
                    $field_query .= ", ".$key;
                    $value_query .= ", ".$value;
                }
                $ctr++;
            }
            echo $insert = "INSERT INTO people"." (".$field_query.") VALUES (".$value_query.")";
        }
        insert_table($person);
    
        ?>
    

    Hope this will help you in some way(y).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装