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

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

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)